counter_culture 3.4.0 → 3.5.1

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: 749d2a26a921731dd342cddd6d9e479776b26eb3ccd836fb703e47bca4c05b22
4
- data.tar.gz: f59ff08373a8831c2f96605bf75063b5d50d6f1adecfa0486a53d41a3c9a8b9b
3
+ metadata.gz: 6a69421ca46fbb8c213e7babacde9bb2d9fc365a956929855574f07b2f9d8d77
4
+ data.tar.gz: 67f26aea01f3b1647da2266c492ca0241fb17615f9a2d6af55e55f8a01b4fe11
5
5
  SHA512:
6
- metadata.gz: 3a57f1f22ce98c08ccf76552a040445bbcf37f53966bf3abde13f2be2fcb9c4abbf3ea13f8e911ffd536122f161e04cb0a6b62b0a1330435c2568bd8d06c39ac
7
- data.tar.gz: f29b2abad46f30de69df23e0e02fa0bcb75447fd2dd854cc42dc979efbe9955316984285ae68bfd3033e4e0d8a2d31bc06699a667b2c375306e654ac2eaff8f7
6
+ metadata.gz: bd230e2a56efec0b5154a3568f00470829a123f36419e1297cb9f715be91755e30cbcc06bc0296c476d9e11df3b491adeff88b03631348be45b6f71b6f0f9ad9
7
+ data.tar.gz: b43aec06f3bc6d12f92d02ee7501ad254e5e2138776bc688b24bea2b0dd3fe68aed3dc5018158bc02ba0e1cacfee57b83ac1cf33349c4ef6fd5f664d08855922
data/.circleci/config.yml CHANGED
@@ -48,8 +48,8 @@ workflows:
48
48
  - test:
49
49
  matrix:
50
50
  parameters:
51
- ruby-version: ["2.6", "2.7", "3.0", "3.1", "3.2"]
52
- rails-version: ["5.2", "6.0", "6.1", "7.0"]
51
+ ruby-version: ["2.6", "2.7", "3.0", "3.1", "3.2", "3.3"]
52
+ rails-version: ["5.2", "6.0", "6.1", "7.0", "7.1"]
53
53
  database: ["postgresql", "sqlite3", "mysql2"]
54
54
  exclude:
55
55
  - ruby-version: "3.0"
@@ -57,28 +57,37 @@ workflows:
57
57
  database: "postgresql"
58
58
  - ruby-version: "3.0"
59
59
  rails-version: "5.2"
60
- database: "sqlite3"
60
+ database: "mysql2"
61
61
  - ruby-version: "3.0"
62
62
  rails-version: "5.2"
63
- database: "mysql2"
63
+ database: "sqlite3"
64
64
  - ruby-version: "3.1"
65
65
  rails-version: "5.2"
66
66
  database: "postgresql"
67
+ - ruby-version: "3.1"
68
+ rails-version: "5.2"
69
+ database: "mysql2"
67
70
  - ruby-version: "3.1"
68
71
  rails-version: "5.2"
69
72
  database: "sqlite3"
70
73
  - ruby-version: "3.2"
71
74
  rails-version: "5.2"
72
- database: "mysql2"
75
+ database: "postgresql"
73
76
  - ruby-version: "3.2"
74
77
  rails-version: "5.2"
75
- database: "postgresql"
78
+ database: "mysql2"
76
79
  - ruby-version: "3.2"
77
80
  rails-version: "5.2"
78
81
  database: "sqlite3"
79
- - ruby-version: "3.1"
82
+ - ruby-version: "3.3"
83
+ rails-version: "5.2"
84
+ database: "postgresql"
85
+ - ruby-version: "3.3"
80
86
  rails-version: "5.2"
81
87
  database: "mysql2"
88
+ - ruby-version: "3.3"
89
+ rails-version: "5.2"
90
+ database: "sqlite3"
82
91
  - ruby-version: "2.6"
83
92
  rails-version: "7.0"
84
93
  database: "postgresql"
@@ -88,3 +97,12 @@ workflows:
88
97
  - ruby-version: "2.6"
89
98
  rails-version: "7.0"
90
99
  database: "mysql2"
100
+ - ruby-version: "2.6"
101
+ rails-version: "7.1"
102
+ database: "postgresql"
103
+ - ruby-version: "2.6"
104
+ rails-version: "7.1"
105
+ database: "sqlite3"
106
+ - ruby-version: "2.6"
107
+ rails-version: "7.1"
108
+ database: "mysql2"
data/Appraisals CHANGED
@@ -3,6 +3,7 @@
3
3
  6.0
4
4
  6.1
5
5
  7.0
6
+ 7.1
6
7
  ].each do |rails_version|
7
8
  appraise "rails-#{rails_version}" do
8
9
  gem 'rails', "~> #{rails_version}.0"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 3.5.1 (January 8, 2024)
