From a920b44e10c779547aec13d0ec0e13921f7c2d8d Mon Sep 17 00:00:00 2001 From: debbbbie Date: Mon, 3 Sep 2018 22:07:41 +0800 Subject: [PATCH] Update counter_cache even not reload --- lib/action_store/mixin.rb | 10 +++++----- test/lib/mixin_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/action_store/mixin.rb b/lib/action_store/mixin.rb index 37a8ad6..bf543e5 100644 --- a/lib/action_store/mixin.rb +++ b/lib/action_store/mixin.rb @@ -85,7 +85,7 @@ def create_action(action_type, opts) if opts[:action_option] action.update_attribute(:action_option, opts[:action_option]) end - reset_counter_cache(action, defined_action) + reset_counter_cache(action, defined_action, opts) true end @@ -101,11 +101,11 @@ def destroy_action(action_type, opts) action = Action.where(where_opts(opts)).first return true if !action action.destroy - reset_counter_cache(action, defined_action) + reset_counter_cache(action, defined_action, opts) true end - def reset_counter_cache(action, defined_action) + def reset_counter_cache(action, defined_action, opts) return false if action.blank? if defined_action[:counter_cache] && action.target.present? target_count = Action.where( @@ -113,7 +113,7 @@ def reset_counter_cache(action, defined_action) target_type: action.target_type, target_id: action.target_id ).count - action.target.update_attribute(defined_action[:counter_cache], target_count) + opts[:target].update_attribute(defined_action[:counter_cache], target_count) if opts[:target] end if defined_action[:user_counter_cache] && action.user.present? user_count = Action.where( @@ -122,7 +122,7 @@ def reset_counter_cache(action, defined_action) user_type: action.user_type, user_id: action.user_id ).count - action.user.update_attribute(defined_action[:user_counter_cache], user_count) + opts[:user].update_attribute(defined_action[:user_counter_cache], user_count) if opts[:user] end end diff --git a/test/lib/mixin_test.rb b/test/lib/mixin_test.rb index 9e71e5f..7396580 100644 --- a/test/lib/mixin_test.rb +++ b/test/lib/mixin_test.rb @@ -55,6 +55,7 @@ class ActionStore::MixinTest < ActiveSupport::TestCase assert_equal "User", a.user_type assert_equal post.user_id, a.user_id assert_equal 1, Action.where(action_type: "like", target_type: "Post").count + assert_equal 1, post.likes_count post.reload assert_equal 1, post.likes_count @@ -75,6 +76,7 @@ class ActionStore::MixinTest < ActiveSupport::TestCase assert_equal false, a1.new_record? assert_not_equal a.id, a1.id assert_equal 2, Action.where(action_type: "like", target: post).count + assert_equal 2, post.likes_count post.reload assert_equal 2, post.likes_count @@ -84,6 +86,7 @@ class ActionStore::MixinTest < ActiveSupport::TestCase assert_not_equal a.id, a2.id assert_not_equal a1.id, a2.id assert_equal 1, Action.where(action_type: "star", target: post).count + assert_equal 1, post.stars_count post.reload assert_equal 1, post.stars_count @@ -121,12 +124,19 @@ class ActionStore::MixinTest < ActiveSupport::TestCase assert_not_nil(action) User.create_action("follow", target: u2, user: u3) User.create_action("follow", target: u2, user: u4) + assert_equal(3, u2.followers_count) + assert_equal(1, u1.following_count) + assert_equal(1, u3.following_count) + assert_equal(1, u4.following_count) assert_equal(3, u2.reload.followers_count) assert_equal(1, u1.reload.following_count) assert_equal(1, u3.reload.following_count) assert_equal(1, u4.reload.following_count) User.destroy_action("follow", target: u2, user: u3) User.destroy_action("follow", target: u2, user: u4) + assert_equal(1, u2.followers_count) + assert_equal(0, u3.following_count) + assert_equal(0, u4.following_count) assert_equal(1, u2.reload.followers_count) assert_equal(0, u3.reload.following_count) assert_equal(0, u4.reload.following_count) @@ -134,6 +144,8 @@ class ActionStore::MixinTest < ActiveSupport::TestCase # u2 -> follow -> u1 action = User.create_action("follow", target: u1, user: u2) assert_not_nil(action) + assert_equal(1, u2.following_count) + assert_equal(1, u1.followers_count) assert_equal(1, u2.reload.following_count) assert_equal(1, u1.reload.followers_count)