custom_counter_cache 0.2.8 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/custom_counter_cache/model.rb +8 -9
- data/lib/custom_counter_cache/version.rb +1 -1
- data/test/counter_test.rb +16 -4
- data/test/test_helper.rb +3 -12
- metadata +20 -20
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e36d2910a85a7b6354c086adad83820f1503947aba48f3caeb3c09b424b565d5
|
|
4
|
+
data.tar.gz: 6e8892f7c9a9d447117ff9ae0c7c23f16afc00739e93e4b1fd407631989fa0eb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7986c0f65fd6c43a38a71f4acfd6d62a7bd36e9aeeb03dbe613dff4553d1c0119cd88b48a1c6d19adffd06e4cfd214462202fc8aafe84e8e05d2034288219750
|
|
7
|
+
data.tar.gz: f52a1259232d2ae16a2117ea42f0950a41f81379892387c7f4191ec595918750b87b4f9b482223faaac03d19c50f459cd4af610523c89428bd38635c25ac0f41
|
|
@@ -15,11 +15,11 @@ module CustomCounterCache::Model
|
|
|
15
15
|
if counters.loaded? && counter = counters.detect{|c| c.key == cache_column.to_s }
|
|
16
16
|
counter.value
|
|
17
17
|
else
|
|
18
|
-
counters.
|
|
18
|
+
counters.find_by(key: cache_column.to_s).try(:value).to_i
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
define_method "#{cache_column}=" do |count|
|
|
22
|
-
if ( counter = counters.
|
|
22
|
+
if ( counter = counters.find_by(key: cache_column.to_s) )
|
|
23
23
|
counter.update_attribute :value, count.to_i
|
|
24
24
|
else
|
|
25
25
|
counters.create key: cache_column.to_s, value: count.to_i
|
|
@@ -53,11 +53,9 @@ module CustomCounterCache::Model
|
|
|
53
53
|
# define callback
|
|
54
54
|
define_method method_name do
|
|
55
55
|
# update old association
|
|
56
|
-
rails_5_1_or_newer = ActiveModel.version >= Gem::Version.new('5.1.0')
|
|
57
56
|
target_key = reflection.options[:polymorphic] ? "#{association}_type" : foreign_key
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
old_id = rails_5_1_or_newer ? send("#{target_key}_before_last_save") : send("#{target_key}_was")
|
|
57
|
+
if send("saved_change_to_#{target_key}?")
|
|
58
|
+
old_id = send("#{target_key}_before_last_save")
|
|
61
59
|
klass = if reflection.options[:polymorphic]
|
|
62
60
|
( old_id || send("#{association}_type") ).constantize
|
|
63
61
|
else
|
|
@@ -79,9 +77,10 @@ module CustomCounterCache::Model
|
|
|
79
77
|
}
|
|
80
78
|
|
|
81
79
|
# set callbacks
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
callback_opts = options.slice(:if, :unless, :prepend)
|
|
81
|
+
after_create method_name, **callback_opts unless skip_callback.call(:create, options)
|
|
82
|
+
after_update method_name, **callback_opts unless skip_callback.call(:update, options)
|
|
83
|
+
after_destroy method_name, **callback_opts unless skip_callback.call(:destroy, options)
|
|
85
84
|
|
|
86
85
|
rescue StandardError => e
|
|
87
86
|
# Support Heroku's database-less assets:precompile pre-deploy step:
|
data/test/counter_test.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper')
|
|
2
2
|
|
|
3
|
-
class CounterTest <
|
|
3
|
+
class CounterTest < Minitest::Test
|
|
4
4
|
|
|
5
5
|
def setup
|
|
6
6
|
@user = User.create
|
|
@@ -36,7 +36,7 @@ class CounterTest < MiniTest::Unit::TestCase
|
|
|
36
36
|
assert_equal 1, @user.published_count
|
|
37
37
|
3.times { |i| @user.articles.create(state: 'published') }
|
|
38
38
|
assert_equal 4, @user.published_count
|
|
39
|
-
@user.articles.each {|a| a.
|
|
39
|
+
@user.articles.each {|a| a.update(state: 'unpublished') }
|
|
40
40
|
assert_equal 0, @user.published_count
|
|
41
41
|
end
|
|
42
42
|
|
|
@@ -48,7 +48,7 @@ class CounterTest < MiniTest::Unit::TestCase
|
|
|
48
48
|
assert_equal 1, @article.comments_count
|
|
49
49
|
3.times { |i| @article.comments.create(state: "published") }
|
|
50
50
|
assert_equal 4, @article.comments_count
|
|
51
|
-
@article.comments.each { |c| c.
|
|
51
|
+
@article.comments.each { |c| c.update(state: 'unpublished') }
|
|
52
52
|
assert_equal 0, @article.comments_count
|
|
53
53
|
end
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ class CounterTest < MiniTest::Unit::TestCase
|
|
|
59
59
|
assert_equal 1, @box.reload.green_balls_count
|
|
60
60
|
3.times { |i| @box.balls.create(color: 'green') }
|
|
61
61
|
assert_equal 4, @box.reload.green_balls_count
|
|
62
|
-
@box.balls.each {|b| b.
|
|
62
|
+
@box.balls.each {|b| b.update(color: 'red') }
|
|
63
63
|
assert_equal 0, @box.reload.green_balls_count
|
|
64
64
|
end
|
|
65
65
|
|
|
@@ -95,4 +95,16 @@ class CounterTest < MiniTest::Unit::TestCase
|
|
|
95
95
|
assert_equal 1, @box.reload.destroyed_balls_count
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
+
def test_reassigning_article_to_different_user_updates_both_counters
|
|
99
|
+
@user2 = User.create
|
|
100
|
+
@article = @user.articles.create(state: 'unpublished')
|
|
101
|
+
assert_equal 0, @user.reload.published_count
|
|
102
|
+
assert_equal 0, @user2.reload.published_count
|
|
103
|
+
# Changing both user and state triggers the :if condition (state changed)
|
|
104
|
+
# and the reassignment logic inside the callback updates the old user's counter too
|
|
105
|
+
@article.update(user: @user2, state: 'published')
|
|
106
|
+
assert_equal 0, @user.reload.published_count
|
|
107
|
+
assert_equal 1, @user2.reload.published_count
|
|
108
|
+
end
|
|
109
|
+
|
|
98
110
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -44,15 +44,6 @@ end
|
|
|
44
44
|
class ApplicationRecord < ActiveRecord::Base
|
|
45
45
|
self.abstract_class = true
|
|
46
46
|
include CustomCounterCache::Model
|
|
47
|
-
@@rails_5_1_or_newer = ActiveModel.version >= Gem::Version.new('5.1.0')
|
|
48
|
-
|
|
49
|
-
def saved_change_to_attribute_compat?(attr)
|
|
50
|
-
if @@rails_5_1_or_newer
|
|
51
|
-
saved_change_to_attribute?(attr)
|
|
52
|
-
else
|
|
53
|
-
attribute_changed?(attr)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
47
|
end
|
|
57
48
|
|
|
58
49
|
class User < ApplicationRecord
|
|
@@ -64,7 +55,7 @@ end
|
|
|
64
55
|
|
|
65
56
|
class Article < ApplicationRecord
|
|
66
57
|
belongs_to :user
|
|
67
|
-
update_counter_cache :user, :published_count, if: Proc.new { |article| article.
|
|
58
|
+
update_counter_cache :user, :published_count, if: Proc.new { |article| article.saved_change_to_attribute?(:state) }
|
|
68
59
|
has_many :comments, as: :commentable, dependent: :destroy
|
|
69
60
|
define_counter_cache :comments_count do |article|
|
|
70
61
|
article.comments.where(state: "published").count
|
|
@@ -73,7 +64,7 @@ end
|
|
|
73
64
|
|
|
74
65
|
class Comment < ApplicationRecord
|
|
75
66
|
belongs_to :commentable, polymorphic: true
|
|
76
|
-
update_counter_cache :commentable, :comments_count, if: Proc.new { |comment| comment.
|
|
67
|
+
update_counter_cache :commentable, :comments_count, if: Proc.new { |comment| comment.saved_change_to_attribute?(:state) }
|
|
77
68
|
end
|
|
78
69
|
|
|
79
70
|
class Counter < ApplicationRecord
|
|
@@ -96,7 +87,7 @@ end
|
|
|
96
87
|
class Ball < ApplicationRecord
|
|
97
88
|
belongs_to :box
|
|
98
89
|
scope :green, lambda { where(color: 'green') }
|
|
99
|
-
update_counter_cache :box, :green_balls_count, if: Proc.new { |ball| ball.
|
|
90
|
+
update_counter_cache :box, :green_balls_count, if: Proc.new { |ball| ball.saved_change_to_attribute?(:color) }
|
|
100
91
|
update_counter_cache :box, :lifetime_balls_count, except: [:update, :destroy]
|
|
101
92
|
update_counter_cache :box, :destroyed_balls_count, only: [:destroy]
|
|
102
93
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: custom_counter_cache
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Cedric Howe
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-04-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -16,34 +16,34 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '7.2'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '9.0'
|
|
20
23
|
type: :runtime
|
|
21
24
|
prerelease: false
|
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
26
|
requirements:
|
|
24
27
|
- - ">="
|
|
25
28
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
29
|
+
version: '7.2'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '9.0'
|
|
27
33
|
- !ruby/object:Gem::Dependency
|
|
28
34
|
name: sqlite3
|
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
|
30
36
|
requirements:
|
|
31
|
-
- - "
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: 1.3.3
|
|
34
|
-
- - "<"
|
|
37
|
+
- - "~>"
|
|
35
38
|
- !ruby/object:Gem::Version
|
|
36
|
-
version:
|
|
39
|
+
version: '2.0'
|
|
37
40
|
type: :development
|
|
38
41
|
prerelease: false
|
|
39
42
|
version_requirements: !ruby/object:Gem::Requirement
|
|
40
43
|
requirements:
|
|
41
|
-
- - "
|
|
42
|
-
- !ruby/object:Gem::Version
|
|
43
|
-
version: 1.3.3
|
|
44
|
-
- - "<"
|
|
44
|
+
- - "~>"
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version:
|
|
46
|
+
version: '2.0'
|
|
47
47
|
description: ''
|
|
48
48
|
email: cedric@howe.net
|
|
49
49
|
executables: []
|
|
@@ -59,7 +59,7 @@ homepage: http://github.com/cedric/custom_counter_cache/
|
|
|
59
59
|
licenses:
|
|
60
60
|
- MIT
|
|
61
61
|
metadata: {}
|
|
62
|
-
post_install_message:
|
|
62
|
+
post_install_message:
|
|
63
63
|
rdoc_options: []
|
|
64
64
|
require_paths:
|
|
65
65
|
- lib
|
|
@@ -67,18 +67,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
67
67
|
requirements:
|
|
68
68
|
- - ">="
|
|
69
69
|
- !ruby/object:Gem::Version
|
|
70
|
-
version: '0'
|
|
70
|
+
version: '3.0'
|
|
71
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
requirements:
|
|
73
73
|
- - ">="
|
|
74
74
|
- !ruby/object:Gem::Version
|
|
75
75
|
version: 1.3.6
|
|
76
76
|
requirements: []
|
|
77
|
-
rubygems_version: 3.
|
|
78
|
-
signing_key:
|
|
77
|
+
rubygems_version: 3.5.22
|
|
78
|
+
signing_key:
|
|
79
79
|
specification_version: 4
|
|
80
80
|
summary: Custom counter_cache functionality that supports conditions and multiple
|
|
81
81
|
models.
|
|
82
82
|
test_files:
|
|
83
|
-
- test/test_helper.rb
|
|
84
83
|
- test/counter_test.rb
|
|
84
|
+
- test/test_helper.rb
|