FIX: update_counters wasn't properly updating topic counters

This commit is contained in:
Régis Hanol 2014-07-28 22:08:31 +02:00
parent 380411457a
commit 57fef6864d
2 changed files with 14 additions and 1 deletions

View File

@ -331,7 +331,8 @@ class PostAction < ActiveRecord::Base
end
topic_id = Post.with_deleted.where(id: post_id).pluck(:topic_id).first
Topic.where(id: topic_id).update_all ["#{column} = ?", count]
topic_count = Post.where(topic_id: topic_id).sum(column)
Topic.where(id: topic_id).update_all ["#{column} = ?", topic_count]
if PostActionType.notify_flag_type_ids.include?(post_action_type_id)
PostAction.update_flagged_posts_count

View File

@ -10,6 +10,7 @@ describe PostAction do
let(:moderator) { Fabricate(:moderator) }
let(:codinghorror) { Fabricate(:coding_horror) }
let(:post) { Fabricate(:post) }
let(:second_post) { Fabricate(:post, topic_id: post.topic_id) }
let(:bookmark) { PostAction.new(user_id: post.user_id, post_action_type_id: PostActionType.types[:bookmark] , post_id: post.id) }
describe "messaging" do
@ -115,6 +116,17 @@ describe PostAction do
end
describe "update_counters" do
it "properly updates topic counters" do
PostAction.act(moderator, post, PostActionType.types[:like])
PostAction.act(codinghorror, second_post, PostActionType.types[:like])
post.topic.reload
post.topic.like_count.should == 2
end
end
describe "when a user bookmarks something" do
it "increases the post's bookmark count when saved" do
lambda { bookmark.save; post.reload }.should change(post, :bookmark_count).by(1)