activemodel 5.2.8.1 → 6.1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +91 -97
  3. data/MIT-LICENSE +1 -2
  4. data/README.rdoc +6 -4
  5. data/lib/active_model/attribute/user_provided_default.rb +1 -2
  6. data/lib/active_model/attribute.rb +21 -21
  7. data/lib/active_model/attribute_assignment.rb +4 -6
  8. data/lib/active_model/attribute_methods.rb +117 -40
  9. data/lib/active_model/attribute_mutation_tracker.rb +90 -33
  10. data/lib/active_model/attribute_set/builder.rb +81 -16
  11. data/lib/active_model/attribute_set/yaml_encoder.rb +1 -2
  12. data/lib/active_model/attribute_set.rb +20 -28
  13. data/lib/active_model/attributes.rb +65 -44
  14. data/lib/active_model/callbacks.rb +11 -9
  15. data/lib/active_model/conversion.rb +1 -1
  16. data/lib/active_model/dirty.rb +51 -101
  17. data/lib/active_model/error.rb +207 -0
  18. data/lib/active_model/errors.rb +347 -155
  19. data/lib/active_model/gem_version.rb +3 -3
  20. data/lib/active_model/lint.rb +1 -1
  21. data/lib/active_model/naming.rb +22 -7
  22. data/lib/active_model/nested_error.rb +22 -0
  23. data/lib/active_model/railtie.rb +6 -0
  24. data/lib/active_model/secure_password.rb +55 -55
  25. data/lib/active_model/serialization.rb +9 -7
  26. data/lib/active_model/serializers/json.rb +17 -9
  27. data/lib/active_model/translation.rb +1 -1
  28. data/lib/active_model/type/big_integer.rb +0 -1
  29. data/lib/active_model/type/binary.rb +1 -1
  30. data/lib/active_model/type/boolean.rb +0 -1
  31. data/lib/active_model/type/date.rb +0 -5
  32. data/lib/active_model/type/date_time.rb +3 -8
  33. data/lib/active_model/type/decimal.rb +0 -1
  34. data/lib/active_model/type/float.rb +2 -3
  35. data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +14 -6
  36. data/lib/active_model/type/helpers/numeric.rb +17 -6
  37. data/lib/active_model/type/helpers/time_value.rb +37 -15
  38. data/lib/active_model/type/helpers/timezone.rb +1 -1
  39. data/lib/active_model/type/immutable_string.rb +14 -11
  40. data/lib/active_model/type/integer.rb +15 -18
  41. data/lib/active_model/type/registry.rb +17 -19
  42. data/lib/active_model/type/string.rb +12 -3
  43. data/lib/active_model/type/time.rb +1 -6
  44. data/lib/active_model/type/value.rb +9 -2
  45. data/lib/active_model/type.rb +3 -2
  46. data/lib/active_model/validations/absence.rb +2 -2
  47. data/lib/active_model/validations/acceptance.rb +34 -27
  48. data/lib/active_model/validations/callbacks.rb +15 -16
  49. data/lib/active_model/validations/clusivity.rb +6 -3
  50. data/lib/active_model/validations/confirmation.rb +4 -4
  51. data/lib/active_model/validations/exclusion.rb +1 -1
  52. data/lib/active_model/validations/format.rb +2 -3
  53. data/lib/active_model/validations/inclusion.rb +2 -2
  54. data/lib/active_model/validations/length.rb +3 -3
  55. data/lib/active_model/validations/numericality.rb +58 -44
  56. data/lib/active_model/validations/presence.rb +1 -1
  57. data/lib/active_model/validations/validates.rb +7 -6
  58. data/lib/active_model/validations.rb +6 -9
  59. data/lib/active_model/validator.rb +8 -3
  60. data/lib/active_model.rb +2 -1
  61. metadata +13 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7f6668533dd90a9b8c479fc71940522acf40284b4604e8d689d637129e6795a
