counter_culture 3.3.1 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce78ea0f9a3ad552e5cb8b0e230dbc70fc3fcb51f9f0c21676c3ecca7601569a
4
- data.tar.gz: 840001761832861fbbef2e3d921d553d0ea39c685a8ab16b4096444722bb5d03
3
+ metadata.gz: acebc4b3981511ea35497a3f276372253cb709314002ab2c6c498fb9b60a0166
4
+ data.tar.gz: b659b8956b8e6911c58563e4f921487f49a3ddf8ada5fbee7b79b0de16f8796d
5
5
  SHA512:
6
- metadata.gz: 41e59967d03892c1d0ce48d1c804862bcd0ffb575df9675869fc94fcba13440f8eaf07f4a06b6b5b88a168c79efd06af4a4b88634f8bdc9d8ca0b5c990b0c81f
7
- data.tar.gz: 1e90fa445eb4972dea2c7ce851719e8fa0a095aa2526b177fd1d2ec5ae9de8d85f4bf9d1b3cc1a112a1186f76add7b7d564efab9bb146085fdf1c1011cda5ebf
6
+ metadata.gz: 9ef804972ebbd99b5945b22d87c85d93dcd3af1ea78f04683a0799b67d25d42cb8c4ca52e08b9267fbd17f11e6fcd07a334e5990cccd5e4fd3ea27991da67fb4
7
+ data.tar.gz: 81fa1adc6e283a1fed1a661e08cc7bd4a5280062cb8567ef730ce59342b0cf49032d09a9fc05bcbff713fbbd37d9a7157f2de35bc25df696d48f5de5bed9281c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 3.5.0 (August 25, 2023)
2
+
3
+ Improvements:
4
+ - Allow passing context to `counter_culture_fix_counts` (#375)
5
+
6
+ ## 3.4.0 (July 12, 2023)
7
+
8
+ Improvements:
9
+ - Ability to skip counter culture updates in a block (#371)
10
+
1
11
  ## 3.3.1 (June 26, 2023)
2
12
 
3
13
  Bugfixes:
data/README.md CHANGED
@@ -190,6 +190,18 @@ Now, the ```Category``` model will keep the counter cache in ```special_count```
190
190
 
191
191
  If you would like to use this with `counter_culture_fix_counts`, make sure to also provide [the `column_names` configuration](#handling-dynamic-column-names).
192
192
 
193
+ ### Temporarily skipping counter cache updates
194
+
195
+ If you would like to temporarily pause counter_culture, for example in a backfill script, you can do so as follows:
196
+
197
+ ```ruby
198
+ Review.skip_counter_culture_updates do
199
+ user.reviews.create!
200
+ end
201
+
202
+ user.reviews_count # => unchanged
203
+ ```
204
+
193
205
  ### Totaling instead of counting
194
206
 
195
207
  Instead of keeping a running count, you may want to automatically track a running total.
@@ -255,7 +267,7 @@ With this option, any time the `category_counter_cache` changes both the `catego
255
267
 
256
268
  Some applications run into issues with deadlocks involving counter cache updates when using this gem. See [#263](https://github.com/magnusvk/counter_culture/issues/263#issuecomment-772284439) for information and helpful links on how to avoid this issue.
257
269
 
258
- Another option is to simply defer the update of counter caches to outside of the transaction. This gives up transacrtional guarantees for your counter cache updates but should resolve any deadlocks you experience. This behavior is disabled by default, enable it on each affected counter cache as follows:
270
+ Another option is to simply defer the update of counter caches to outside of the transaction. This gives up transactional guarantees for your counter cache updates but should resolve any deadlocks you experience. This behavior is disabled by default, enable it on each affected counter cache as follows:
259
271
 
260
272
  ```ruby
261
273
  counter_culture :category, execute_after_commit: true
@@ -419,6 +431,23 @@ dynamic, you can pass `skip_unsupported`:
419
431
  Product.counter_culture_fix_counts skip_unsupported: true
420
432
  ```
421
433
 
434
+ You can also use context within the block that was provided with the `column_names` method:
435
+
436
+ ```ruby
437
+ class Product < ActiveRecord::Base
438
+ belongs_to :category
439
+ scope :awesomes, -> (ids) { where(ids: ids, product_type: 'awesome') }
440
+
441
+ counter_culture :category,
442
+ column_name: 'awesome_count'
443
+ column_names: -> (context) {
444
+ { Product.awesomes(context[:ids]) => :awesome_count }
445
+ }
446
+ end
447
+
448
+ Product.counter_culture_fix_counts(context: { ids: [1, 2] })
449
+ ```
450
+
422
451
  #### Handling over-written, dynamic foreign keys
423
452
 
424
453
  Manually populating counter caches with dynamically over-written foreign keys (```:foreign_key_values``` option) is not supported. You will have to write code to handle this case yourself.
@@ -84,13 +84,23 @@ module CounterCulture
84
84
  next if options[:exclude] && options[:exclude].include?(counter.relation)
85
85
  next if options[:only] && !options[:only].include?(counter.relation)
86
86
 
87
- reconciler_options = %i(batch_size column_name db_connection_builder finish skip_unsupported start touch verbose where polymorphic_classes)
87
+ reconciler_options = %i(context batch_size column_name db_connection_builder finish skip_unsupported start touch verbose where polymorphic_classes)
88
88
 
89
89
  reconciler = CounterCulture::Reconciler.new(counter, options.slice(*reconciler_options))
90
90
  reconciler.reconcile!
91
91
  reconciler.changes
92
92
  end.compact
93
93
  end
94
+
95
+ def skip_counter_culture_updates
96
+ return unless block_given?
97
+
98
+ counter_culture_updates_was = Thread.current[:skip_counter_culture_updates]
99
+ Thread.current[:skip_counter_culture_updates] = Array(counter_culture_updates_was) + [self]
100
+ yield
101
+ ensure
102
+ Thread.current[:skip_counter_culture_updates] = counter_culture_updates_was
103
+ end
94
104
  end
95
105
 
96
106
  private
@@ -77,7 +77,11 @@ module CounterCulture
77
77
  counter_column_names =
78
78
  case column_names
79
79
  when Proc
80
- column_names.call
80
+ if column_names.lambda? && column_names.arity == 0
81
+ column_names.call
82
+ else
83
+ column_names.call(options.fetch(:context, {}))
84
+ end
81
85
  when Hash
82
86
  column_names
83
87
  else
@@ -0,0 +1,26 @@
1
+ module CounterCulture
2
+ module SkipUpdates
3
+ private
4
+
5
+ # called by after_create callback
6
+ def _update_counts_after_create
7
+ unless Array(Thread.current[:skip_counter_culture_updates]).include?(self.class)
8
+ super
9
+ end
10
+ end
11
+
12
+ # called by after_destroy callback
13
+ def _update_counts_after_destroy
14
+ unless Array(Thread.current[:skip_counter_culture_updates]).include?(self.class)
15
+ super
16
+ end
17
+ end
18
+
19
+ # called by after_update callback
20
+ def _update_counts_after_update
21
+ unless Array(Thread.current[:skip_counter_culture_updates]).include?(self.class)
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module CounterCulture
2
- VERSION = '3.3.1'.freeze
2
+ VERSION = '3.5.0'.freeze
3
3
  end
@@ -5,6 +5,7 @@ require 'counter_culture/version'
5
5
  require 'counter_culture/extensions'
6
6
  require 'counter_culture/counter'
7
7
  require 'counter_culture/reconciler'
8
+ require 'counter_culture/skip_updates'
8
9
 
9
10
  module CounterCulture
10
11
  mattr_accessor :batch_size
@@ -19,4 +20,5 @@ end
19
20
  # extend ActiveRecord with our own code here
20
21
  ActiveSupport.on_load(:active_record) do
21
22
  include CounterCulture::Extensions
23
+ include CounterCulture::SkipUpdates
22
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_culture
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.1
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus von Koeller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-26 00:00:00.000000000 Z
11
+ date: 2023-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -321,6 +321,7 @@ files:
321
321
  - lib/counter_culture/counter.rb
322
322
  - lib/counter_culture/extensions.rb
323
323
  - lib/counter_culture/reconciler.rb
324
+ - lib/counter_culture/skip_updates.rb
324
325
  - lib/counter_culture/version.rb
325
326
  - lib/generators/counter_culture_generator.rb
326
327
  - lib/generators/templates/counter_culture_migration.rb.erb
@@ -343,7 +344,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
343
344
  - !ruby/object:Gem::Version
344
345
  version: '0'
345
346
  requirements: []
346
- rubygems_version: 3.4.2
347
+ rubygems_version: 3.4.19
347
348
  signing_key:
348
349
  specification_version: 4
349
350
  summary: Turbo-charged counter caches for your Rails app.