2
+
3
+ Bugfixes:
4
+ - Fix touching for counted models without `updated_at` (#383)
5
+
6
+ ## 3.5.0 (August 25, 2023)
7
+
8
+ Improvements:
9
+ - Allow passing context to `counter_culture_fix_counts` (#375)
10
+
1
11
  ## 3.4.0 (July 12, 2023)
2
12
 
3
13
  Improvements:
data/README.md CHANGED
@@ -74,7 +74,7 @@ class Group < ActiveRecord::Base
74
74
  has_many :members, through: :group_memberships, class: "User"
75
75
  end
76
76
 
77
- class Membership < ActiveRecord::Base
77
+ class GroupMembership < ActiveRecord::Base
78
78
  belongs_to :group
79
79
  belongs_to :member, class: "User"
80
80
  counter_culture :group, column_name: "members_count"
@@ -267,7 +267,7 @@ With this option, any time the `category_counter_cache` changes both the `catego
267
267
 
268
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.
269
269
 
270
- 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:
271
271
 
272
272
  ```ruby
273
273
  counter_culture :category, execute_after_commit: true
@@ -358,6 +358,8 @@ Product.counter_culture_fix_counts touch: 'category_count_changed'
358
358
 
359
359
  The options start and finish are especially useful if you want multiple workers dealing with the same processing queue. You can make worker 1 handle all the records between id 1 and 9999 and worker 2 handle from 10000 and beyond by setting the :start and :finish option on each worker.
360
360
 
361
+ > **! NOTE**: the IDs we pass as `start` and `finish` here are in fact `Category` IDs, not `Product`!
362
+
361
363
  ```ruby
362
364
  Product.counter_culture_fix_counts start: 10_000
363
365
  # will fix counts for all counter caches defined on Product from record 10000 and onwards.
@@ -374,7 +376,7 @@ Product.counter_culture_fix_counts start: 2001, finish: 3000
374
376
 
375
377
  #### Fix counter cache using a replica database
376
378
 
377
- When fixing counter caches the number of reads usually vastly exceeds the number of writes. It can make sense to offload the road load to a replica database in this case. Rails 6 introduced [native handling of multiple database connections](https://guides.rubyonrails.org/v6.0/active_record_multiple_databases.html). You can use this to send read traffic to a read-only repliace using the option `db_connection_builder`:
379
+ When fixing counter caches the number of reads usually vastly exceeds the number of writes. It can make sense to offload the read load to a replica database in this case. Rails 6 introduced [native handling of multiple database connections](https://guides.rubyonrails.org/v6.0/active_record_multiple_databases.html). You can use this to send read traffic to a read-only replica using the option `db_connection_builder`:
378
380
 
379
381
  ```ruby
380
382
  Product.counter_culture_fix_counts db_connection_builder: proc{|reading, block|
@@ -431,6 +433,23 @@ dynamic, you can pass `skip_unsupported`:
431
433
  Product.counter_culture_fix_counts skip_unsupported: true
432
434
  ```
433
435
 
436
+ You can also use context within the block that was provided with the `column_names` method:
437
+
438
+ ```ruby
439
+ class Product < ActiveRecord::Base
440
+ belongs_to :category
441
+ scope :awesomes, -> (ids) { where(ids: ids, product_type: 'awesome') }
442
+
443
+ counter_culture :category,
444
+ column_name: 'awesome_count'
445
+ column_names: -> (context) {
446
+ { Product.awesomes(context[:ids]) => :awesome_count }
447
+ }
448
+ end
449
+
450
+ Product.counter_culture_fix_counts(context: { ids: [1, 2] })
451
+ ```
452
+
434
453
  #### Handling over-written, dynamic foreign keys
435
454
 
436
455
  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.
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 7.1.0"
6
+
7
+ gemspec path: "../"
@@ -81,8 +81,8 @@ module CounterCulture
81
81
  end
82
82
  # and here we update the timestamp, if so desired
83
83
  if touch
84
- current_time = obj.send(:current_time_from_proper_timezone)
85
- timestamp_columns = obj.send(:timestamp_attributes_for_update_in_model)
84
+ current_time = klass.send(:current_time_from_proper_timezone)
85
+ timestamp_columns = klass.send(:timestamp_attributes_for_update_in_model)
86
86
  if touch != true
87
87
  # starting in Rails 6 this is frozen
88
88
  timestamp_columns = timestamp_columns.dup
@@ -84,7 +84,7 @@ 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!
@@ -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
@@ -1,3 +1,3 @@
1
1
  module CounterCulture
2
- VERSION = '3.4.0'.freeze
2
+ VERSION = '3.5.1'.freeze
3
3
  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.4.0
4
+ version: 3.5.1
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-07-13 00:00:00.000000000 Z
11
+ date: 2024-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -317,6 +317,7 @@ files:
317
317
  - gemfiles/rails_6.0.gemfile
318
318
  - gemfiles/rails_6.1.gemfile
319
319
  - gemfiles/rails_7.0.gemfile
320
+ - gemfiles/rails_7.1.gemfile
320
321
  - lib/counter_culture.rb
321
322
  - lib/counter_culture/counter.rb
322
323
  - lib/counter_culture/extensions.rb
@@ -344,7 +345,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
344
345
  - !ruby/object:Gem::Version
345
346
  version: '0'
346
347
  requirements: []
347
- rubygems_version: 3.4.10
348
+ rubygems_version: 3.4.19
348
349
  signing_key:
349
350
  specification_version: 4
350
351
  summary: Turbo-charged counter caches for your Rails app.