4
- data.tar.gz: 9a519747c7fc021167852457a0e03056bc68351001f61d366d32386afe584d11
3
+ metadata.gz: ae24a33b1bbfa6eb1e4c5e55f14a7bb92631f3c16b894fe516e01507f3a3354f
4
+ data.tar.gz: fa7badebe0d2d6213db0e1122bb94fbff72cbc11fe1e8bce870ed1b340a8f31b
5
5
  SHA512:
6
- metadata.gz: 0f5a6ba7be9164544db261136c14337a256ad42cf9776e8364b745a6ff2c90f32f5e7a53e24ddb6032971de7e6bca59cf27808dea11d060ab5c3b20b92baef9d
7
- data.tar.gz: b63d763a8de2b5438801b3698f33522cd68f37c22de007d752a4d1d9250b6f2ed31aab780cc07c4179670c863bb517286b85dd77ffb9cdb1624cbf5cbbe3935b
6
+ metadata.gz: dd715570f3cba8ebe323210ac537b2ca5852cf1827cd791cd8b023f01d848047d0b0e52d205a99a49f310ea81fb9accbc404de5d7d53f2c629a83cd7a5ae7d7f
7
+ data.tar.gz: 80284508ce715c8335715acff27814c89e40036d2c107f3684cf97741a50e7cc188b25636ee5c3f01ea4ec0c74c34367aa4eb3daa1ebc7a93ad2946c568670a9
data/CHANGELOG.md CHANGED
@@ -1,198 +1,192 @@
1
- ## Rails 5.2.8.1 (July 12, 2022) ##
1
+ ## Rails 6.1.6.1 (July 12, 2022) ##
2
2
 
3
3
  * No changes.
4
4
 
5
5
 
6
- ## Rails 5.2.8 (May 09, 2022) ##
6
+ ## Rails 6.1.5.1 (April 26, 2022) ##
7
7
 
8
8
  * No changes.
9
9
 
10
10
 
11
- ## Rails 5.2.7.1 (April 26, 2022) ##
11
+ ## Rails 6.1.5 (March 09, 2022) ##
12
12
 
13
- * No changes.
13
+ * Clear secure password cache if password is set to `nil`
14
14
 
15
+ Before:
15
16
 
16
- ## Rails 5.2.7 (March 10, 2022) ##
17
+ user.password = 'something'
18
+ user.password = nil
17
19
 
18
- * No changes.
20
+ user.password # => 'something'
19
21
 
22
+ Now:
20
23
 
21
- ## Rails 5.2.6.3 (March 08, 2022) ##
24
+ user.password = 'something'
25
+ user.password = nil
22
26
 
23
- * No changes.
27
+ user.password # => nil
24
28
 
29
+ *Markus Doits*
25
30
 
26
- ## Rails 5.2.6.2 (February 11, 2022) ##
27
-
28
- * No changes.
31
+ * Fix delegation in `ActiveModel::Type::Registry#lookup` and `ActiveModel::Type.lookup`
29
32
 
33
+ Passing a last positional argument `{}` would be incorrectly considered as keyword argument.
30
34
 
31
- ## Rails 5.2.6.1 (February 11, 2022) ##
35
+ *Benoit Daloze*
32
36
 
33
- * No changes.
37
+ * Fix `to_json` after `changes_applied` for `ActiveModel::Dirty` object.
34
38
 
35
-
36
- ## Rails 5.2.6 (May 05, 2021) ##
37
-
38
- * No changes.
39
+ *Ryuta Kamizono*
39
40
 
40
41
 
41
- ## Rails 5.2.5 (March 26, 2021) ##
42
+ ## Rails 6.1.4.7 (March 08, 2022) ##
42
43
 
43
44
  * No changes.
44
45
 
45
46
 
46
- ## Rails 5.2.4.6 (May 05, 2021) ##
47
+ ## Rails 6.1.4.6 (February 11, 2022) ##
47
48
 
48
49
  * No changes.
49
50
 
50
51
 
