activemodel 7.2.3 → 8.1.3

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +36 -56
  3. data/lib/active_model/attribute/user_provided_default.rb +10 -0
  4. data/lib/active_model/attribute.rb +9 -1
  5. data/lib/active_model/attribute_assignment.rb +22 -1
  6. data/lib/active_model/attribute_methods.rb +4 -4
  7. data/lib/active_model/attribute_mutation_tracker.rb +5 -5
  8. data/lib/active_model/attribute_set.rb +1 -1
  9. data/lib/active_model/attributes/normalization.rb +197 -0
  10. data/lib/active_model/conversion.rb +1 -1
  11. data/lib/active_model/dirty.rb +11 -4
  12. data/lib/active_model/error.rb +3 -2
  13. data/lib/active_model/errors.rb +1 -4
  14. data/lib/active_model/gem_version.rb +2 -2
  15. data/lib/active_model/lint.rb +7 -3
  16. data/lib/active_model/naming.rb +0 -1
  17. data/lib/active_model/nested_error.rb +1 -3
  18. data/lib/active_model/railtie.rb +8 -4
  19. data/lib/active_model/secure_password.rb +60 -3
  20. data/lib/active_model/serialization.rb +21 -21
  21. data/lib/active_model/translation.rb +8 -3
  22. data/lib/active_model/type/big_integer.rb +21 -0
  23. data/lib/active_model/type/boolean.rb +1 -0
  24. data/lib/active_model/type/date.rb +1 -0
  25. data/lib/active_model/type/date_time.rb +8 -0
  26. data/lib/active_model/type/decimal.rb +1 -0
  27. data/lib/active_model/type/float.rb +1 -0
  28. data/lib/active_model/type/helpers/immutable.rb +13 -0
  29. data/lib/active_model/type/helpers/time_value.rb +20 -44
  30. data/lib/active_model/type/helpers.rb +1 -0
  31. data/lib/active_model/type/immutable_string.rb +2 -0
  32. data/lib/active_model/type/integer.rb +35 -19
  33. data/lib/active_model/type/string.rb +4 -0
  34. data/lib/active_model/type/value.rb +4 -4
  35. data/lib/active_model/validations/acceptance.rb +1 -1
  36. data/lib/active_model/validations/callbacks.rb +10 -0
  37. data/lib/active_model/validations/validates.rb +7 -2
  38. data/lib/active_model/validations.rb +53 -30
  39. data/lib/active_model.rb +6 -0
  40. metadata +10 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd11eb90ab99961854be28823adafd875c43c0ee7d7e6067b2af752a1f183f88
4
- data.tar.gz: 690c5e435225163f5ebee8eadeb5ebdcb6b9269140be69471e187d4bce24b7fa
3
+ metadata.gz: 6f7d8fbb4c4137982a3ccb5521f42a84a3456b4c2502ed639da79b7d7587b6de
4
+ data.tar.gz: 3f0d1b35fcc14b77642ee595bd681a8a666c8e925d0d9e3d4f090a256549a922
5
5
  SHA512:
6
- metadata.gz: c0c5710770bf4a62c45d71ccc25629f73c39f45c9c38f013abd7947c63e72c4e35ec0bfe85da0b32a4ab3c62892aadea62b78ca64e04666015a318339d544c00
7
- data.tar.gz: e1db829ae10154c73749425d12c1749df1cbbb9f6485c68badac84f2bd945d91ae9c0022eb0d5e886c11edced5e13476be20bf49605e2b318cb2d1dd3e58d9a8
6
+ metadata.gz: b5a74d83ab02559e2aff05ccbcc3076ac618e26c8aeb8927701caee22a7aea360448f38e16b9469d9b17c71c8d0ebbb81dc87c550c365bb9a5a435891cb31054
7
+ data.tar.gz: 9b9eff19887f81221fde407bc4e4233cf0e3ab03534778d8aba2ae614ab55b0c0a0d6adf9b7755242416709cc9389c696967299b4be77cf69728a3eccbc9a423
data/CHANGELOG.md CHANGED
@@ -1,88 +1,68 @@
1
- ## Rails 7.2.3 (October 28, 2025) ##
1
+ ## Rails 8.1.3 (March 24, 2026) ##
2
2
 
