fix regression with tag post counts
This commit is contained in:
@@ -8,20 +8,24 @@ class Favorite < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def self.add(post, user)
|
||||
return if Favorite.for_user(user.id).exists?(:user_id => user.id, :post_id => post.id)
|
||||
return if Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists?
|
||||
Favorite.create(:user_id => user.id, :post_id => post.id)
|
||||
Post.update_all("fav_count = fav_count + 1", "id = #{post.id}")
|
||||
Post.update_all("score = score + 1", "id = #{post.id}") if user.is_gold?
|
||||
Post.update_all("fav_count = fav_count + 1", {:id => post.id})
|
||||
Post.update_all("score = score + 1", {:id => post.id}) if user.is_gold?
|
||||
post.append_user_to_fav_string(user.id)
|
||||
User.update_all("favorite_count = favorite_count + 1", "id = #{user.id}")
|
||||
User.update_all("favorite_count = favorite_count + 1", {:id => user.id})
|
||||
user.favorite_count += 1
|
||||
post.fav_count += 1
|
||||
end
|
||||
|
||||
def self.remove(post, user)
|
||||
return unless Favorite.for_user(user.id).exists?(:user_id => user.id, :post_id => post.id)
|
||||
return unless Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists?
|
||||
Favorite.destroy_all(:user_id => user.id, :post_id => post.id)
|
||||
Post.update_all("fav_count = fav_count - 1", "id = #{post.id}")
|
||||
Post.update_all("score = score - 1", "id = #{post.id}") if user.is_gold?
|
||||
Post.update_all("fav_count = fav_count - 1", {:id => post.id})
|
||||
Post.update_all("score = score - 1", {:id => post.id}) if user.is_gold?
|
||||
post.delete_user_from_fav_string(user.id)
|
||||
User.update_all("favorite_count = favorite_count - 1", "id = #{user.id}")
|
||||
User.update_all("favorite_count = favorite_count - 1", {:id => user.id})
|
||||
user.favorite_count -= 1
|
||||
post.fav_count -= 1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -314,18 +314,24 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def increment_tag_post_counts
|
||||
Post.execute_sql("UPDATE tags SET post_count = post_count + 1 WHERE name IN (?)", tag_array) if tag_array.any?
|
||||
Tag.update_all("post_count = post_count + 1", {:name => tag_array}) if tag_array.any?
|
||||
end
|
||||
|
||||
def decrement_tag_post_counts
|
||||
Post.execute_sql("UPDATE tags SET post_count = post_count - 1 WHERE name IN (?)", tag_array) if tag_array.any?
|
||||
Tag.update_all("post_count = post_count - 1", {:name => tag_array}) if tag_array.any?
|
||||
end
|
||||
|
||||
def update_tag_post_counts
|
||||
decrement_tags = tag_array_was - tag_array
|
||||
increment_tags = tag_array - tag_array_was
|
||||
Post.expire_cache_for_all(decrement_tags) if decrement_tags.any?
|
||||
Post.expire_cache_for_all(increment_tags) if increment_tags.any?
|
||||
if increment_tags.any?
|
||||
Tag.update_all("post_count = post_count + 1", {:name => increment_tags})
|
||||
Post.expire_cache_for_all(increment_tags) if increment_tags.any?
|
||||
end
|
||||
if decrement_tags.any?
|
||||
Tag.update_all("post_count = post_count - 1", {:name => decrement_tags})
|
||||
Post.expire_cache_for_all(decrement_tags) if decrement_tags.any?
|
||||
end
|
||||
Post.expire_cache_for_all([""]) if new_record? || id <= 100_000
|
||||
end
|
||||
|
||||
|
||||
@@ -690,6 +690,7 @@ class PostTest < ActiveSupport::TestCase
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
@post = FactoryGirl.create(:post)
|
||||
@post.add_favorite!(@user)
|
||||
@user.reload
|
||||
end
|
||||
|
||||
teardown do
|
||||
@@ -698,9 +699,9 @@ class PostTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "decrement the user's favorite_count" do
|
||||
assert_difference("CurrentUser.favorite_count", -1) do
|
||||
assert_difference("@user.favorite_count", -1) do
|
||||
@post.remove_favorite!(@user)
|
||||
CurrentUser.reload
|
||||
@user.reload
|
||||
end
|
||||
end
|
||||
|
||||
@@ -721,9 +722,9 @@ class PostTest < ActiveSupport::TestCase
|
||||
|
||||
should "not decrement the user's favorite_count if the user did not favorite the post" do
|
||||
@post2 = FactoryGirl.create(:post)
|
||||
assert_difference("CurrentUser.favorite_count", 0) do
|
||||
assert_difference("@user.favorite_count", 0) do
|
||||
@post2.remove_favorite!(@user)
|
||||
CurrentUser.reload
|
||||
@user.reload
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user