From ca1609c8218f67a30f01a40a56267e65bf386815 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 9 Aug 2017 15:57:35 -0400 Subject: [PATCH] FIX: user directory didn't update stats of users with no recent activity --- app/models/directory_item.rb | 6 ++++-- spec/models/directory_item_spec.rb | 29 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/models/directory_item.rb b/app/models/directory_item.rb index b877705d70b..4014f4d9c4d 100644 --- a/app/models/directory_item.rb +++ b/app/models/directory_item.rb @@ -41,6 +41,7 @@ class DirectoryItem < ActiveRecord::Base end ActiveRecord::Base.transaction do + # Delete records that belonged to users who have been deleted exec_sql "DELETE FROM directory_items USING directory_items di LEFT JOIN users u ON u.id = user_id @@ -48,6 +49,7 @@ class DirectoryItem < ActiveRecord::Base u.id IS NULL AND di.period_type = :period_type", period_type: period_types[period_type] + # Create new records for users who don't have one yet exec_sql "INSERT INTO directory_items(period_type, user_id, likes_received, likes_given, topics_entered, days_visited, posts_read, topic_count, post_count) SELECT :period_type, @@ -64,6 +66,7 @@ class DirectoryItem < ActiveRecord::Base WHERE di.id IS NULL AND u.id > 0 ", period_type: period_types[period_type] + # Calculate new values and update records exec_sql "WITH x AS (SELECT u.id user_id, SUM(CASE WHEN ua.action_type = :was_liked_type THEN 1 ELSE 0 END) likes_received, @@ -74,13 +77,12 @@ class DirectoryItem < ActiveRecord::Base SUM(CASE WHEN ua.action_type = :new_topic_type THEN 1 ELSE 0 END) topic_count, SUM(CASE WHEN ua.action_type = :reply_type THEN 1 ELSE 0 END) post_count FROM users AS u - LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id + LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id AND COALESCE(ua.created_at, :since) >= :since LEFT OUTER JOIN topics AS t ON ua.target_topic_id = t.id AND t.archetype = 'regular' LEFT OUTER JOIN posts AS p ON ua.target_post_id = p.id LEFT OUTER JOIN categories AS c ON t.category_id = c.id WHERE u.active AND NOT u.blocked - AND COALESCE(ua.created_at, :since) >= :since AND t.deleted_at IS NULL AND COALESCE(t.visible, true) AND p.deleted_at IS NULL diff --git a/spec/models/directory_item_spec.rb b/spec/models/directory_item_spec.rb index 37131d1bf83..2dbc51da737 100644 --- a/spec/models/directory_item_spec.rb +++ b/spec/models/directory_item_spec.rb @@ -23,14 +23,39 @@ describe DirectoryItem do UserActionCreator.enable end - let!(:post) { create_post } - it "creates the record for the user" do + post = create_post DirectoryItem.refresh! expect(DirectoryItem.where(period_type: DirectoryItem.period_types[:all]) .where(user_id: post.user.id) .where(topic_count: 1).count).to eq(1) end + it "handles users with no activity" do + post = nil + + freeze_time(2.years.ago) do + post = create_post + # Create records for that activity + DirectoryItem.refresh! + end + + DirectoryItem.refresh! + [:yearly, :monthly, :weekly, :daily, :quarterly].each do |period| + directory_item = DirectoryItem + .where(period_type: DirectoryItem.period_types[period]) + .where(user_id: post.user.id) + .first + expect(directory_item.topic_count).to eq(0) + expect(directory_item.post_count).to eq(0) + end + + directory_item = DirectoryItem + .where(period_type: DirectoryItem.period_types[:all]) + .where(user_id: post.user.id) + .first + expect(directory_item.topic_count).to eq(1) + end + end end