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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccba1337c291259711f6a18d6604a72c9be6a8a858342b345dab51f434d05816
4
- data.tar.gz: 97d0e479eb6900a090449243a326a5a3572597219bb119af248dade70adc188b
3
+ metadata.gz: e36d2910a85a7b6354c086adad83820f1503947aba48f3caeb3c09b424b565d5
4
+ data.tar.gz: 6e8892f7c9a9d447117ff9ae0c7c23f16afc00739e93e4b1fd407631989fa0eb
5
5
  SHA512:
6
- metadata.gz: 6d342ee7e3857b846c66f3183ce2010c0f5c9fc9719060c3d542669fd66ca0266dd2caa77b972013ed01b516bf48799b62ffe32188124900ce8ae74bdb0ef2e9
7
- data.tar.gz: c5b2d8ee7b87e16b04cd62c85d358787126112c8781775a9648aec8f31f5d7f808c363a282a27d6713ec4f6bd452f9a4c4f69998980df95befe84f745d1c3c34
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.find_by_key(cache_column.to_s).try(:value).to_i
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.find_by_key(cache_column.to_s) )
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
- target_changed = rails_5_1_or_newer ? send("saved_change_to_#{target_key}?") : send("#{target_key}_changed?")
59
- if target_changed
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
- after_create method_name, options unless skip_callback.call(:create, options)
83
- after_update method_name, options unless skip_callback.call(:update, options)
84
- after_destroy method_name, options unless skip_callback.call(:destroy, options)
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:
@@ -1,5 +1,5 @@
1
1
  module CustomCounterCache
2
2
 
3
- VERSION = '0.2.8'
3
+ VERSION = '0.3.0'
4
4
 
5
5
  end
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 < MiniTest::Unit::TestCase
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.update_attributes(state: 'unpublished') }
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.update_attributes(state: "unpublished") }
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.update_attributes(color: 'red') }
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.saved_change_to_attribute_compat?(:state) }
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.saved_change_to_attribute_compat?(:state) }
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.saved_change_to_attribute_compat?(:color) }
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.2.8
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: 2020-03-02 00:00:00.000000000 Z
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: '4.0'
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: '4.0'
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: 1.4.0
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: 1.4.0
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.0.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