delta_changes 2.6.0 → 2.6.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: 97f11bfed3b6028fd8812904f934f307c6b1c93bcd5f719178ee3afd4594d43d
4
- data.tar.gz: 353cdce6118da8ec69c32eb03be3df7c15179186553e02801883986900924a40
3
+ metadata.gz: 4303928f95fbe7fb767206dad04bef6e2ffbb55cdcb5ae5aed59b7a76ba83378
4
+ data.tar.gz: 2d1780367a7e28992416f0a160ae283dde8d3b59955f2bd17a3b665a3b473fb8
5
5
  SHA512:
6
- metadata.gz: f7463039bb4941f785c438a5b7161b2d7341f75f27f46a21ad2c81ad9cf6c13d02aa940abd3e90cddf597aaf344e0c5cceb6b58471562f1db5a6480b082f186c
7
- data.tar.gz: 4932c56c58cdddf933f051297f42a5d9cdb9996b49997f3d311059b269a05455969c040461032f8271a3656a7eaee33d8697cb226864c9bd5e1c402c6aaebbca
6
+ metadata.gz: 389e179447249c0a7abc151f53ec55029ffef7e56df5e353a09dfeaa5f598df5bca863c0ccfe23ce24a60d1f79e5d2d2fcc52a95c9275055bf7f97cb6c8a6882
7
+ data.tar.gz: 976c0d66af3512c10b06fbfc4cf28bc454ffeb9b4edce9288a4f3ad44ebe2a8fa3d1b4c8953bdc7d2b9196b112aee841ea49f1a1f40d55ce56baba6e3ab46a82
@@ -1,3 +1,3 @@
1
1
  module DeltaChanges
2
- VERSION = "2.6.0"
2
+ VERSION = "2.6.1"
3
3
  end
data/lib/delta_changes.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require "delta_changes/version"
2
+ require "active_record/version"
2
3
 
3
4
  module DeltaChanges
5
+ AR_GTE_82 = ActiveRecord.version >= Gem::Version.new("8.2.a")
6
+
4
7
  module Extension
5
8
  def self.included(base)
6
9
  base.extend(ClassMethods)
@@ -12,6 +15,11 @@ module DeltaChanges
12
15
  module ClassMethods
13
16
  def delta_changes(options)
14
17
  self.delta_changes_options = options
18
+ if AR_GTE_82
19
+ # Default Rails 8.2+ setters generated before configuration will bypass DeltaChanges. Remove existing
20
+ # attribute methods so they can be re-generated to call `_write_attribute` and be tracked.
21
+ undefine_attribute_methods if options[:columns].any?
22
+ end
15
23
  define_virtual_attribute_delta_methods
16
24
  end
17
25
 
@@ -39,6 +47,38 @@ module DeltaChanges
39
47
  end
40
48
  end
41
49
  end
50
+
51
+ if AR_GTE_82
52
+ # Rails calls `define_method_attribute=` to dynamically create assignment methods based on database columns.
53
+ # These methods used to call `_write_attribute`, which called `@attributes.write_from_user`. Rails changed the
54
+ # generated methods to call `@attributes.write_from_user` directly instead. This improved performance by
55
+ # eliminating a readonly check at runtime, but it also bypasses DeltaChanges' tracking. To keep tracking
56
+ # working, we define assignment methods to call `_write_attribute` again. We lose the performance improvement,
57
+ # but we need it to make tracking work and it's no less-performant than the original Rails 8.1 behavior.
58
+ #
59
+ # Ref: https://github.com/rails/rails/commit/0f6c0a05e2064b56f759677d94f060f057d89c66
60
+ def define_method_attribute=(canonical_name, owner:, **options)
61
+
62
+ unless delta_changes_options && delta_changes_options[:columns].include?(canonical_name)
63
+ # If a column isn't tracked, delegate setter method generation to Rails
64
+ return super
65
+ end
66
+
67
+ as = options.fetch(:as, canonical_name)
68
+ ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
69
+ canonical_name, writer: true,
70
+ ) do |temp_method_name, attr_name_expr|
71
+ owner.define_cached_method(temp_method_name, as: "#{as}=", namespace: :delta_changes) do |batch|
72
+ batch << <<~RUBY
73
+ def #{temp_method_name}(value)
74
+ _write_attribute(#{attr_name_expr}, value)
75
+ end
76
+ RUBY
77
+ end
78
+ end
79
+ end
80
+ private :define_method_attribute=
81
+ end
42
82
  end
43
83
 
44
84
  module InstanceMethods
@@ -49,7 +89,7 @@ module DeltaChanges
49
89
  return super(attr, value)
50
90
  end
51
91
 
52
- # The attribute already has an unsaved change.
92
+ # The attribute already has a delta-tracked change.
53
93
  if delta_changed_attributes.include?(attr)
54
94
  old = delta_changed_attributes[attr]
55
95
  super(attr, value)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delta_changes
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubygems_version: 3.6.7
51
+ rubygems_version: 3.6.9
52
52
  specification_version: 4
53
53
  summary: Additional real/virtual attribute change tracking independent of ActiveRecords
54
54
  test_files: []