51
- ## Rails 5.2.4.5 (February 10, 2021) ##
52
+ ## Rails 6.1.4.5 (February 11, 2022) ##
52
53
 
53
54
  * No changes.
54
55
 
55
56
 
56
- ## Rails 5.2.4.4 (September 09, 2020) ##
57
+ ## Rails 6.1.4.4 (December 15, 2021) ##
57
58
 
58
59
  * No changes.
59
60
 
60
61
 
61
- ## Rails 5.2.4.3 (May 18, 2020) ##
62
+ ## Rails 6.1.4.3 (December 14, 2021) ##
62
63
 
63
64
  * No changes.
64
65
 
65
66
 
66
- ## Rails 5.2.4.2 (March 19, 2020) ##
67
+ ## Rails 6.1.4.2 (December 14, 2021) ##
67
68
 
68
69
  * No changes.
69
70
 
70
71
 
71
- ## Rails 5.2.4.1 (December 18, 2019) ##
72
+ ## Rails 6.1.4.1 (August 19, 2021) ##
72
73
 
73
74
  * No changes.
74
75
 
75
76
 
76
- ## Rails 5.2.4 (November 27, 2019) ##
77
-
78
- * Type cast falsy boolean symbols on boolean attribute as false.
79
-
80
- Fixes #35676.
81
-
82
- *Ryuta Kamizono*
77
+ ## Rails 6.1.4 (June 24, 2021) ##
83
78
 
79
+ * Fix `to_json` for `ActiveModel::Dirty` object.
84
80
 
85
- ## Rails 5.2.3 (March 27, 2019) ##
81
+ Exclude +mutations_from_database+ attribute from json as it lead to recursion.
86
82
 
87
- * Fix date value when casting a multiparameter date hash to not convert
88
- from Gregorian date to Julian date.
89
-
90
- Before:
83
+ *Anil Maurya*
91
84
 
92
- Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
93
- => #<Day id: nil, day: "0001-01-03", created_at: nil, updated_at: nil>
94
85
 
95
- After:
86
+ ## Rails 6.1.3.2 (May 05, 2021) ##
96
87
 
97
- Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
98
- => #<Day id: nil, day: "0001-01-01", created_at: nil, updated_at: nil>
99
-
100
- Fixes #28521.
88
+ * No changes.
101
89
 
102
- *Sayan Chakraborty*
103
90
 
104
- * Fix numericality equality validation of `BigDecimal` and `Float`
105
- by casting to `BigDecimal` on both ends of the validation.
91
+ ## Rails 6.1.3.1 (March 26, 2021) ##
106
92
 
107
- *Gannon McGibbon*
93
+ * No changes.
108
94
 
109
95
 
110
- ## Rails 5.2.2.1 (March 11, 2019) ##
96
+ ## Rails 6.1.3 (February 17, 2021) ##
111
97
 
112
98
  * No changes.
113
99
 
114
100
 
115
- ## Rails 5.2.2 (December 04, 2018) ##
101
+ ## Rails 6.1.2.1 (February 10, 2021) ##
116
102
 
117
- * Fix numericality validator to still use value before type cast except Active Record.
118
-
119
- Fixes #33651, #33686.
120
-
121
- *Ryuta Kamizono*
103
+ * No changes.
122
104
 
123
105
 
124
- ## Rails 5.2.1.1 (November 27, 2018) ##
106
+ ## Rails 6.1.2 (February 09, 2021) ##
125
107
 
126
108
  * No changes.
127
109
 
128
110
 
129
- ## Rails 5.2.1 (August 07, 2018) ##
111
+ ## Rails 6.1.1 (January 07, 2021) ##
130
112
 
131
113
  * No changes.
132
114
 
133
115
 
134
- ## Rails 5.2.0 (April 09, 2018) ##
116
+ ## Rails 6.1.0 (December 09, 2020) ##
117
+
118
+ * Pass in `base` instead of `base_class` to Error.human_attribute_name
135
119
 
