active_record_compose 1.4.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 +4 -4
- data/CHANGELOG.md +17 -0
- data/lib/active_record_compose/attributes/delegation.rb +13 -10
- data/lib/active_record_compose/attributes.rb +41 -3
- data/lib/active_record_compose/transaction_support.rb +7 -0
- data/lib/active_record_compose/version.rb +1 -1
- data/sig/_internal/package_private.rbs +3 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e904bcabbe83a5bb2122f82c766f4ab6e7b7b7b61fb34366c9f1e8a87911f0de
|
|
4
|
+
data.tar.gz: eae09546473a8c849cda4e71324a66e4c9f7fd8c2897ebc274b493ee592d094e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 51638c2d51aebb0a9f379f72beffe7e0e3f5355ddaa2bb47073fefffa3be13fb87032bc81e57decb2770752064a107de91417faf6564f6e7ed5837c42644f953
|
|
7
|
+
data.tar.gz: c9d0312495a2046a014f804fb3d69f6dcf026e13b7b9261756b9fb15427b682fc3e91b8bbf4af35adff493882d968595f3410a6aab3539a7cd57644712ac92aa
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
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
|
+
|
|
3
20
|
## [1.4.0] - 2026-10-14
|
|
4
21
|
|
|
5
22
|
* Use a dedicated deprecator for deprecated APIs.
|
|
@@ -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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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,7 +179,11 @@ 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.
|
|
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
|
|
|
@@ -173,6 +195,22 @@ module ActiveRecordCompose
|
|
|
173
195
|
# @private
|
|
174
196
|
def attribute(...) = _require_attributes_initialized { super }
|
|
175
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
|
+
|
|
176
214
|
# @private
|
|
177
215
|
def _require_attributes_initialized
|
|
178
216
|
unless @attributes
|
|
@@ -104,6 +104,13 @@ module ActiveRecordCompose
|
|
|
104
104
|
end
|
|
105
105
|
end
|
|
106
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
|
+
|
|
107
114
|
def save(**options) = with_transaction_returning_status { super }
|
|
108
115
|
|
|
109
116
|
def save!(**options) = with_transaction_returning_status { super }
|
|
@@ -15,6 +15,7 @@ module ActiveRecordCompose
|
|
|
15
15
|
@attributes: untyped
|
|
16
16
|
|
|
17
17
|
private
|
|
18
|
+
def attribute_method?: (String attr_name) -> bool
|
|
18
19
|
def _require_attributes_initialized: [T] () { () -> T } -> T
|
|
19
20
|
|
|
20
21
|
class AttributePredicate
|
|
@@ -31,8 +32,8 @@ module ActiveRecordCompose
|
|
|
31
32
|
def initialize: (attribute: String, to: Symbol, ?allow_nil: bool) -> void
|
|
32
33
|
def attribute: () -> Symbol
|
|
33
34
|
def attribute_name: () -> String
|
|
34
|
-
def attribute_hash: (Object model) -> Hash[String, untyped]
|
|
35
35
|
def define_delegated_attribute: ((Module & ActiveModel::AttributeMethods::ClassMethods) klass) -> void
|
|
36
|
+
def undefine_delegated_attribute: ((Module & ActiveModel::AttributeMethods::ClassMethods) klass) -> void
|
|
36
37
|
|
|
37
38
|
@attribute: Symbol
|
|
38
39
|
@to: Symbol
|
|
@@ -43,6 +44,7 @@ module ActiveRecordCompose
|
|
|
43
44
|
def allow_nil: () -> bool
|
|
44
45
|
def reader: () -> String
|
|
45
46
|
def writer: () -> String
|
|
47
|
+
def predicator: () -> String
|
|
46
48
|
end
|
|
47
49
|
|
|
48
50
|
module Querying
|