3
- * Fix `has_secure_password` to perform confirmation validation of the password even when blank.
3
+ * Fix Ruby 4.0 delegator warning when calling inspect on attributes.
4
4
 
5
- The validation was incorrectly skipped when the password only contained whitespace characters.
5
+ *Hammad Khan*
6
6
 
7
- *Fabio Sangiovanni*
7
+ * Fix `NoMethodError` when deserialising `Type::Integer` objects marshalled under Rails 8.0.
8
8
 
9
- * Handle missing attributes for `ActiveModel::Translation#human_attribute_name`.
9
+ The performance optimisation that replaced `@range` with `@max`/`@min`
10
+ broke Marshal compatibility. Objects serialised under 8.0 (with `@range`)
11
+ and deserialised under 8.1 (expecting `@max`/`@min`) would crash with
12
+ `undefined method '<=' for nil` because `Marshal.load` restores instance
13
+ variables without calling `initialize`.
10
14
 
11
- *zzak*
15
+ *Edward Woodcock*
12
16
 
13
- * Fix `ActiveModel::AttributeAssignment#assign_attributes` to accept objects without `each`.
14
17
 
15
- *Kouhei Yanagita*
16
-
17
-
18
- ## Rails 7.2.2.2 (August 13, 2025) ##
18
+ ## Rails 8.1.2.1 (March 23, 2026) ##
19
19
 
20
20
  * No changes.
21
21
 
22
22
 
23
- ## Rails 7.2.2.1 (December 10, 2024) ##
23
+ ## Rails 8.1.2 (January 08, 2026) ##
24
24
 
25
25
  * No changes.
26
26
 
27
27
 
28
- ## Rails 7.2.2 (October 30, 2024) ##
29
-
30
- * Fix regression in `alias_attribute` to work with user defined methods.
31
-
32
- `alias_attribute` would wrongly assume the attribute accessor was generated by Active Model.
33
-
34
- ```ruby
35
- class Person
36
- include ActiveModel::AttributeMethods
37
-
38
- define_attribute_methods :name
39
- attr_accessor :name
40
-
41
- alias_attribute :full_name, :name
42
- end
43
-
44
- person.full_name # => NoMethodError: undefined method `attribute' for an instance of Person
45
- ```
46
-
47
- *Jean Boussier*
48
-
49
-
50
- ## Rails 7.2.1.2 (October 23, 2024) ##
28
+ ## Rails 8.1.1 (October 28, 2025) ##
51
29
 
52
30
  * No changes.
53
31
 
54
32
 
55
- ## Rails 7.2.1.1 (October 15, 2024) ##
56
-
57
- * No changes.
33
+ ## Rails 8.1.0 (October 22, 2025) ##
58
34
 
35
+ * Add `reset_token: { expires_in: ... }` option to `has_secure_password`.
59
36
 
60
- ## Rails 7.2.1 (August 22, 2024) ##
37
+ Allows configuring the expiry duration of password reset tokens (default remains 15 minutes for backwards compatibility).
61
38
 
62
- * No changes.
39
+ ```ruby
40
+ has_secure_password reset_token: { expires_in: 1.hour }
41
+ ```
63
42
 
43
+ *Jevin Sew*, *Abeid Ahmed*
64
44
 
65
- ## Rails 7.2.0 (August 09, 2024) ##
45
+ * Add `except_on:` option for validation callbacks.
66
46
 
67
- * Fix a bug where type casting of string to `Time` and `DateTime` doesn't
68
- calculate minus minute value in TZ offset correctly.
47
+ *Ben Sheldon*
69
48
 
70
- *Akira Matsuda*
49
+ * Backport `ActiveRecord::Normalization` to `ActiveModel::Attributes::Normalization`
71
50
 
72
- * Port the `type_for_attribute` method to Active Model. Classes that include
73
- `ActiveModel::Attributes` will now provide this method. This method behaves
74
- the same for Active Model as it does for Active Record.
51
+ ```ruby
52
+ class User
53
+ include ActiveModel::Attributes
54
+ include ActiveModel::Attributes::Normalization
75
55
 
