Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update counter_cache even not reload #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/action_store/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -101,19 +101,19 @@ 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(
action_type: defined_action[:action_type],
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 target 有可能没有,有些时候传递的是 target_type 和 target_id

所以得用 action.target 稳妥些


看起来是 action_store 更新了数据,但内存中的 target 数据没变,得想其他方法来解决


抱歉,现在在看到这个 PR

end
if defined_action[:user_counter_cache] && action.user.present?
user_count = Action.where(
Expand All @@ -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

Expand Down
12 changes: 12 additions & 0 deletions test/lib/mixin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -121,19 +124,28 @@ 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)

# 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)

Expand Down