136
- * Do not lose all multiple `:includes` with options in serialization.
120
+ This is useful in cases where the `human_attribute_name` method depends
121
+ on other attributes' values of the class under validation to derive what the
122
+ attribute name should be.
137
123
 
138
- *Mike Mangino*
124
+ *Filipe Sabella*
139
125
 
140
- * Models using the attributes API with a proc default can now be marshalled.
126
+ * Deprecate marshalling load from legacy attributes format.
141
127
 
142
- Fixes #31216.
128
+ *Ryuta Kamizono*
143
129
 
144
- *Sean Griffin*
130
+ * `*_previously_changed?` accepts `:from` and `:to` keyword arguments like `*_changed?`.
145
131
 
146
- * Fix to working before/after validation callbacks on multiple contexts.
132
+ topic.update!(status: :archived)
133
+ topic.status_previously_changed?(from: "active", to: "archived")
134
+ # => true
147
135
 
148
- *Yoshiyuki Hirano*
136
+ *George Claghorn*
149
137
 
150
- * Execute `ConfirmationValidator` validation when `_confirmation`'s value is `false`.
138
+ * Raise FrozenError when trying to write attributes that aren't backed by the database on an object that is frozen:
151
139
 
152
- *bogdanvlviv*
140
+ class Animal
141
+ include ActiveModel::Attributes
142
+ attribute :age
143
+ end
153
144
 
154
- * Allow passing a Proc or Symbol to length validator options.
145
+ animal = Animal.new
146
+ animal.freeze
147
+ animal.age = 25 # => FrozenError, "can't modify a frozen Animal"
155
148
 
156
- *Matt Rohrer*
149
+ *Josh Brody*
157
150
 
158
- * Add method `#merge!` for `ActiveModel::Errors`.
151
+ * Add `*_previously_was` attribute methods when dirty tracking. Example:
159
152
 
160
- *Jahfer Husain*
153
+ pirate.update(catchphrase: "Ahoy!")
154
+ pirate.previous_changes["catchphrase"] # => ["Thar She Blows!", "Ahoy!"]
155
+ pirate.catchphrase_previously_was # => "Thar She Blows!"
161
156
 
162
- * Fix regression in numericality validator when comparing Decimal and Float input
163
- values with more scale than the schema.
157
+ *DHH*
164
158
 
165
- *Bradley Priest*
159
+ * Encapsulate each validation error as an Error object.
166
160
 
167
- * Fix methods `#keys`, `#values` in `ActiveModel::Errors`.
161
+ The `ActiveModel`’s `errors` collection is now an array of these Error
162
+ objects, instead of messages/details hash.
168
163
 
169
- Change `#keys` to only return the keys that don't have empty messages.
164
+ For each of these `Error` object, its `message` and `full_message` methods
165
+ are for generating error messages. Its `details` method would return error’s
166
+ extra parameters, found in the original `details` hash.
170
167
 
171
- Change `#values` to only return the not empty values.
168
+ The change tries its best at maintaining backward compatibility, however
169
+ some edge cases won’t be covered, like `errors#first` will return `ActiveModel::Error` and manipulating
170
+ `errors.messages` and `errors.details` hashes directly will have no effect. Moving forward,
171
+ please convert those direct manipulations to use provided API methods instead.
172
+ Please note that `errors#add` now accepts `options` as keyword arguments instead of `Hash` which
173
+ introduced a change in Ruby 3 to [keyword arguments][kwargs-ann].
172
174
 
173
- Example:
175
+ [kwargs-ann]: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
174
176
 
175
- # Before
176
- person = Person.new
177
- person.errors.keys # => []
178
- person.errors.values # => []
179
- person.errors.messages # => {}
180
- person.errors[:name] # => []
181
- person.errors.messages # => {:name => []}
182
- person.errors.keys # => [:name]
183
- person.errors.values # => [[]]
177
+ The list of deprecated methods and their planned future behavioral changes at the next major release are:
184
178
 