76
- ```ruby
77
- class MyModel
78
- include ActiveModel::Attributes
56
+ attribute :email, :string
79
57
 
80
- attribute :my_attribute, :integer
81
- end
58
+ normalizes :email, with: -> email { email.strip.downcase }
59
+ end
82
60
 
83
- MyModel.type_for_attribute(:my_attribute) # => #<ActiveModel::Type::Integer ...>
84
- ```
61
+ user = User.new
62
+ user.email = " CRUISE-CONTROL@EXAMPLE.COM\n"
63
+ user.email # => "cruise-control@example.com"
64
+ ```
85
65
 
86
- *Jonathan Hefner*
66
+ *Sean Doyle*
87
67
 
88
- Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/activemodel/CHANGELOG.md) for previous changes.
68
+ Please check [8-0-stable](https://github.com/rails/rails/blob/8-0-stable/activemodel/CHANGELOG.md) for previous changes.
@@ -26,6 +26,16 @@ module ActiveModel
26
26
  self.class.new(name, user_provided_value, type, original_attribute)
27
27
  end
28
28
 
29
+ def dup_or_share # :nodoc:
30
+ # Can't elide dup when the default is a Proc
31
+ # See Attribute#dup_or_share
32
+ if @user_provided_value.is_a?(Proc)
33
+ dup
34
+ else
35
+ super
36
+ end
37
+ end
38
+
29
39
  def marshal_dump
30
40
  result = [
31
41
  name,
@@ -38,7 +38,7 @@ module ActiveModel
38
38
  @value = value unless value.nil?
39
39
  end
40
40
 
41
- def value
41
+ def value(&)
42
42
  # `defined?` is cheaper than `||=` when we get back falsy values
43
43
  @value = type_cast(value_before_type_cast) unless defined?(@value)
44
44
  @value
@@ -96,6 +96,14 @@ module ActiveModel
96
96
  end
97
97
  end
98
98
 
99
+ def dup_or_share # :nodoc:
100
+ if @type.mutable?
101
+ dup
102
+ else
103
+ self # If the underlying type is immutable we can get away with not duping
104
+ end
105
+ end
106
+
99
107
  def type_cast(*)
100
108
  raise NotImplementedError
101
109
  end
@@ -36,6 +36,27 @@ module ActiveModel
36
36
 
37
37
  alias attributes= assign_attributes
38
38
 
39
+ # Like `BasicObject#method_missing`, `#attribute_writer_missing` is invoked
40
+ # when `#assign_attributes` is passed an unknown attribute name.
41
+ #
42
+ # By default, `#attribute_writer_missing` raises an UnknownAttributeError.
43
+ #
44
+ # class Rectangle
45
+ # include ActiveModel::AttributeAssignment
46
+ #
47
+ # attr_accessor :length, :width
48
+ #
49
+ # def attribute_writer_missing(name, value)
50
+ # Rails.logger.warn "Tried to assign to unknown attribute #{name}"
51
+ # end
52
+ # end
53
+ #
54
+ # rectangle = Rectangle.new
55
+ # rectangle.assign_attributes(height: 10) # => Logs "Tried to assign to unknown attribute 'height'"
56
+ def attribute_writer_missing(name, value)
57
+ raise UnknownAttributeError.new(self, name)
58
+ end
59
+
39
60
  private
40
61
  def _assign_attributes(attributes)
41
62
  attributes.each_pair do |k, v|
@@ -50,7 +71,7 @@ module ActiveModel
50
71
  if respond_to?(setter)
51
72
  raise
52
73
  else
53
- raise UnknownAttributeError.new(self, k.to_s)
74
+ attribute_writer_missing(k.to_s, v)
54
75
  end
55
76
  end
56
77
  end
@@ -321,8 +321,8 @@ module ActiveModel
321
321
  canonical_method_name = pattern.method_name(attr_name)
322
322
  public_method_name = pattern.method_name(as)
323
323
 
324
- # If defining a regular attribute method, we don't override methods that are explictly
325
- # defined in parrent classes.
324
+ # If defining a regular attribute method, we don't override methods that are explicitly
325
+ # defined in parent classes.
326
326
  if instance_method_already_implemented?(public_method_name)
327
327
  # However, for `alias_attribute`, we always define the method.
