custom_counter_cache 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 7714477f7fd065e02a9f87251350719498f140c9
4
- data.tar.gz: e27ba6a73b69704e213665681f1139e32cc8705b
3
+ metadata.gz: 51f38aaeec8fe7a42868c8d1bb9da6335ad58232
4
+ data.tar.gz: 1409e90333ee408e02c893d744e968e1ea56a919
5
5
  SHA512:
6
- metadata.gz: 32847b6bb5c6deb88c485b2c6d3120192a4244087f9f1e24d6242373b5e23e4ebdf421d7fd76549ca4a62be50ff56a186aa2b0f2b242dcc974d2c007d3c0f7d2
7
- data.tar.gz: 5f3776cb002ab64a1c19272035d827cb3abd5a443d1cd71b6af30c2ee79f140ea61255936304a8e2bea3aa7c4a8b7961365fba28ae5801359dbba4b08a6ff0c5
6
+ metadata.gz: 903199c537808240f5d1fba9057277970bb6b0c0fdd8685690d9f16cbc23d1003d383ffcdd27f911ada7c720dfc038798e728641fcb2aa2ac2dc762450c9193e
7
+ data.tar.gz: 7a19e2dd0d91060ab41ae8b98dfefcec4d2aa0ac7d01c019b26d2ccc4c5b1af48ede587c00e4fb8be724b945a178d9d5c2d8af97bfe4557def51a134753ec598
@@ -70,10 +70,15 @@ module CustomCounterCache::Model
70
70
  end
71
71
  end
72
72
 
73
+ skip_callback = Proc.new { |callback, opts|
74
+ (opts[:except].present? && opts[:except].include?(callback)) ||
75
+ (opts[:only].present? && !opts[:only].include?(callback))
76
+ }
77
+
73
78
  # set callbacks
74
- after_create method_name, options
75
- after_update method_name, options
76
- after_destroy method_name, options
79
+ after_create method_name, options unless skip_callback.call(:create, options)
80
+ after_update method_name, options unless skip_callback.call(:update, options)
81
+ after_destroy method_name, options unless skip_callback.call(:destroy, options)
77
82
 
78
83
  rescue StandardError => e
79
84
  # Support Heroku's database-less assets:precompile pre-deploy step:
@@ -1,5 +1,5 @@
1
1
  module CustomCounterCache
2
2
 
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
 
5
5
  end
data/test/counter_test.rb CHANGED
@@ -77,4 +77,22 @@ class CounterTest < MiniTest::Unit::TestCase
77
77
  assert_equal 1, @user.published_count
78
78
  end
79
79
 
80
+ def test_except_option
81
+ @ball = @box.balls.create
82
+ assert_equal 1, @box.reload.lifetime_balls_count
83
+ @ball.update(color: 'green')
84
+ assert_equal 1, @box.reload.lifetime_balls_count
85
+ @ball.destroy
86
+ assert_equal 1, @box.reload.lifetime_balls_count
87
+ end
88
+
89
+ def test_only_option
90
+ @ball = @box.balls.create
91
+ assert_equal 0, @box.reload.destroyed_balls_count
92
+ @ball.update(color: 'green')
93
+ assert_equal 0, @box.reload.destroyed_balls_count
94
+ @ball.destroy
95
+ assert_equal 1, @box.reload.destroyed_balls_count
96
+ end
97
+
80
98
  end
data/test/test_helper.rb CHANGED
@@ -31,6 +31,8 @@ ActiveRecord::Schema.define(version: 1) do
31
31
 
32
32
  create_table :boxes do |t|
33
33
  t.integer :green_balls_count, default: 0
34
+ t.integer :lifetime_balls_count, default: 0
35
+ t.integer :destroyed_balls_count, default: 0
34
36
  end
35
37
 
36
38
  create_table :balls do |t|
@@ -74,10 +76,18 @@ class Box < ApplicationRecord
74
76
  define_counter_cache :green_balls_count do |box|
75
77
  box.balls.green.count
76
78
  end
79
+ define_counter_cache :lifetime_balls_count do |box|
80
+ box.lifetime_balls_count + 1
81
+ end
82
+ define_counter_cache :destroyed_balls_count do |box|
83
+ box.destroyed_balls_count + 1
84
+ end
77
85
  end
78
86
 
79
87
  class Ball < ApplicationRecord
80
88
  belongs_to :box
81
89
  scope :green, lambda { where(color: 'green') }
82
90
  update_counter_cache :box, :green_balls_count, if: Proc.new { |ball| ball.color_changed? }
91
+ update_counter_cache :box, :lifetime_balls_count, except: [:update, :destroy]
92
+ update_counter_cache :box, :destroyed_balls_count, only: [:destroy]
83
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.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cedric Howe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-16 00:00:00.000000000 Z
11
+ date: 2017-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -75,5 +75,5 @@ specification_version: 4
75
75
  summary: Custom counter_cache functionality that supports conditions and multiple
76
76
  models.
77
77
  test_files:
78
- - test/counter_test.rb
79
78
  - test/test_helper.rb
79
+ - test/counter_test.rb