From 94a4af6af77b3d6366f4c7c685d9edd2b7e9be43 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 21 Jun 2016 12:59:33 -0400 Subject: [PATCH] FIX: If posts are deleted they should be updated in consistency jobs --- app/models/topic_featured_users.rb | 25 +++++++++++++----------- spec/models/topic_featured_users_spec.rb | 18 ++++++++++++----- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/models/topic_featured_users.rb b/app/models/topic_featured_users.rb index 9d6297a4cb3..f28c555b4ce 100644 --- a/app/models/topic_featured_users.rb +++ b/app/models/topic_featured_users.rb @@ -25,6 +25,7 @@ class TopicFeaturedUsers def self.ensure_consistency!(topic_id=nil) filter = "#{"AND t.id = #{topic_id.to_i}" if topic_id}" + filter2 = "#{"AND tt.id = #{topic_id.to_i}" if topic_id}" sql = < COALESCE(featured_user1,-99) OR - COALESCE(featured_user2_id,-99) <> COALESCE(featured_user2,-99) OR - COALESCE(featured_user3_id,-99) <> COALESCE(featured_user3,-99) OR - COALESCE(featured_user4_id,-99) <> COALESCE(featured_user4,-99) + COALESCE(tt.featured_user1_id,-99) <> COALESCE(x.featured_user1,-99) OR + COALESCE(tt.featured_user2_id,-99) <> COALESCE(x.featured_user2,-99) OR + COALESCE(tt.featured_user3_id,-99) <> COALESCE(x.featured_user3,-99) OR + COALESCE(tt.featured_user4_id,-99) <> COALESCE(x.featured_user4,-99) ) +#{filter2} SQL Topic.exec_sql(sql) diff --git a/spec/models/topic_featured_users_spec.rb b/spec/models/topic_featured_users_spec.rb index 71ff4a54235..09459115eaa 100644 --- a/spec/models/topic_featured_users_spec.rb +++ b/spec/models/topic_featured_users_spec.rb @@ -1,12 +1,12 @@ require 'rails_helper' describe TopicFeaturedUsers do - it 'ensures consistenct' do + it 'ensures consistency' do t = Fabricate(:topic) Fabricate(:post, topic_id: t.id, user_id: t.user_id) p2 = Fabricate(:post, topic_id: t.id) - Fabricate(:post, topic_id: t.id, user_id: p2.user_id) + p3 = Fabricate(:post, topic_id: t.id, user_id: p2.user_id) p4 = Fabricate(:post, topic_id: t.id) p5 = Fabricate(:post, topic_id: t.id) @@ -14,11 +14,9 @@ describe TopicFeaturedUsers do featured_user2_id: 70, featured_user3_id: 12, featured_user4_id: 7, - last_post_user_id: p5.user_id - ) + last_post_user_id: p5.user_id) TopicFeaturedUsers.ensure_consistency! - t.reload expect(t.featured_user1_id).to eq(p2.user_id) @@ -26,6 +24,16 @@ describe TopicFeaturedUsers do expect(t.featured_user3_id).to eq(nil) expect(t.featured_user4_id).to eq(nil) + # after removing a post + p2.update_column(:deleted_at, Time.now) + p3.update_column(:hidden, true) + TopicFeaturedUsers.ensure_consistency! + t.reload + + expect(t.featured_user1_id).to eq(p4.user_id) + expect(t.featured_user2_id).to eq(nil) + expect(t.featured_user3_id).to eq(nil) + expect(t.featured_user4_id).to eq(nil) end end