328
328
  # We check for override second because `instance_method_already_implemented?`
@@ -373,7 +373,7 @@ module ActiveModel
373
373
  # person.name_short? # => NoMethodError
374
374
  # person.first_name # => NoMethodError
375
375
  def undefine_attribute_methods
376
- generated_attribute_methods.module_eval do
376
+ @generated_attribute_methods&.module_eval do
377
377
  undef_method(*instance_methods)
378
378
  end
379
379
  attribute_method_patterns_cache.clear
@@ -402,7 +402,7 @@ module ActiveModel
402
402
  end
403
403
 
404
404
  def instance_method_already_implemented?(method_name)
405
- generated_attribute_methods.method_defined?(method_name)
405
+ @generated_attribute_methods&.method_defined?(method_name)
406
406
  end
407
407
 
408
408
  # The methods +method_missing+ and +respond_to?+ of this module are
@@ -5,7 +5,7 @@ require "active_support/core_ext/object/duplicable"
5
5
 
6
6
  module ActiveModel
7
7
  class AttributeMutationTracker # :nodoc:
8
- OPTION_NOT_GIVEN = Object.new
8
+ OPTION_NOT_GIVEN = Object.new.freeze
9
9
 
10
10
  def initialize(attributes)
11
11
  @attributes = attributes
@@ -16,17 +16,17 @@ module ActiveModel
16
16
  end
17
17
 
18
18
  def changed_values
19
- attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
19
+ attr_names.each_with_object(ActiveSupport::HashWithIndifferentAccess.new) do |attr_name, result|
20
20
  if changed?(attr_name)
21
- result[attr_name] = original_value(attr_name)
21
+ result.store(attr_name, original_value(attr_name), convert_value: false)
22
22
  end
23
23
  end
24
24
  end
25
25
 
26
26
  def changes
27
- attr_names.each_with_object({}.with_indifferent_access) do |attr_name, result|
27
+ attr_names.each_with_object(ActiveSupport::HashWithIndifferentAccess.new) do |attr_name, result|
28
28
  if change = change_to_attribute(attr_name)
29
- result.merge!(attr_name => change)
29
+ result.store(attr_name, change, convert_value: false)
30
30
  end
31
31
  end
32
32
  end
@@ -71,7 +71,7 @@ module ActiveModel
71
71
  end
72
72
 
73
73
  def deep_dup
74
- AttributeSet.new(attributes.deep_dup)
74
+ AttributeSet.new(attributes.transform_values(&:dup_or_share))
75
75
  end
76
76
 
77
77
  def initialize_dup(_)