185
- # After
186
- person = Person.new
187
- person.errors.keys # => []
188
- person.errors.values # => []
189
- person.errors.messages # => {}
190
- person.errors[:name] # => []
191
- person.errors.messages # => {:name => []}
192
- person.errors.keys # => []
193
- person.errors.values # => []
179
+ * `errors#slice!` will be removed.
180
+ * `errors#each` with the `key, value` two-arguments block will stop working, while the `error` single-argument block would return `Error` object.
181
+ * `errors#values` will be removed.
182
+ * `errors#keys` will be removed.
183
+ * `errors#to_xml` will be removed.
184
+ * `errors#to_h` will be removed, and can be replaced with `errors#to_hash`.
185
+ * Manipulating `errors` itself as a hash will have no effect (e.g. `errors[:foo] = 'bar'`).
186
+ * Manipulating the hash returned by `errors#messages` (e.g. `errors.messages[:foo] = 'bar'`) will have no effect.
187
+ * Manipulating the hash returned by `errors#details` (e.g. `errors.details[:foo].clear`) will have no effect.
194
188
 
195
- *bogdanvlviv*
189
+ *lulalala*
196
190
 
197
191
 
198
- Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activemodel/CHANGELOG.md) for previous changes.
192
+ Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/activemodel/CHANGELOG.md) for previous changes.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2004-2018 David Heinemeier Hansson
1
+ Copyright (c) 2004-2022 David Heinemeier Hansson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -18,4 +18,3 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
data/README.rdoc CHANGED
@@ -5,6 +5,8 @@ They allow for Action Pack helpers to interact with non-Active Record models,
5
5
  for example. Active Model also helps with building custom ORMs for use outside of
6
6
  the Rails framework.
7
7
 
