activemodel 7.2.3.1 → 8.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 +68 -45
- data/lib/active_model/attribute/user_provided_default.rb +10 -0
- data/lib/active_model/attribute.rb +9 -1
- data/lib/active_model/attribute_assignment.rb +22 -1
- data/lib/active_model/attribute_methods.rb +14 -6
- data/lib/active_model/attribute_mutation_tracker.rb +5 -5
- data/lib/active_model/attribute_set.rb +1 -1
- data/lib/active_model/attributes/normalization.rb +202 -0
- data/lib/active_model/conversion.rb +1 -1
- data/lib/active_model/dirty.rb +12 -5
- data/lib/active_model/error.rb +3 -2
- data/lib/active_model/errors.rb +3 -5
- data/lib/active_model/gem_version.rb +3 -3
- data/lib/active_model/lint.rb +7 -3
- data/lib/active_model/naming.rb +0 -1
- data/lib/active_model/nested_error.rb +1 -3
- data/lib/active_model/railtie.rb +8 -4
- data/lib/active_model/secure_password.rb +60 -3
- data/lib/active_model/serialization.rb +23 -21
- data/lib/active_model/serializers/json.rb +1 -1
- data/lib/active_model/translation.rb +8 -3
- data/lib/active_model/type/big_integer.rb +21 -0
- data/lib/active_model/type/boolean.rb +1 -0
- data/lib/active_model/type/date.rb +1 -0
- data/lib/active_model/type/date_time.rb +10 -5
- data/lib/active_model/type/decimal.rb +2 -1
- data/lib/active_model/type/float.rb +1 -0
- data/lib/active_model/type/helpers/immutable.rb +13 -0
- data/lib/active_model/type/helpers/time_value.rb +20 -44
- data/lib/active_model/type/helpers.rb +1 -0
- data/lib/active_model/type/immutable_string.rb +2 -0
- data/lib/active_model/type/integer.rb +43 -19
- data/lib/active_model/type/string.rb +4 -0
- data/lib/active_model/type/value.rb +4 -4
- data/lib/active_model/validations/acceptance.rb +1 -1
- data/lib/active_model/validations/callbacks.rb +10 -0
- data/lib/active_model/validations/validates.rb +7 -2
- data/lib/active_model/validations.rb +56 -31
- data/lib/active_model.rb +6 -0
- metadata +10 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 92052b32ecadeef71291013540d31e5322d5915f8423ab502a44fe509f0681e3
|
|
4
|
+
data.tar.gz: cb798f78fc73bddb96e58dff775e756a303f70bec5b0ce06cf292927d185ffe4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f247ec435c923e63791c49c58a77ca6cc0d722dabdef8fdc34065034b13e3eea7abca8e7b5d6c1f271471672b062bb4d089ca954133ac005b3635445b59e79a6
|
|
7
|
+
data.tar.gz: 3aec11a3ad206d062f9842a36672d046d75df201f849648886b1e5e2e4380fe1c794df56622261e873d92f803b3da52850077e144c404def49dc0730d3b9f802
|
data/CHANGELOG.md
CHANGED
|
@@ -1,93 +1,116 @@
|
|
|
1
|
-
## Rails
|
|
1
|
+
## Rails 8.1.4.1 (November 03, 2026) ##
|
|
2
2
|
|
|
3
3
|
* No changes.
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
## Rails
|
|
6
|
+
## Rails 8.1.4 (September 24, 2026) ##
|
|
7
7
|
|
|
8
|
-
* Fix `
|
|
8
|
+
* Fix `ActiveModel::Errors#import` mutating the override options hash passed to it.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
*Kenta Ishizaki*
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
* Fix `normalizes` not detecting in-place changes for attributes whose database
|
|
13
|
+
cast type differs from the attribute type (e.g. JSON columns), causing
|
|
14
|
+
normalization to be skipped on validation.
|
|
13
15
|
|
|
14
|
-
*
|
|
16
|
+
*Chedli Bourguiba*
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
* Fix `alias_attribute` accumulating duplicate entries in `aliases_by_attribute_name`
|
|
19
|
+
when called multiple times with the same arguments.
|
|
17
20
|
|
|
18
|
-
*
|
|
21
|
+
*Nicholas Jakobsen*
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
* Fix `normalizes` re-applying normalizations on every validation of an
|
|
24
|
+
unpersisted record, and speed up validation of normalized attributes.
|
|
21
25
|
|
|
26
|
+
The in-place mutation check re-ran the normalizer on every `valid?` of an
|
|
27
|
+
unpersisted record: wasteful for idempotent normalizers and compounded the
|
|
28
|
+
result for non-idempotent ones. Normalizations are now re-applied only on a
|
|
29
|
+
genuine in-place mutation.
|
|
22
30
|
|
|
23
|
-
|
|
31
|
+
*Yaroslav Markin*
|
|
24
32
|
|
|
25
|
-
*
|
|
33
|
+
* Limit the size of strings `ActiveModel::Type::Integer` will coerce with `to_i`.
|
|
26
34
|
|
|
35
|
+
Calling `to_i` on very long strings can take a long time and could be used as
|
|
36
|
+
a DoS vector. Integer casting now only considers the first `_limit * 4` bytes
|
|
37
|
+
of a string (16 bytes for a default 4-byte integer, 32 bytes for an 8-byte
|
|
38
|
+
bigint), which is enough to hold the maximum representable value plus a sign
|
|
39
|
+
or a short slug suffix.
|
|
27
40
|
|
|
28
|
-
|
|
41
|
+
*Aaron Patterson*, *Jean Boussier*
|
|
29
42
|
|
|
30
|
-
* No changes.
|
|
31
43
|
|
|
44
|
+
## Rails 8.1.3.1 (July 29, 2026) ##
|
|
32
45
|
|
|
33
|
-
|
|
46
|
+
* No changes.
|
|
34
47
|
|
|
35
|
-
* Fix regression in `alias_attribute` to work with user defined methods.
|
|
36
48
|
|
|
37
|
-
|
|
49
|
+
## Rails 8.1.3 (March 24, 2026) ##
|
|
38
50
|
|
|
39
|
-
|
|
40
|
-
class Person
|
|
41
|
-
include ActiveModel::AttributeMethods
|
|
51
|
+
* Fix Ruby 4.0 delegator warning when calling inspect on attributes.
|
|
42
52
|
|
|
43
|
-
|
|
44
|
-
attr_accessor :name
|
|
53
|
+
*Hammad Khan*
|
|
45
54
|
|
|
46
|
-
|
|
47
|
-
end
|
|
55
|
+
* Fix `NoMethodError` when deserialising `Type::Integer` objects marshalled under Rails 8.0.
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
The performance optimisation that replaced `@range` with `@max`/`@min`
|
|
58
|
+
broke Marshal compatibility. Objects serialised under 8.0 (with `@range`)
|
|
59
|
+
and deserialised under 8.1 (expecting `@max`/`@min`) would crash with
|
|
60
|
+
`undefined method '<=' for nil` because `Marshal.load` restores instance
|
|
61
|
+
variables without calling `initialize`.
|
|
51
62
|
|
|
52
|
-
*
|
|
63
|
+
*Edward Woodcock*
|
|
53
64
|
|
|
54
65
|
|
|
55
|
-
## Rails
|
|
66
|
+
## Rails 8.1.2.1 (March 23, 2026) ##
|
|
56
67
|
|
|
57
68
|
* No changes.
|
|
58
69
|
|
|
59
70
|
|
|
60
|
-
## Rails
|
|
71
|
+
## Rails 8.1.2 (January 08, 2026) ##
|
|
61
72
|
|
|
62
73
|
* No changes.
|
|
63
74
|
|
|
64
75
|
|
|
65
|
-
## Rails
|
|
76
|
+
## Rails 8.1.1 (October 28, 2025) ##
|
|
66
77
|
|
|
67
78
|
* No changes.
|
|
68
79
|
|
|
69
80
|
|
|
70
|
-
## Rails
|
|
81
|
+
## Rails 8.1.0 (October 22, 2025) ##
|
|
82
|
+
|
|
83
|
+
* Add `reset_token: { expires_in: ... }` option to `has_secure_password`.
|
|
84
|
+
|
|
85
|
+
Allows configuring the expiry duration of password reset tokens (default remains 15 minutes for backwards compatibility).
|
|
71
86
|
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
```ruby
|
|
88
|
+
has_secure_password reset_token: { expires_in: 1.hour }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
*Jevin Sew*, *Abeid Ahmed*
|
|
92
|
+
|
|
93
|
+
* Add `except_on:` option for validation callbacks.
|
|
74
94
|
|
|
75
|
-
*
|
|
95
|
+
*Ben Sheldon*
|
|
76
96
|
|
|
77
|
-
*
|
|
78
|
-
`ActiveModel::Attributes` will now provide this method. This method behaves
|
|
79
|
-
the same for Active Model as it does for Active Record.
|
|
97
|
+
* Backport `ActiveRecord::Normalization` to `ActiveModel::Attributes::Normalization`
|
|
80
98
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
99
|
+
```ruby
|
|
100
|
+
class User
|
|
101
|
+
include ActiveModel::Attributes
|
|
102
|
+
include ActiveModel::Attributes::Normalization
|
|
103
|
+
|
|
104
|
+
attribute :email, :string
|
|
84
105
|
|
|
85
|
-
|
|
86
|
-
|
|
106
|
+
normalizes :email, with: -> email { email.strip.downcase }
|
|
107
|
+
end
|
|
87
108
|
|
|
88
|
-
|
|
89
|
-
|
|
109
|
+
user = User.new
|
|
110
|
+
user.email = " CRUISE-CONTROL@EXAMPLE.COM\n"
|
|
111
|
+
user.email # => "cruise-control@example.com"
|
|
112
|
+
```
|
|
90
113
|
|
|
91
|
-
*
|
|
114
|
+
*Sean Doyle*
|
|
92
115
|
|
|
93
|
-
Please check [
|
|
116
|
+
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
|
-
|
|
74
|
+
attribute_writer_missing(k.to_s, v)
|
|
54
75
|
end
|
|
55
76
|
end
|
|
56
77
|
end
|
|
@@ -204,7 +204,7 @@ module ActiveModel
|
|
|
204
204
|
old_name = old_name.to_s
|
|
205
205
|
new_name = new_name.to_s
|
|
206
206
|
self.attribute_aliases = attribute_aliases.merge(new_name => old_name)
|
|
207
|
-
aliases_by_attribute_name[old_name]
|
|
207
|
+
aliases_by_attribute_name[old_name] |= [new_name]
|
|
208
208
|
eagerly_generate_alias_attribute_methods(new_name, old_name)
|
|
209
209
|
end
|
|
210
210
|
|
|
@@ -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
|
|
325
|
-
# defined in
|
|
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
|
|
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
|
|
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
|
|
@@ -521,10 +521,18 @@ module ActiveModel
|
|
|
521
521
|
__send__(match.proxy_target, match.attr_name, ...)
|
|
522
522
|
end
|
|
523
523
|
|
|
524
|
+
##
|
|
525
|
+
# :method: respond_to_without_attributes?
|
|
526
|
+
# :call-seq:
|
|
527
|
+
# respond_to_without_attributes?(method, include_private_methods = false)
|
|
528
|
+
#
|
|
529
|
+
# Checks whether the object responds to +method+ without searching the
|
|
530
|
+
# attributes.
|
|
531
|
+
define_method(:respond_to_without_attributes?, Kernel.instance_method(:respond_to?))
|
|
532
|
+
|
|
524
533
|
# A +Person+ instance with a +name+ attribute can ask
|
|
525
534
|
# <tt>person.respond_to?(:name)</tt>, <tt>person.respond_to?(:name=)</tt>,
|
|
526
535
|
# and <tt>person.respond_to?(:name?)</tt> which will all return +true+.
|
|
527
|
-
alias :respond_to_without_attributes? :respond_to?
|
|
528
536
|
def respond_to?(method, include_private_methods = false)
|
|
529
537
|
if super
|
|
530
538
|
true
|
|
@@ -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(
|
|
19
|
+
attr_names.each_with_object(ActiveSupport::HashWithIndifferentAccess.new) do |attr_name, result|
|
|
20
20
|
if changed?(attr_name)
|
|
21
|
-
result
|
|
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(
|
|
27
|
+
attr_names.each_with_object(ActiveSupport::HashWithIndifferentAccess.new) do |attr_name, result|
|
|
28
28
|
if change = change_to_attribute(attr_name)
|
|
29
|
-
result.
|
|
29
|
+
result.store(attr_name, change, convert_value: false)
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
attribute = @attributes[name.to_s]
|
|
143
|
+
normalize_attribute(name) if normalized_attribute_changed_in_place?(attribute)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def normalized_attribute_changed_in_place?(attribute)
|
|
148
|
+
attribute.changed_in_place? && attribute.value != attribute.type_cast(attribute.value_before_type_cast)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
class NormalizedValueType < DelegateClass(ActiveModel::Type::Value) # :nodoc:
|
|
152
|
+
include ActiveModel::Type::SerializeCastValue
|
|
153
|
+
|
|
154
|
+
attr_reader :cast_type, :normalizer, :normalize_nil
|
|
155
|
+
alias :normalize_nil? :normalize_nil
|
|
156
|
+
|
|
157
|
+
def initialize(cast_type:, normalizer:, normalize_nil:)
|
|
158
|
+
@cast_type = cast_type
|
|
159
|
+
@normalizer = normalizer
|
|
160
|
+
@normalize_nil = normalize_nil
|
|
161
|
+
super(cast_type)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def cast(value)
|
|
165
|
+
normalize(super(value))
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def serialize(value)
|
|
169
|
+
serialize_cast_value(cast(value))
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def serialize_cast_value(value)
|
|
173
|
+
ActiveModel::Type::SerializeCastValue.serialize(cast_type, value)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def ==(other)
|
|
177
|
+
self.class == other.class &&
|
|
178
|
+
normalize_nil? == other.normalize_nil? &&
|
|
179
|
+
normalizer == other.normalizer &&
|
|
180
|
+
cast_type == other.cast_type
|
|
181
|
+
end
|
|
182
|
+
alias eql? ==
|
|
183
|
+
|
|
184
|
+
def hash
|
|
185
|
+
[self.class, cast_type, normalizer, normalize_nil?].hash
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
define_method(:inspect, Kernel.instance_method(:inspect))
|
|
189
|
+
|
|
190
|
+
private
|
|
191
|
+
# Prevent Ruby 4.0 "delegator does not forward private method" warning.
|
|
192
|
+
# Kernel#inspect calls instance_variables_to_inspect which, without this,
|
|
193
|
+
# triggers Delegator#respond_to_missing? for a private method.
|
|
194
|
+
define_method(:instance_variables_to_inspect, Kernel.instance_method(:instance_variables))
|
|
195
|
+
|
|
196
|
+
def normalize(value)
|
|
197
|
+
normalizer.call(value) unless value.nil? && !normalize_nil?
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
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
|
#
|
data/lib/active_model/dirty.rb
CHANGED
|
@@ -230,7 +230,7 @@ module ActiveModel
|
|
|
230
230
|
#
|
|
231
231
|
# This method is generated for each attribute.
|
|
232
232
|
#
|
|
233
|
-
# Clears
|
|
233
|
+
# Clears the current changes of the attribute.
|
|
234
234
|
#
|
|
235
235
|
# person = Person.new(name: 'Chris')
|
|
236
236
|
# person.name = 'Jason'
|
|
@@ -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
|
-
|
|
252
|
-
attr.with_value_from_user(
|
|
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
|
-
|
|
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
|
|
data/lib/active_model/error.rb
CHANGED
|
@@ -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
|
|
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
|
data/lib/active_model/errors.rb
CHANGED
|
@@ -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
|
-
|
|
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>.
|
|
@@ -152,6 +149,7 @@ module ActiveModel
|
|
|
152
149
|
# * +:attribute+ - Override the attribute the error belongs to.
|
|
153
150
|
# * +:type+ - Override type of the error.
|
|
154
151
|
def import(error, override_options = {})
|
|
152
|
+
override_options = override_options.dup
|
|
155
153
|
[:attribute, :type].each do |key|
|
|
156
154
|
if override_options.key?(key)
|
|
157
155
|
override_options[key] = override_options[key].to_sym
|
|
@@ -314,7 +312,7 @@ module ActiveModel
|
|
|
314
312
|
#
|
|
315
313
|
# person.errors.add(:name, :too_long, count: 25)
|
|
316
314
|
# person.errors.messages
|
|
317
|
-
# # => ["is too long (maximum is 25 characters)"]
|
|
315
|
+
# # => {:name=>["is too long (maximum is 25 characters)"]}
|
|
318
316
|
#
|
|
319
317
|
# If +type+ is a proc, it will be called, allowing for things like
|
|
320
318
|
# <tt>Time.now</tt> to be used within an error.
|