@@ -0,0 +1,197 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ module Attributes
5
+ module Normalization
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include ActiveModel::Dirty
10
+ include ActiveModel::Validations::Callbacks
11
+
12
+ class_attribute :normalized_attributes, default: Set.new
13
+
14
+ before_validation :normalize_changed_in_place_attributes
15
+ end
16
+
17
+ # Normalizes a specified attribute using its declared normalizations.
18
+ #
19
+ # ==== Examples
20
+ #
21
+ # class User
22
+ # include ActiveModel::Attributes
23
+ # include ActiveModel::Attributes::Normalization
24
+ #
25
+ # attribute :email, :string
26
+ #
27
+ # normalizes :email, with: -> email { email.strip.downcase }
28
+ # end
29
+ #
30
+ # legacy_user = User.load_from_legacy_data(...)
31
+ # legacy_user.email # => " CRUISE-CONTROL@EXAMPLE.COM\n"
32
+ # legacy_user.normalize_attribute(:email)
33
+ # legacy_user.email # => "cruise-control@example.com"
34
+ #
35
+ # ==== Behavior with Active Record
36
+ #
37
+ # To prevent confusion, normalization will not be applied
38
+ # when the attribute is fetched from the database. This means that if a
39
+ # record was persisted before the normalization was declared, the record's
40
+ # attribute will not be normalized until either it is assigned a new
41
+ # value, or it is explicitly migrated via Normalization#normalize_attribute.
42
+ #
43
+ # Be aware that if your app was created before Rails 7.1, and your app
44
+ # marshals instances of the targeted model (for example, when caching),
45
+ # then you should set ActiveRecord.marshalling_format_version to +7.1+ or
46
+ # higher via either <tt>config.load_defaults 7.1</tt> or
47
+ # <tt>config.active_record.marshalling_format_version = 7.1</tt>.
48
+ # Otherwise, +Marshal+ may attempt to serialize the normalization +Proc+
49
+ # and raise +TypeError+.
50
+ #
51
+ # class User < ActiveRecord::Base
52
+ # normalizes :email, with: -> email { email.strip.downcase }
53
+ # normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
54
+ # end
55
+ #
56
+ # user = User.create(email: " CRUISE-CONTROL@EXAMPLE.COM\n")
57
+ # user.email # => "cruise-control@example.com"
58
+ #
59
+ # user = User.find_by(email: "\tCRUISE-CONTROL@EXAMPLE.COM ")
60
+ # user.email # => "cruise-control@example.com"
61
+ # user.email_before_type_cast # => "cruise-control@example.com"
62
+ #
63
+ # User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1
64
+ # User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0
65
+ #
66
+ # User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true
67
+ # User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false
68
+ #
69
+ # User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
70
+ def normalize_attribute(name)
71
+ # Treat the value as a new, unnormalized value.
72
+ send(:"#{name}=", send(name))
73
+ end
74
+
75
+ module ClassMethods
76
+ # Declares a normalization for one or more attributes. The normalization
77
+ # is applied when the attribute is assigned or validated.
78
+ #
79
+ # Because the normalization may be applied multiple times, it should be
80
+ # _idempotent_. In other words, applying the normalization more than once
81
+ # should have the same result as applying it only once.
82
+ #
83
+ # By default, the normalization will not be applied to +nil+ values. This
84
+ # behavior can be changed with the +:apply_to_nil+ option.
85
+ #
86
+ # ==== Options
87
+ #
88
+ # * +:with+ - Any callable object that accepts the attribute's value as
89
+ # its sole argument, and returns it normalized.
90
+ # * +:apply_to_nil+ - Whether to apply the normalization to +nil+ values.
91
+ # Defaults to +false+.
92
+ #
93
+ # ==== Examples
94
+ #
95
+ # class User
96
+ # include ActiveModel::Attributes
97
+ # include ActiveModel::Attributes::Normalization
98
+ #
99
+ # attribute :email, :string
100
+ # attribute :phone, :string
101
+ #
102
+ # normalizes :email, with: -> email { email.strip.downcase }
103
+ # normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
104
+ # end
105
+ #
106
+ # user = User.new
107
+ # user.email = " CRUISE-CONTROL@EXAMPLE.COM\n"
108
+ # user.email # => "cruise-control@example.com"
109
+ #
110
+ # User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
111
+ def normalizes(*names, with:, apply_to_nil: false)
112
+ decorate_attributes(names) do |name, cast_type|
113
+ NormalizedValueType.new(cast_type: cast_type, normalizer: with, normalize_nil: apply_to_nil)
114
+ end
115
+
116
+ self.normalized_attributes += names.map(&:to_sym)
117
+ end
118
+
119
+ # Normalizes a given +value+ using normalizations declared for +name+.
120
+ #
121
+ # ==== Examples
122
+ #
123
+ # class User
124
+ # include ActiveModel::Attributes
125
+ # include ActiveModel::Attributes::Normalization
126
+ #
127
+ # attribute :email, :string
128
+ #
129
+ # normalizes :email, with: -> email { email.strip.downcase }
130
+ # end
131
+ #
132
+ # User.normalize_value_for(:email, " CRUISE-CONTROL@EXAMPLE.COM\n")
133
+ # # => "cruise-control@example.com"
134
+ def normalize_value_for(name, value)
135
+ type_for_attribute(name).cast(value)
136
+ end
137
+ end
138
+
139
+ private
140
+ def normalize_changed_in_place_attributes
141
+ self.class.normalized_attributes.each do |name|
142
+ normalize_attribute(name) if attribute_changed_in_place?(name)
143
+ end
144
+ end
145
+
146
+ class NormalizedValueType < DelegateClass(ActiveModel::Type::Value) # :nodoc:
147
+ include ActiveModel::Type::SerializeCastValue
148
+
149
+ attr_reader :cast_type, :normalizer, :normalize_nil
150
+ alias :normalize_nil? :normalize_nil
151
+
152
+ def initialize(cast_type:, normalizer:, normalize_nil:)
153
+ @cast_type = cast_type
154
+ @normalizer = normalizer
155
+ @normalize_nil = normalize_nil
156
+ super(cast_type)
157
+ end
158
+
159
+ def cast(value)
160
+ normalize(super(value))
161
+ end
162
+
163
+ def serialize(value)
164
+ serialize_cast_value(cast(value))
165
+ end
166
+
167
+ def serialize_cast_value(value)
168
+ ActiveModel::Type::SerializeCastValue.serialize(cast_type, value)
169
+ end
170
+
171
+ def ==(other)
172
+ self.class == other.class &&
173
+ normalize_nil? == other.normalize_nil? &&
174
+ normalizer == other.normalizer &&
175
+ cast_type == other.cast_type
176
+ end
177
+ alias eql? ==
178
+
179
+ def hash
180
+ [self.class, cast_type, normalizer, normalize_nil?].hash
181
+ end
182
+
183
+ define_method(:inspect, Kernel.instance_method(:inspect))
184
+
185
+ private
186
+ # Prevent Ruby 4.0 "delegator does not forward private method" warning.
187
+ # Kernel#inspect calls instance_variables_to_inspect which, without this,
188
+ # triggers Delegator#respond_to_missing? for a private method.
189
+ define_method(:instance_variables_to_inspect, Kernel.instance_method(:instance_variables))
190
+
191
+ def normalize(value)
192
+ normalizer.call(value) unless value.nil? && !normalize_nil?
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end
@@ -3,7 +3,7 @@
3
3
  module ActiveModel