8
+ You can read more about Active Model in the {Active Model Basics}[https://edgeguides.rubyonrails.org/active_model_basics.html] guide.
9
+
8
10
  Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
9
11
  interact with Action Pack helpers, it was required to either copy chunks of
10
12
  code from Rails, or monkey patch entire helpers to make them handle objects
@@ -198,7 +200,7 @@ behavior out of the box:
198
200
  attr_accessor :first_name, :last_name
199
201
 
200
202
  validates_each :first_name, :last_name do |record, attr, value|
201
- record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
203
+ record.errors.add attr, "starts with z." if value.start_with?("z")
202
204
  end
203
205
  end
204
206
 
@@ -239,7 +241,7 @@ The latest version of Active Model can be installed with RubyGems:
239
241
 
240
242
  Source code can be downloaded as part of the Rails project on GitHub
241
243
 
242
- * https://github.com/rails/rails/tree/5-2-stable/activemodel
244
+ * https://github.com/rails/rails/tree/main/activemodel
243
245
 
244
246
 
245
247
  == License
@@ -253,7 +255,7 @@ Active Model is released under the MIT license:
253
255
 
254
256
  API documentation is at:
255
257
 
256
- * http://api.rubyonrails.org
258
+ * https://api.rubyonrails.org
257
259
 
258
260
  Bug reports for the Ruby on Rails project can be filed here:
259
261
 
@@ -261,4 +263,4 @@ Bug reports for the Ruby on Rails project can be filed here:
261
263
 
262
264
  Feature requests should be discussed on the rails-core mailing list here:
263
265
 
264
- * https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-core
266
+ * https://discuss.rubyonrails.org/c/rubyonrails-core
@@ -44,8 +44,7 @@ module ActiveModel
44
44
  end
45
45
  end
46
46
 
47
- protected
48
-
47
+ private
49
48
  attr_reader :user_provided_value
50
49
  end
51
50
  end
@@ -5,16 +5,16 @@ require "active_support/core_ext/object/duplicable"
5
5
  module ActiveModel
6
6
  class Attribute # :nodoc:
7
7
  class << self
8
- def from_database(name, value, type)
9
- FromDatabase.new(name, value, type)
8
+ def from_database(name, value_before_type_cast, type, value = nil)
9
+ FromDatabase.new(name, value_before_type_cast, type, nil, value)
10
10
  end
11
11
 
12
- def from_user(name, value, type, original_attribute = nil)
13
- FromUser.new(name, value, type, original_attribute)
12
+ def from_user(name, value_before_type_cast, type, original_attribute = nil)
13
+ FromUser.new(name, value_before_type_cast, type, original_attribute)
14
14
  end
15
15
 
16
- def with_cast_value(name, value, type)
17
- WithCastValue.new(name, value, type)
16
+ def with_cast_value(name, value_before_type_cast, type)
17
+ WithCastValue.new(name, value_before_type_cast, type)
18
18
  end
19
19
 
20
20
  def null(name)
@@ -30,11 +30,12 @@ module ActiveModel
30
30
 
31
31
  # This method should not be called directly.
32
32
  # Use #from_database or #from_user
33
- def initialize(name, value_before_type_cast, type, original_attribute = nil)
33
+ def initialize(name, value_before_type_cast, type, original_attribute = nil, value = nil)
34
34
  @name = name
35
35
  @value_before_type_cast = value_before_type_cast
36
36
  @type = type
37
37
  @original_attribute = original_attribute
38
+ @value = value unless value.nil?
38
39
  end
39
40
 
40
41
  def value
@@ -132,20 +133,18 @@ module ActiveModel
132
133
  coder["value"] = value if defined?(@value)
133
134
  end
134
135
 
135
- protected
136
-
137
- attr_reader :original_attribute
138
- alias_method :assigned?, :original_attribute
139
-
140
- def original_value_for_database
141
- if assigned?
142
- original_attribute.original_value_for_database
143
- else
144
- _original_value_for_database
145
- end
136
+ def original_value_for_database
137
+ if assigned?
138
+ original_attribute.original_value_for_database
139
+ else
140
+ _original_value_for_database
146
141
  end
142
+ end
147
143
 
148
144
  private
145
+ attr_reader :original_attribute
146
+ alias :assigned? :original_attribute
147
+
149
148
  def initialize_dup(other)
150
149
  if defined?(@value) && @value.duplicable?
151
150
  @value = @value.dup
@@ -165,9 +164,10 @@ module ActiveModel
165
164
  type.deserialize(value)
166
165
  end
167
166
 
168
- def _original_value_for_database
169
- value_before_type_cast
170
- end
167
+ private
168
+ def _original_value_for_database
169
+ value_before_type_cast
170
+ end
171
171
  end
172
172
 
173
173
  class FromUser < Attribute # :nodoc:
@@ -26,19 +26,17 @@ module ActiveModel
26
26
  # cat.name # => 'Gorby'
27
27
  # cat.status # => 'sleeping'
28
28
  def assign_attributes(new_attributes)
29
- if !new_attributes.respond_to?(:stringify_keys)
30
- raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
29
+ unless new_attributes.respond_to?(:each_pair)
30
+ raise ArgumentError, "When assigning attributes, you must pass a hash as an argument, #{new_attributes.class} passed."
31
31
  end
32
32
  return if new_attributes.empty?
33
33
 
34
- attributes = new_attributes.stringify_keys
35
- _assign_attributes(sanitize_for_mass_assignment(attributes))
34
+ _assign_attributes(sanitize_for_mass_assignment(new_attributes))
36
35
  end
37
36
 
38
37
  alias attributes= assign_attributes
39
38
 
40
39
  private
41
-
42
40
  def _assign_attributes(attributes)
43
41
  attributes.each do |k, v|
44
42
  _assign_attribute(k, v)
@@ -50,7 +48,7 @@ module ActiveModel
50
48
  if respond_to?(setter)
51
49
  public_send(setter, v)
52
50
  else
53
- raise UnknownAttributeError.new(self, k)
51
+ raise UnknownAttributeError.new(self, k.to_s)
54
52
  end
55
53
  end
56
54
  end