active_record_compose 1.3.0 → 1.4.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: 148413ab669022c937e7f4775cff13dc018c9ba190014b130c10b9ca7e767a3f
4
- data.tar.gz: 51ec332dcf2decf04dfb7f3bf8f0d38bf791d36d960d0d19c388608ef3f33a32
3
+ metadata.gz: e904bcabbe83a5bb2122f82c766f4ab6e7b7b7b61fb34366c9f1e8a87911f0de
4
+ data.tar.gz: eae09546473a8c849cda4e71324a66e4c9f7fd8c2897ebc274b493ee592d094e
5
5
  SHA512:
6
- metadata.gz: 7db6b03f1a56d9d9d1ef0427362b65966bcfca997a465c4ce843d5dad9c4237e43b443ce0620629c665ab09163bf46fd362976009c2e426ae40849c1a41d18bd
7
- data.tar.gz: 43ecb29b976c4136650e2152f1533af7e06de40afeba68f7265e97ba6eff208165271c338b4348d44127d850ebe75de2908eea60a18e986ef47e181d95d6e784
6
+ metadata.gz: 51638c2d51aebb0a9f379f72beffe7e0e3f5355ddaa2bb47073fefffa3be13fb87032bc81e57decb2770752064a107de91417faf6564f6e7ed5837c42644f953
7
+ data.tar.gz: c9d0312495a2046a014f804fb3d69f6dcf026e13b7b9261756b9fb15427b682fc3e91b8bbf4af35adff493882d968595f3410a6aab3539a7cd57644712ac92aa
data/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.4.1] - 2026-11-23
4
+
5
+ * Fix attribute methods resolved via `method_missing` reading all delegated attributes.
6
+ (https://github.com/hamajyotan/active_record_compose/pull/80)
7
+ - When generated attribute methods are removed (e.g. by calling `attribute_method_suffix` after declaring attributes),
8
+ attribute methods such as `#foo`, `#foo?` were resolved by building the whole `#attributes` hash.
9
+ - As a result, they raised `ActiveSupport::DelegationError` when the delegation target of an unrelated attribute was nil.
10
+ - Attribute methods are now determined by attribute name only, without reading any attribute values.
11
+ * Reset `@transaction_coordinator` on `#dup`.
12
+ (https://github.com/hamajyotan/active_record_compose/pull/83)
13
+ - Duplicating a model inside a transaction no longer shares `@transaction_coordinator` with the original instance.
14
+ * Fix overriding and redeclaring attributes with `delegate_attribute`.
15
+ (https://github.com/hamajyotan/active_record_compose/pull/84)
16
+ - Remove conflicting `delegated_attributes` entries and delegation methods when `.attribute` is declared with the same name, and deduplicate `attribute_names`.
17
+ * refactor: Refactor `define_delegated_attribute` and optimize `#attributes` merging.
18
+ (https://github.com/hamajyotan/active_record_compose/pull/81, https://github.com/hamajyotan/active_record_compose/pull/82)
19
+
20
+ ## [1.4.0] - 2026-10-14
21
+
22
+ * Use a dedicated deprecator for deprecated APIs.
23
+ (https://github.com/hamajyotan/active_record_compose/pull/75)
24
+ * Fix `before_commit` callback timing for transactions spanning multiple database connections.
25
+ (https://github.com/hamajyotan/active_record_compose/pull/76)
26
+ - Ensure that `before_commit` callbacks are invoked at the appropriate point before the outermost transaction commits.
27
+ - Leave behavior unchanged when all models use a single database connection.
28
+ * Perform validations outside a transaction.
29
+ (https://github.com/hamajyotan/active_record_compose/pull/78)
30
+ * Execute inner models' `after_commit` callbacks before outer models' callbacks.
31
+ (https://github.com/hamajyotan/active_record_compose/pull/79)
32
+ * Add `ActiveRecordCompose::UninitializedAttribute#record`.
33
+
3
34
  ## [1.3.0] - 2026-08-09
4
35
 
5
36
  * Support secondary and multiple database
@@ -19,21 +19,22 @@ module ActiveRecordCompose
19
19
 
20
20
  def define_delegated_attribute(klass)
21
21
  klass.delegate(reader, writer, to:, allow_nil:)
22
- klass.module_eval <<~RUBY, __FILE__, __LINE__ + 1
23
- def #{reader}?
24
- ActiveRecordCompose::Attributes::AttributePredicate.new(#{reader}).call
25
- end
26
- RUBY
22
+
23
+ attr = attribute
24
+ klass.define_method(predicator) do
25
+ ActiveRecordCompose::Attributes::AttributePredicate.new(public_send(attr)).call # steep:ignore
26
+ end
27
+ end
28
+
29
+ def undefine_delegated_attribute(klass)
30
+ [ reader, writer, predicator ].each do |method_name|
31
+ klass.remove_method(method_name) if klass.method_defined?(method_name, false)
32
+ end
27
33
  end
28
34
 
29
35
  # @return [String] The attribute name as string
30
36
  def attribute_name = attribute.to_s
31
37
 
32
- # @return [Hash<String, Object>]
33
- def attribute_hash(model)
34
- { attribute_name => model.public_send(attribute) }
35
- end
36
-
37
38
  private
38
39
 
39
40
  attr_reader :to, :allow_nil
@@ -41,6 +42,8 @@ module ActiveRecordCompose
41
42
  def reader = attribute.to_s
42
43
 
43
44
  def writer = "#{attribute}="
45
+
46
+ def predicator = "#{reader}?"
44
47
  end
45
48
  end
46
49
  end
@@ -65,11 +65,29 @@ module ActiveRecordCompose
65
65
  # steep:ignore:start
66
66
 
67
67
  class_methods do
68
+ # Defines a model attribute and removes any conflicting delegated attribute
69
+ # with the same name so that the later declaration takes precedence.
70
+ #
71
+ # @see ActiveModel::Attributes::ClassMethods#attribute
72
+ def attribute(name, ...)
73
+ target = name.to_sym
74
+ delegations, remaining = delegated_attributes.to_a.partition { _1.attribute == target }
75
+ if delegations.any?
76
+ self.delegated_attributes = remaining
77
+ delegations.each { _1.undefine_delegated_attribute(self) }
78
+ end
79
+
80
+ super
81
+ end
82
+
68
83
  # Provides a method of attribute access to the encapsulated model.
69
84
  #
70
85
  # It provides a way to access the attributes of the model it encompasses,
71
86
  # allowing transparent access as if it had those attributes itself.
72
87
  #
88
+ # If an attribute with the same name was previously declared via
89
+ # {.attribute} or {.delegate_attribute}, the later declaration takes precedence.
90
+ #
73
91
  # @param [Array<Symbol, String>] attributes
74
92
  # attributes A variable-length list of attribute names to delegate.
75
93
  # @param [Symbol, String] to
@@ -97,7 +115,7 @@ module ActiveRecordCompose
97
115
  #
98
116
  # @see #attribute_names
99
117
  # @return [Array<String>] array of attribute name.
100
- def attribute_names = super + delegated_attributes.to_a.map { _1.attribute_name }
118
+ def attribute_names = (super + delegated_attributes.to_a.map { _1.attribute_name }).uniq
101
119
  end
102
120
 
103
121
  # steep:ignore:end
@@ -130,7 +148,7 @@ module ActiveRecordCompose
130
148
  # @return [Array<String>] array of attribute name.
131
149
  def attribute_names
132
150
  _require_attributes_initialized do
133
- super + delegated_attributes.to_a.map { _1.attribute_name }
151
+ (super + delegated_attributes.to_a.map { _1.attribute_name }).uniq
134
152
  end
135
153
  end
136
154
 
@@ -161,20 +179,42 @@ module ActiveRecordCompose
161
179
  # @return [Hash<String, Object>] hash with the attribute name as key and the attribute value as value.
162
180
  def attributes
163
181
  _require_attributes_initialized do
164
- super.merge(*delegated_attributes.to_a.map { _1.attribute_hash(self) })
182
+ super.tap do |hash|
183
+ delegated_attributes&.each do |delegation|
184
+ hash[delegation.attribute_name] = public_send(delegation.attribute)
185
+ end
186
+ end
165
187
  end
166
188
  end
167
189
 
168
190
  private
169
191
 
170
- def _write_attribute(...) = _require_attributes_initialized { super } # steep:ignore
192
+ # @private
193
+ def _write_attribute(...) = _require_attributes_initialized { super }
171
194
 
195
+ # @private
172
196
  def attribute(...) = _require_attributes_initialized { super }
173
197
 
198
+ # @private
199
+ #
200
+ # Overrides `ActiveModel::AttributeMethods#attribute_method?`, which is used by
201
+ # `method_missing` and `respond_to?`.
202
+ # The original implementation builds the whole `#attributes` hash, which reads every
203
+ # delegated attribute. This causes unnecessary reads and raises an error when
204
+ # a delegation target of an unrelated attribute is nil.
205
+ # Here we determine it by attribute name only, without reading any values.
206
+ # As with the original implementation (via `#attributes`), it raises
207
+ # `ActiveRecordCompose::UninitializedAttribute` if the attributes are not initialized.
208
+ #
209
+ def attribute_method?(attr_name)
210
+ _require_attributes_initialized { @attributes.key?(attr_name) } ||
211
+ delegated_attributes.to_a.any? { _1.attribute_name == attr_name }
212
+ end
213
+
214
+ # @private
174
215
  def _require_attributes_initialized
175
216
  unless @attributes
176
- raise ActiveRecordCompose::UninitializedAttribute,
177
- "No attributes have been set. Is proper initialization performed, such as calling `super` in `initialize`?"
217
+ raise ActiveRecordCompose::UninitializedAttribute.new(self)
178
218
  end
179
219
 
180
220
  yield
@@ -1,6 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCompose
4
+ # Generic Active Record Compose exception class.
5
+ #
6
+ class ActiveRecordComposeError < StandardError
7
+ end
8
+
4
9
  # Occurs when a circular reference is detected in the containing model.
5
10
  #
6
11
  # @example
@@ -25,7 +30,7 @@ module ActiveRecordCompose
25
30
  # inner.model = outer # There is a circular reference in the form outer > middle > inner > outer.
26
31
  # outer.save #=> raises ActiveRecordCompose::CircularReferenceDetected
27
32
  #
28
- class CircularReferenceDetected < StandardError; end
33
+ class CircularReferenceDetected < ActiveRecordComposeError; end
29
34
 
30
35
  # Occurs when accessing Attributes without initializing it.
31
36
  #
@@ -40,5 +45,13 @@ module ActiveRecordCompose
40
45
  # model = Model.new
41
46
  # model.foo = 1 #=> raises ActiveRecordCompose::UninitializedAttribute
42
47
  #
43
- class UninitializedAttribute < StandardError; end
48
+ class UninitializedAttribute < ActiveRecordComposeError
49
+ attr_reader :record
50
+
51
+ def initialize(record = nil)
52
+ @record = record
53
+ message = "No attributes have been set. Is proper initialization performed, such as calling `super` in `initialize`?"
54
+ super(message)
55
+ end
56
+ end
44
57
  end
@@ -85,8 +85,8 @@ module ActiveRecordCompose
85
85
 
86
86
  include ActiveRecordCompose::Attributes
87
87
  include ActiveRecordCompose::Persistence
88
- include ActiveRecordCompose::Validations
89
88
  include ActiveRecordCompose::TransactionSupport
89
+ include ActiveRecordCompose::Validations
90
90
  include ActiveRecordCompose::Inspectable
91
91
 
92
92
  def initialize(attributes = {})
@@ -15,10 +15,9 @@ module ActiveRecordCompose
15
15
  # but not just before any inner transactions commit.
16
16
  #
17
17
  def on_before_comitted
18
- return if @_before_committed_called
18
+ return unless final_transaction?
19
19
 
20
20
  yield
21
- @_before_committed_called = true
22
21
  end
23
22
 
24
23
  # It processes after_commit/after_rollback only immediately
@@ -35,10 +34,11 @@ module ActiveRecordCompose
35
34
  def with_transaction_cleanup
36
35
  yield
37
36
  ensure
38
- @_before_committed_called = false
39
37
  transactions.reject! { _1.closed? }
40
38
  end
41
39
 
40
+ def final_transaction? = transactions.one? { !_1.closed? }
41
+
42
42
  def all_finished? = transactions.all? { _1.closed? }
43
43
 
44
44
  def transactions = @transactions ||= Set.new
@@ -15,24 +15,26 @@ module ActiveRecordCompose
15
15
  # steep:ignore:start
16
16
 
17
17
  class_methods do
18
- # @private
19
- # @deprecated
20
- def with_connection(...)
21
- ActiveRecord.deprecator.warn("`with_connection` is deprecated. Use `ActiveRecord::Base.with_connection` instead.")
22
- ActiveRecord::Base.with_connection(...)
23
- end
18
+ if ActiveRecord.version >= Gem::Version.new("7.2")
19
+ # @private
20
+ # @deprecated
21
+ def with_connection(...)
22
+ ActiveRecordCompose.deprecator.deprecation_warning(:with_connection, "Use `ActiveRecord::Base.with_connection` instead.")
23
+ ActiveRecord::Base.with_connection(...)
24
+ end
24
25
 
25
- # @private
26
- # @deprecated
27
- def lease_connection(...)
28
- ActiveRecord.deprecator.warn("`lease_connection` is deprecated. Use `ActiveRecord::Base.lease_connection` instead.")
29
- ActiveRecord::Base.lease_connection(...)
26
+ # @private
27
+ # @deprecated
28
+ def lease_connection(...)
29
+ ActiveRecordCompose.deprecator.deprecation_warning(:lease_connection, "Use `ActiveRecord::Base.lease_connection` instead.")
30
+ ActiveRecord::Base.lease_connection(...)
31
+ end
30
32
  end
31
33
 
32
34
  # @private
33
35
  # @deprecated
34
36
  def connection(...)
35
- ActiveRecord.deprecator.warn("`connection` is deprecated. Use `ActiveRecord::Base.connection` instead.")
37
+ ActiveRecordCompose.deprecator.deprecation_warning(:connection, "Use `ActiveRecord::Base.connection` instead.")
36
38
  ActiveRecord::Base.connection(...)
37
39
  end
38
40
  end
@@ -102,6 +104,13 @@ module ActiveRecordCompose
102
104
  end
103
105
  end
104
106
 
107
+ # @private
108
+ # A duplicated model must not share the transaction coordinator with the original.
109
+ def initialize_dup(other)
110
+ @transaction_coordinator = nil # steep:ignore
111
+ super
112
+ end
113
+
105
114
  def save(**options) = with_transaction_returning_status { super }
106
115
 
107
116
  def save!(**options) = with_transaction_returning_status { super }
@@ -135,9 +144,12 @@ module ActiveRecordCompose
135
144
  connection.transaction do |tran|
136
145
  tran ||= ActiveTransaction.new(connection.current_transaction) # steep:ignore
137
146
  transaction_coordinator.add_transaction(tran)
138
- connection.add_transaction_record(self, ensure_finalize || has_transactional_callbacks?)
139
147
 
140
- inner_proc.call.tap { raise ActiveRecord::Rollback unless _1 }
148
+ begin
149
+ inner_proc.call.tap { raise ActiveRecord::Rollback unless _1 }
150
+ ensure
151
+ connection.add_transaction_record(self, ensure_finalize || has_transactional_callbacks?)
152
+ end
141
153
  end
142
154
  end
143
155
  end
@@ -155,7 +167,7 @@ module ActiveRecordCompose
155
167
  # @private
156
168
  def connection_pool(ar_class: default_ar_class)
157
169
  connection_specification_name = ar_class.connection_specification_name
158
- role = ar_class.current_role
170
+ role = ar_class.current_role # steep:ignore
159
171
  shard = ar_class.current_shard # steep:ignore
160
172
  connection_handler = ar_class.connection_handler # steep:ignore
161
173
  retrieve_options = { role:, shard: }
@@ -167,7 +179,7 @@ module ActiveRecordCompose
167
179
  # @private
168
180
  def with_pool_transaction_isolation_level(connection, &block)
169
181
  if ActiveRecord.gem_version.release >= Gem::Version.new("8.1.0")
170
- isolation_level = ActiveRecord.default_transaction_isolation_level # steep:ignore
182
+ isolation_level = ActiveRecord.default_transaction_isolation_level
171
183
  connection.pool.with_pool_transaction_isolation_level(isolation_level, connection.transaction_open?, &block)
172
184
  else
173
185
  block.call
@@ -1,5 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordCompose
4
- VERSION = "1.3.0"
4
+ VERSION = "1.4.1"
5
+
6
+ class << self
7
+ def version = Gem::Version.new(VERSION)
8
+
9
+ private
10
+
11
+ # @private
12
+ def next_major_version
13
+ version.segments.first + 1 # steep:ignore
14
+ end
15
+ end
5
16
  end
@@ -11,6 +11,10 @@ require_relative "active_record_compose/model"
11
11
  # Most of the functionality resides in {ActiveRecordCompose::Model}.
12
12
  #
13
13
  module ActiveRecordCompose
14
+ # @private
15
+ def self.deprecator
16
+ @deprecator ||= ActiveSupport::Deprecation.new(next_major_version, "ActiveRecordCompose") # steep:ignore
17
+ end
14
18
  end
15
19
 
16
20
  require "active_record_compose/railtie" if defined?(::Rails::Railtie)
@@ -1,4 +1,7 @@
1
1
  module ActiveRecordCompose
2
+ private
3
+ def self.next_major_version: () -> Integer
4
+
2
5
  module Attributes
3
6
  extend ActiveSupport::Concern
4
7
  include ActiveModel::Attributes
@@ -12,6 +15,7 @@ module ActiveRecordCompose
12
15
  @attributes: untyped
13
16
 
14
17
  private
18
+ def attribute_method?: (String attr_name) -> bool
15
19
  def _require_attributes_initialized: [T] () { () -> T } -> T
16
20
 
17
21
  class AttributePredicate
@@ -28,8 +32,8 @@ module ActiveRecordCompose
28
32
  def initialize: (attribute: String, to: Symbol, ?allow_nil: bool) -> void
29
33
  def attribute: () -> Symbol
30
34
  def attribute_name: () -> String
31
- def attribute_hash: (Object model) -> Hash[String, untyped]
32
35
  def define_delegated_attribute: ((Module & ActiveModel::AttributeMethods::ClassMethods) klass) -> void
36
+ def undefine_delegated_attribute: ((Module & ActiveModel::AttributeMethods::ClassMethods) klass) -> void
33
37
 
34
38
  @attribute: Symbol
35
39
  @to: Symbol
@@ -40,6 +44,7 @@ module ActiveRecordCompose
40
44
  def allow_nil: () -> bool
41
45
  def reader: () -> String
42
46
  def writer: () -> String
47
+ def predicator: () -> String
43
48
  end
44
49
 
45
50
  module Querying
@@ -139,7 +144,6 @@ module ActiveRecordCompose
139
144
  def transaction_coordinator: () -> ActiveRecordCompose::TransactionSupport::TransactionCoordinator
140
145
 
141
146
  class TransactionCoordinator
142
- @_before_committed_called: bool
143
147
  @transactions: Set[ActiveRecord::ConnectionAdapters::Transaction]
144
148
 
145
149
  def add_transaction: (ActiveRecord::ConnectionAdapters::Transaction) -> void
@@ -147,6 +151,7 @@ module ActiveRecordCompose
147
151
  def on_before_comitted: () { () -> void } -> void
148
152
 
149
153
  private
154
+ def final_transaction?: -> bool
150
155
  def all_finished?: -> bool
151
156
  def transactions: () -> Set[ActiveRecord::ConnectionAdapters::Transaction]
152
157
  def with_transaction_cleanup: () { () -> void } -> void
@@ -4,6 +4,9 @@
4
4
  module ActiveRecordCompose
5
5
  VERSION: String
6
6
 
7
+ def self.version: () -> Gem::Version
8
+ def self.deprecator: () -> ActiveSupport::Deprecation
9
+
7
10
  interface _ARLike
8
11
  def save: (**untyped options) -> bool
9
12
  def save!: (**untyped options) -> untyped
@@ -36,6 +39,9 @@ module ActiveRecordCompose
36
39
  type destroy_context_type = ((^() -> boolish) | (^(ar_like) -> boolish))
37
40
  type condition_type = ((^() -> boolish) | (^(ar_like) -> boolish))
38
41
 
42
+ class ActiveRecordComposeError < StandardError
43
+ end
44
+
39
45
  class ComposedCollection
40
46
  include ::Enumerable[ar_like]
41
47
 
@@ -47,7 +53,7 @@ module ActiveRecordCompose
47
53
  def delete: (ar_like) -> ComposedCollection?
48
54
  end
49
55
 
50
- class CircularReferenceDetected < StandardError
56
+ class CircularReferenceDetected < ActiveRecordComposeError
51
57
  end
52
58
 
53
59
  class Model
@@ -99,6 +105,9 @@ module ActiveRecordCompose
99
105
  extend Rails::Initializable::ClassMethods
100
106
  end
101
107
 
102
- class UninitializedAttribute < StandardError
108
+ class UninitializedAttribute < ActiveRecordComposeError
109
+ def initialize: (?untyped) -> void
110
+
111
+ attr_reader record: untyped
103
112
  end
104
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_compose
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hamajyotan