4
4
  # = Active \Model \Conversion
5
5
  #
6
- # Handles default conversions: to_model, to_key, to_param, and to_partial_path.
6
+ # Handles default conversions: #to_model, #to_key, #to_param, and #to_partial_path.
7
7
  #
8
8
  # Let's take for example this non-persisted object.
9
9
  #
@@ -247,16 +247,23 @@ module ActiveModel
247
247
 
248
248
  def initialize_dup(other) # :nodoc:
249
249
  super
250
+ @mutations_from_database = nil
251
+ end
252
+
253
+ def init_attributes(other) # :nodoc:
254
+ attrs = super
250
255
  if self.class.respond_to?(:_default_attributes)
251
- @attributes = self.class._default_attributes.map do |attr|
252
- attr.with_value_from_user(@attributes.fetch_value(attr.name))
256
+ self.class._default_attributes.map do |attr|
257
+ attr.with_value_from_user(attrs.fetch_value(attr.name))
253
258
  end
259
+ else
260
+ attrs
254
261
  end
255
- @mutations_from_database = nil
256
262
  end
257
263
 
258
264
  def as_json(options = {}) # :nodoc:
259
- options[:except] = [*options[:except], "mutations_from_database", "mutations_before_last_save"]
265
+ except = [*options[:except], "mutations_from_database", "mutations_before_last_save"]
266
+ options = options.merge except: except
260
267
  super(options)
261
268
  end
262
269
 
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/class/attribute"
4
3
 
5
4
  module ActiveModel
6
5
  # = Active \Model \Error
@@ -83,7 +82,7 @@ module ActiveModel
83
82
  defaults << :"#{i18n_scope}.errors.messages.#{type}"
84
83
 
85
84
  catch(:exception) do
86
- translation = I18n.translate(defaults.first, **options.merge(default: defaults.drop(1), throw: true))
85
+ translation = I18n.translate(defaults.first, **options, default: defaults.drop(1), throw: true)
87
86
  return translation unless translation.nil?
88
87
  end unless options[:message]
89
88
  else
@@ -205,4 +204,6 @@ module ActiveModel
205
204
  [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)]
206
205
  end
207
206
  end
207
+
208
+ ActiveSupport.run_load_hooks(:active_model_error, Error)
208
209
  end
@@ -5,7 +5,6 @@ require "active_support/core_ext/string/inflections"
5
5
  require "active_support/core_ext/object/deep_dup"
6
6
  require "active_model/error"
7
7
  require "active_model/nested_error"
8
- require "forwardable"
9
8
 
10
9
  module ActiveModel
11
10
  # = Active \Model \Errors
@@ -61,8 +60,6 @@ module ActiveModel
61
60
  class Errors
62
61
  include Enumerable
63
62
 
64
- extend Forwardable
65
-
66
63
  ##
67
64
  # :method: each
68
65
  #
@@ -100,7 +97,7 @@ module ActiveModel
100
97
  #
101
98
  # Returns number of errors.
102
99
 
103
- def_delegators :@errors, :each, :clear, :empty?, :size, :uniq!
100
+ delegate :each, :clear, :empty?, :size, :uniq!, to: :@errors
104
101
 
105
102
  # The actual array of +Error+ objects
106
103
  # This method is aliased to <tt>objects</tt>.
@@ -7,8 +7,8 @@ module ActiveModel
7
7
  end
8
8
 
9
9
  module VERSION
10
- MAJOR = 7
11
- MINOR = 2
10
+ MAJOR = 8
11
+ MINOR = 1
12
12
  TINY = 3
13
13
  PRE = nil
14
14
 
@@ -30,7 +30,7 @@ module ActiveModel
30
30
  # of the model, and is used to a generate unique DOM id for the object.
31
31
  def test_to_key
32
32
  assert_respond_to model, :to_key
33
- def model.persisted?() false end
33
+ def_method(model, :persisted?) { false }
34
34
  assert model.to_key.nil?, "to_key should return nil when `persisted?` returns false"
35
35
  end
36
36
 
@@ -45,8 +45,8 @@ module ActiveModel
45
45
  # any of the possible implementation strategies on the implementer.
46
46
  def test_to_param
47
47
  assert_respond_to model, :to_param
48
- def model.to_key() [1] end
49
- def model.persisted?() false end
48
+ def_method(model, :to_key) { [1] }
49
+ def_method(model, :persisted?) { false }
50
50
  assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false"
51
51
  end
52
52
 
@@ -105,6 +105,10 @@ module ActiveModel
105
105
  end
106
106
 
107
107
  private
108
+ def def_method(receiver, name, &block)
109
+ ::Object.instance_method(:define_singleton_method).bind_call(receiver, name, &block)
110
+ end
111
+
108
112
  def model
109
113
  assert_respond_to @model, :to_model
110
114
  @model.to_model
@@ -3,7 +3,6 @@
3
3
  require "active_support/core_ext/hash/except"
4
4
  require "active_support/core_ext/module/introspection"
5
5
  require "active_support/core_ext/module/redefine_method"
6
- require "active_support/core_ext/module/delegation"
7
6
 
8
7
  module ActiveModel
9
8
  class Name
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_model/error"
4
- require "forwardable"
5
4
 
6
5
  module ActiveModel
7
6
  class NestedError < Error
@@ -16,7 +15,6 @@ module ActiveModel
16
15
 
17
16
  attr_reader :inner_error
18
17
 
19
- extend Forwardable
20
- def_delegators :@inner_error, :message
18
+ delegate :message, to: :@inner_error
21
19
  end
22
20
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_model"
4
3
  require "rails"
4
+ require "active_model"
5
5
 
6
6
  module ActiveModel
7
7
  class Railtie < Rails::Railtie # :nodoc:
@@ -14,11 +14,15 @@ module ActiveModel
14
14
  end
15
15
 
16
16
  initializer "active_model.secure_password" do
17
- ActiveModel::SecurePassword.min_cost = Rails.env.test?
17
+ ActiveSupport.on_load(:active_model_secure_password) do
18
+ ActiveModel::SecurePassword.min_cost = Rails.env.test?
19
+ end
18
20
  end
19
21
 
20
- initializer "active_model.i18n_customize_full_message" do
21
- ActiveModel::Error.i18n_customize_full_message = config.active_model.delete(:i18n_customize_full_message) || false
22
+ initializer "active_model.i18n_customize_full_message" do |app|
23
+ ActiveSupport.on_load(:active_model_error) do
24
+ ActiveModel::Error.i18n_customize_full_message = app.config.active_model.i18n_customize_full_message || false
25
+ end
22
26
  end
23
27
  end
24
28
  end