counter_culture 3.9.0 → 3.10.1

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: f93d3d3e415f5d92e3870caf67067cdb75a6a76f68542b82c9ec2411987a0e4d
4
- data.tar.gz: 89cf5171b549aab368a1dd87117afd429bf557d93f198e220fbe3b2a24e81f20
3
+ metadata.gz: 157dbe49de1382e3c20686dbb3b22327af960831612683cb08b59db7ccf8dd8e
4
+ data.tar.gz: 88db04f7a2d879c9b25255f627d13f823f7bf487cf42fb5efc7d9c764d890b59
5
5
  SHA512:
6
- metadata.gz: 17b50c40207f615d0018c142062e536c3da69bebab209d4facb63ee03273363d97f83675d173f3096f98c68393f374e39aae0dba0db6364de9bc3c83d38af160
7
- data.tar.gz: d7bef1c4214a30fa9888843c35fe2f074ea89e78b9341b5ed458471934576dea9ef8bf12fac72bd549a532fc50d34b9e327c4f2b943e7965720f3b12bef503b6
6
+ metadata.gz: f0ddda7335aa711ca57fa20e36e4b0a3a12086263281994093749407662b2b0c5ab32851ce14c3f3b44b960a11a758b2f4462fe88c93b5f8b65bde4a05eaa2b0
7
+ data.tar.gz: 020d0e8e5084419a67c14a102d77ba76d1b445fb980fa3e441e009114f0a4e6074d47e11689aadc16edf5efdee2b4f3b2e7db15787cb0a57de125a31faecd4eb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 3.10.1 (April 30, 2025)
2
+
3
+ Bigfixes:
4
+ - Fix issue when using `delegate` instead of `has_many :through` (#411)
5
+
6
+ ## 3.10.0 (April 15, 2025)
7
+
8
+ New features:
9
+ - Update counter cache values without requiring a reload (#407)
10
+
1
11
  ## 3.9.0 (March 20, 2025)
2
12
 
3
13
  New features:
@@ -138,6 +138,7 @@ module CounterCulture
138
138
  unless Thread.current[:aggregate_counter_updates]
139
139
  execute_now_or_after_commit(obj) do
140
140
  klass.where(primary_key => id_to_change).update_all updates.join(', ')
141
+ assign_to_associated_object(obj, relation, change_counter_column, operator, delta_magnitude)
141
142
  end
142
143
  end
143
144
  end
@@ -365,6 +366,49 @@ module CounterCulture
365
366
  obj.public_send("#{attr}#{changes_method}")
366
367
  end
367
368
 
369
+ # update associated object counter attribute
370
+ def assign_to_associated_object(obj, relation, change_counter_column, operator, delta_magnitude)
371
+ association_name = relation_reflect(relation).name
372
+
373
+ association_object = association_object_for_assign(obj, association_name)
374
+ return if association_object.blank?
375
+
376
+ association_object.assign_attributes(
377
+ change_counter_column =>
378
+ association_object_new_counter(association_object, change_counter_column, operator, delta_magnitude)
379
+ )
380
+ association_object_clear_change(association_object, change_counter_column)
381
+ end
382
+
383
+ def association_object_for_assign(obj, association_name)
384
+ if obj.class.reflect_on_all_associations.
385
+ find { |assoc| assoc.name.to_sym == association_name.to_sym }.
386
+ blank?
387
+ # This means that this isn't defined as an association; either it
388
+ # doesn't exist at all, or it uses delegate. In either case, we can't
389
+ # update the count this way.
390
+ return
391
+ end
392
+ if !obj.association(association_name).loaded?
393
+ # If the association isn't loaded, no need to update the count
394
+ return
395
+ end
396
+
397
+ obj.public_send(association_name)
398
+ end
399
+
400
+ def association_object_clear_change(association_object, change_counter_column)
401
+ if ACTIVE_RECORD_VERSION >= Gem::Version.new("6.1.0")
402
+ association_object.public_send(:"clear_#{change_counter_column}_change")
403
+ else
404
+ association_object.send(:clear_attribute_change, change_counter_column)
405
+ end
406
+ end
407
+
408
+ def association_object_new_counter(association_object, change_counter_column, operator, delta_magnitude)
409
+ (association_object.public_send(change_counter_column) || 0).public_send(operator, delta_magnitude)
410
+ end
411
+
368
412
  def assemble_money_counter_update(klass, id_to_change, quoted_column, operator, delta_magnitude)
369
413
  counter_update_snippet(
370
414
  "#{quoted_column} = COALESCE(CAST(#{quoted_column} as NUMERIC), 0)",
@@ -1,3 +1,3 @@
1
1
  module CounterCulture
2
- VERSION = '3.9.0'.freeze
2
+ VERSION = '3.10.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.9.0
4
+ version: 3.10.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: 2025-03-20 00:00:00.000000000 Z
11
+ date: 2025-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord