activemodel 5.2.5 → 6.0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +160 -71
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -3
  5. data/lib/active_model/attribute/user_provided_default.rb +1 -2
  6. data/lib/active_model/attribute.rb +3 -4
  7. data/lib/active_model/attribute_assignment.rb +1 -2
  8. data/lib/active_model/attribute_methods.rb +56 -15
  9. data/lib/active_model/attribute_mutation_tracker.rb +88 -34
  10. data/lib/active_model/attribute_set/builder.rb +1 -3
  11. data/lib/active_model/attribute_set/yaml_encoder.rb +1 -2
  12. data/lib/active_model/attribute_set.rb +2 -12
  13. data/lib/active_model/attributes.rb +60 -35
  14. data/lib/active_model/callbacks.rb +10 -8
  15. data/lib/active_model/conversion.rb +1 -1
  16. data/lib/active_model/dirty.rb +36 -99
  17. data/lib/active_model/errors.rb +105 -21
  18. data/lib/active_model/gem_version.rb +4 -4
  19. data/lib/active_model/naming.rb +20 -5
  20. data/lib/active_model/railtie.rb +6 -0
  21. data/lib/active_model/secure_password.rb +47 -48
  22. data/lib/active_model/serialization.rb +0 -1
  23. data/lib/active_model/serializers/json.rb +10 -9
  24. data/lib/active_model/translation.rb +1 -1
  25. data/lib/active_model/type/big_integer.rb +0 -1
  26. data/lib/active_model/type/binary.rb +1 -1
  27. data/lib/active_model/type/boolean.rb +0 -1
  28. data/lib/active_model/type/date.rb +0 -5
  29. data/lib/active_model/type/date_time.rb +3 -8
  30. data/lib/active_model/type/decimal.rb +0 -1
  31. data/lib/active_model/type/float.rb +0 -3
  32. data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +4 -0
  33. data/lib/active_model/type/helpers/numeric.rb +9 -3
  34. data/lib/active_model/type/helpers/time_value.rb +17 -5
  35. data/lib/active_model/type/immutable_string.rb +0 -1
  36. data/lib/active_model/type/integer.rb +8 -20
  37. data/lib/active_model/type/registry.rb +11 -16
  38. data/lib/active_model/type/string.rb +2 -3
  39. data/lib/active_model/type/time.rb +1 -6
  40. data/lib/active_model/type/value.rb +0 -1
  41. data/lib/active_model/validations/absence.rb +1 -1
  42. data/lib/active_model/validations/acceptance.rb +33 -26
  43. data/lib/active_model/validations/callbacks.rb +0 -1
  44. data/lib/active_model/validations/clusivity.rb +1 -2
  45. data/lib/active_model/validations/confirmation.rb +2 -2
  46. data/lib/active_model/validations/format.rb +1 -2
  47. data/lib/active_model/validations/inclusion.rb +1 -1
  48. data/lib/active_model/validations/length.rb +1 -1
  49. data/lib/active_model/validations/numericality.rb +5 -4
  50. data/lib/active_model/validations/validates.rb +2 -3
  51. data/lib/active_model/validations.rb +0 -3
  52. data/lib/active_model/validator.rb +1 -2
  53. data/lib/active_model.rb +1 -1
  54. metadata +15 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9039d6e13061cd4e199a31b36d31d552a65dbd4ec37fff5030a08bfcbd3f787
4
- data.tar.gz: 94d3de0fcc02f8f23302e59fbfb9ec8e690c4bc796f90c13257c0d47f8421cd5
3
+ metadata.gz: 7ecbea618c3d687827eb845c727679477517de6982f358f141ac616366527c89
4
+ data.tar.gz: 145c510d7a4fa6f89a0a18ab250c1847a3473bdc0a8399ba26479c98caeec39c
5
5
  SHA512:
6
- metadata.gz: 45a41acc88fb4a97d513feb0356998fe1007a59b24b4f0e97801a1ca0f5def91c90afea459badbc50c2d20ff6054ebb862caf91f7e87f45f76cab8398793456f
7
- data.tar.gz: 333361f1821156c0ef7844c57f5557b60eb6dcdd4132c652093b7ac39b2e268e725f058285127e0c58d3090687282ff107f8a75ae36183b4feb9d89c8db11345
6
+ metadata.gz: d3531a1a0edad1393ec7ae41deaa3f732f436511df28fd30a5749b6420495094719843f2bec1f8dc6f6846cbb6c8d10a4e202783425f2f429e041639d0cb634f
7
+ data.tar.gz: bff8dbeb868911a4583342ff79e6a4723ebbe07d1b14d7da1674670bcb535104121f6297493a7269599412499c7eb177f81b2aacd9257896def60af3a401b5f0
data/CHANGELOG.md CHANGED
@@ -1,34 +1,84 @@
1
- ## Rails 5.2.5 (March 26, 2021) ##
1
+ ## Rails 6.0.4.1 (August 19, 2021) ##
2
2
 
3
3
  * No changes.
4
4
 
5
5
 
6
- ## Rails 5.2.4.5 (February 10, 2021) ##
6
+ ## Rails 6.0.4 (June 15, 2021) ##
7
7
 
8
8
  * No changes.
9
9
 
10
10
 
11
- ## Rails 5.2.4.4 (September 09, 2020) ##
11
+ ## Rails 6.0.3.7 (May 05, 2021) ##
12
12
 
13
13
  * No changes.
14
14
 
15
15
 
16
- ## Rails 5.2.4.3 (May 18, 2020) ##
16
+ ## Rails 6.0.3.6 (March 26, 2021) ##
17
17
 
18
18
  * No changes.
19
19
 
20
20
 
21
- ## Rails 5.2.4.2 (March 19, 2020) ##
21
+ ## Rails 6.0.3.5 (February 10, 2021) ##
22
22
 
23
23
  * No changes.
24
24
 
25
25
 
26
- ## Rails 5.2.4.1 (December 18, 2019) ##
26
+ ## Rails 6.0.3.4 (October 07, 2020) ##
27
27
 
28
28
  * No changes.
29
29
 
30
30
 
31
- ## Rails 5.2.4 (November 27, 2019) ##
31
+ ## Rails 6.0.3.3 (September 09, 2020) ##
32
+
33
+ * No changes.
34
+
35
+
36
+ ## Rails 6.0.3.2 (June 17, 2020) ##
37
+
38
+ * No changes.
39
+
40
+
41
+ ## Rails 6.0.3.1 (May 18, 2020) ##
42
+
43
+ * No changes.
44
+
45
+
46
+ ## Rails 6.0.3 (May 06, 2020) ##
47
+
48
+ * No changes.
49
+
50
+
51
+ ## Rails 6.0.2.2 (March 19, 2020) ##
52
+
53
+ * No changes.
54
+
55
+
56
+ ## Rails 6.0.2.1 (December 18, 2019) ##
57
+
58
+ * No changes.
59
+
60
+
61
+ ## Rails 6.0.2 (December 13, 2019) ##
62
+
63
+ * No changes.
64
+
65
+
66
+ ## Rails 6.0.1 (November 5, 2019) ##
67
+
68
+ * No changes.
69
+
70
+
71
+ ## Rails 6.0.0 (August 16, 2019) ##
72
+
73
+ * No changes.
74
+
75
+
76
+ ## Rails 6.0.0.rc2 (July 22, 2019) ##
77
+
78
+ * No changes.
79
+
80
+
81
+ ## Rails 6.0.0.rc1 (April 24, 2019) ##
32
82
 
33
83
  * Type cast falsy boolean symbols on boolean attribute as false.
34
84
 
@@ -36,8 +86,36 @@
36
86
 
37
87
  *Ryuta Kamizono*
38
88
 
89
+ * Change how validation error translation strings are fetched: The new behavior
90
+ will first try the more specific keys, including doing locale fallback, then try
91
+ the less specific ones.
92
+
93
+ For example, this is the order in which keys will now be tried for a `blank`
94
+ error on a `product`'s `title` attribute with current locale set to `en-US`:
95
+
96
+ en-US.activerecord.errors.models.product.attributes.title.blank
97
+ en-US.activerecord.errors.models.product.blank
98
+ en-US.activerecord.errors.messages.blank
99
+
100
+ en.activerecord.errors.models.product.attributes.title.blank
101
+ en.activerecord.errors.models.product.blank
102
+ en.activerecord.errors.messages.blank
39
103
 
40
- ## Rails 5.2.3 (March 27, 2019) ##
104
+ en-US.errors.attributes.title.blank
105
+ en-US.errors.messages.blank
106
+
107
+ en.errors.attributes.title.blank
108
+ en.errors.messages.blank
109
+
110
+ *Hugo Vacher*
111
+
112
+
113
+ ## Rails 6.0.0.beta3 (March 11, 2019) ##
114
+
115
+ * No changes.
116
+
117
+
118
+ ## Rails 6.0.0.beta2 (February 25, 2019) ##
41
119
 
42
120
  * Fix date value when casting a multiparameter date hash to not convert
43
121
  from Gregorian date to Julian date.
@@ -45,109 +123,120 @@
45
123
  Before:
46
124
 
47
125
  Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
48
- => #<Day id: nil, day: "0001-01-03", created_at: nil, updated_at: nil>
126
+ # => #<Day id: nil, day: "0001-01-03", created_at: nil, updated_at: nil>
49
127
 
50
128
  After:
51
129
 
52
130
  Day.new({"day(1i)"=>"1", "day(2i)"=>"1", "day(3i)"=>"1"})
53
- => #<Day id: nil, day: "0001-01-01", created_at: nil, updated_at: nil>
131
+ # => #<Day id: nil, day: "0001-01-01", created_at: nil, updated_at: nil>
54
132
 
55
133
  Fixes #28521.
56
134
 
57
135
  *Sayan Chakraborty*
58
136
 
59
- * Fix numericality equality validation of `BigDecimal` and `Float`
60
- by casting to `BigDecimal` on both ends of the validation.
61
-
62
- *Gannon McGibbon*
137
+ * Fix year value when casting a multiparameter time hash.
63
138
 
139
+ When assigning a hash to a time attribute that's missing a year component
140
+ (e.g. a `time_select` with `:ignore_date` set to `true`) then the year
141
+ defaults to 1970 instead of the expected 2000. This results in the attribute
142
+ changing as a result of the save.
64
143
 
65
- ## Rails 5.2.2.1 (March 11, 2019) ##
66
-
67
- * No changes.
144
+ Before:
145
+ ```
146
+ event = Event.new(start_time: { 4 => 20, 5 => 30 })
147
+ event.start_time # => 1970-01-01 20:30:00 UTC
148
+ event.save
149
+ event.reload
150
+ event.start_time # => 2000-01-01 20:30:00 UTC
151
+ ```
68
152
 
153
+ After:
154
+ ```
155
+ event = Event.new(start_time: { 4 => 20, 5 => 30 })
156
+ event.start_time # => 2000-01-01 20:30:00 UTC
157
+ event.save
158
+ event.reload
159
+ event.start_time # => 2000-01-01 20:30:00 UTC
160
+ ```
69
161
 
70
- ## Rails 5.2.2 (December 04, 2018) ##
162
+ *Andrew White*
71
163
 
72
- * Fix numericality validator to still use value before type cast except Active Record.
73
164
 
74
- Fixes #33651, #33686.
165
+ ## Rails 6.0.0.beta1 (January 18, 2019) ##
75
166
 
76
- *Ryuta Kamizono*
167
+ * Internal calls to `human_attribute_name` on an `Active Model` now pass attributes as strings instead of symbols
168
+ in some cases.
77
169
 
170
+ This is in line with examples in Rails docs and puts the code in line with the intention -
171
+ the potential use of strings or symbols.
78
172
 
79
- ## Rails 5.2.1.1 (November 27, 2018) ##
80
-
81
- * No changes.
173
+ It is recommended to cast the attribute input to your desired type as if you you are overriding that methid.
82
174
 
175
+ *Martin Larochelle*
83
176
 
84
- ## Rails 5.2.1 (August 07, 2018) ##
177
+ * Add `ActiveModel::Errors#of_kind?`.
85
178
 
86
- * No changes.
179
+ *bogdanvlviv*, *Rafael Mendonça França*
87
180
 
181
+ * Fix numericality equality validation of `BigDecimal` and `Float`
182
+ by casting to `BigDecimal` on both ends of the validation.
88
183
 
89
- ## Rails 5.2.0 (April 09, 2018) ##
184
+ *Gannon McGibbon*
90
185
 
91
- * Do not lose all multiple `:includes` with options in serialization.
186
+ * Add `#slice!` method to `ActiveModel::Errors`.
92
187
 
93
- *Mike Mangino*
188
+ *Daniel López Prat*
94
189
 
95
- * Models using the attributes API with a proc default can now be marshalled.
190
+ * Fix numericality validator to still use value before type cast except Active Record.
96
191
 
97
- Fixes #31216.
192
+ Fixes #33651, #33686.
98
193
 
99
- *Sean Griffin*
194
+ *Ryuta Kamizono*
100
195
 
101
- * Fix to working before/after validation callbacks on multiple contexts.
196
+ * Fix `ActiveModel::Serializers::JSON#as_json` method for timestamps.
102
197
 
103
- *Yoshiyuki Hirano*
198
+ Before:
199
+ ```
200
+ contact = Contact.new(created_at: Time.utc(2006, 8, 1))
201
+ contact.as_json["created_at"] # => 2006-08-01 00:00:00 UTC
202
+ ```
104
203
 
105
- * Execute `ConfirmationValidator` validation when `_confirmation`'s value is `false`.
204
+ After:
205
+ ```
206
+ contact = Contact.new(created_at: Time.utc(2006, 8, 1))
207
+ contact.as_json["created_at"] # => "2006-08-01T00:00:00.000Z"
208
+ ```
106
209
 
107
- *bogdanvlviv*
210
+ *Bogdan Gusiev*
108
211
 
109
- * Allow passing a Proc or Symbol to length validator options.
212
+ * Allows configurable attribute name for `#has_secure_password`. This
213
+ still defaults to an attribute named 'password', causing no breaking
214
+ change. There is a new method `#authenticate_XXX` where XXX is the
215
+ configured attribute name, making the existing `#authenticate` now an
216
+ alias for this when the attribute is the default 'password'.
110
217
 
111
- *Matt Rohrer*
218
+ Example:
112
219
 
113
- * Add method `#merge!` for `ActiveModel::Errors`.
220
+ class User < ActiveRecord::Base
221
+ has_secure_password :recovery_password, validations: false
222
+ end
114
223
 
115
- *Jahfer Husain*
224
+ user = User.new()
225
+ user.recovery_password = "42password"
226
+ user.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uX..."
227
+ user.authenticate_recovery_password('42password') # => user
116
228
 
117
- * Fix regression in numericality validator when comparing Decimal and Float input
118
- values with more scale than the schema.
229
+ *Unathi Chonco*
119
230
 
120
- *Bradley Priest*
231
+ * Add `config.active_model.i18n_customize_full_message` in order to control whether
232
+ the `full_message` error format can be overridden at the attribute or model
233
+ level in the locale files. This is `false` by default.
121
234
 
122
- * Fix methods `#keys`, `#values` in `ActiveModel::Errors`.
235
+ *Martin Larochelle*
123
236
 
124
- Change `#keys` to only return the keys that don't have empty messages.
237
+ * Rails 6 requires Ruby 2.5.0 or newer.
125
238
 
126
- Change `#values` to only return the not empty values.
239
+ *Jeremy Daer*, *Kasper Timm Hansen*
127
240
 
128
- Example:
129
241
 
130
- # Before
131
- person = Person.new
132
- person.errors.keys # => []
133
- person.errors.values # => []
134
- person.errors.messages # => {}
135
- person.errors[:name] # => []
136
- person.errors.messages # => {:name => []}
137
- person.errors.keys # => [:name]
138
- person.errors.values # => [[]]
139
-
140
- # After
141
- person = Person.new
142
- person.errors.keys # => []
143
- person.errors.values # => []
144
- person.errors.messages # => {}
145
- person.errors[:name] # => []
146
- person.errors.messages # => {:name => []}
147
- person.errors.keys # => []
148
- person.errors.values # => []
149
-
150
- *bogdanvlviv*
151
-
152
-
153
- Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activemodel/CHANGELOG.md) for previous changes.
242
+ Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-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-2019 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
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
@@ -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
@@ -133,10 +133,6 @@ module ActiveModel
133
133
  end
134
134
 
135
135
  protected
136
-
137
- attr_reader :original_attribute
138
- alias_method :assigned?, :original_attribute
139
-
140
136
  def original_value_for_database
141
137
  if assigned?
142
138
  original_attribute.original_value_for_database
@@ -146,6 +142,9 @@ module ActiveModel
146
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
@@ -27,7 +27,7 @@ module ActiveModel
27
27
  # cat.status # => 'sleeping'
28
28
  def assign_attributes(new_attributes)
29
29
  if !new_attributes.respond_to?(:stringify_keys)
30
- raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
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
 
@@ -38,7 +38,6 @@ module ActiveModel
38
38
  alias attributes= assign_attributes
39
39
 
40
40
  private
41
-
42
41
  def _assign_attributes(attributes)
43
42
  attributes.each do |k, v|
44
43
  _assign_attribute(k, v)
@@ -286,12 +286,12 @@ module ActiveModel
286
286
  method_name = matcher.method_name(attr_name)
287
287
 
288
288
  unless instance_method_already_implemented?(method_name)
289
- generate_method = "define_method_#{matcher.method_missing_target}"
289
+ generate_method = "define_method_#{matcher.target}"
290
290
 
291
291
  if respond_to?(generate_method, true)
292
292
  send(generate_method, attr_name.to_s)
293
293
  else
294
- define_proxy_call true, generated_attribute_methods, method_name, matcher.method_missing_target, attr_name.to_s
294
+ define_proxy_call true, generated_attribute_methods, method_name, matcher.target, attr_name.to_s
295
295
  end
296
296
  end
297
297
  end
@@ -352,53 +352,55 @@ module ActiveModel
352
352
 
353
353
  def attribute_method_matchers_matching(method_name)
354
354
  attribute_method_matchers_cache.compute_if_absent(method_name) do
355
- # Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix
356
- # will match every time.
355
+ # Bump plain matcher to last place so that only methods that do not
356
+ # match any other pattern match the actual attribute name.
357
+ # This is currently only needed to support legacy usage.
357
358
  matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1)
358
- matchers.map { |method| method.match(method_name) }.compact
359
+ matchers.map { |matcher| matcher.match(method_name) }.compact
359
360
  end
360
361
  end
361
362
 
362
363
  # Define a method `name` in `mod` that dispatches to `send`
363
364
  # using the given `extra` args. This falls back on `define_method`
364
365
  # and `send` if the given names cannot be compiled.
365
- def define_proxy_call(include_private, mod, name, send, *extra)
366
+ def define_proxy_call(include_private, mod, name, target, *extra)
366
367
  defn = if NAME_COMPILABLE_REGEXP.match?(name)
367
368
  "def #{name}(*args)"
368
369
  else
369
370
  "define_method(:'#{name}') do |*args|"
370
371
  end
371
372
 
372
- extra = (extra.map!(&:inspect) << "*args").join(", ".freeze)
373
+ extra = (extra.map!(&:inspect) << "*args").join(", ")
373
374
 
374
- target = if CALL_COMPILABLE_REGEXP.match?(send)
375
- "#{"self." unless include_private}#{send}(#{extra})"
375
+ body = if CALL_COMPILABLE_REGEXP.match?(target)
376
+ "#{"self." unless include_private}#{target}(#{extra})"
376
377
  else
377
- "send(:'#{send}', #{extra})"
378
+ "send(:'#{target}', #{extra})"
378
379
  end
379
380
 
380
381
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
381
382
  #{defn}
382
- #{target}
383
+ #{body}
383
384
  end
385
+ ruby2_keywords(:'#{name}') if respond_to?(:ruby2_keywords, true)
384
386
  RUBY
385
387
  end
386
388
 
387
389
  class AttributeMethodMatcher #:nodoc:
388
- attr_reader :prefix, :suffix, :method_missing_target
390
+ attr_reader :prefix, :suffix, :target
389
391
 
390
- AttributeMethodMatch = Struct.new(:target, :attr_name, :method_name)
392
+ AttributeMethodMatch = Struct.new(:target, :attr_name)
391
393
 
392
394
  def initialize(options = {})
393
395
  @prefix, @suffix = options.fetch(:prefix, ""), options.fetch(:suffix, "")
394
396
  @regex = /^(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})$/
395
- @method_missing_target = "#{@prefix}attribute#{@suffix}"
397
+ @target = "#{@prefix}attribute#{@suffix}"
396
398
  @method_name = "#{prefix}%s#{suffix}"
397
399
  end
398
400
 
399
401
  def match(method_name)
400
402
  if @regex =~ method_name
401
- AttributeMethodMatch.new(method_missing_target, $1, method_name)
403
+ AttributeMethodMatch.new(target, $1)
402
404
  end
403
405
  end
404
406
 
@@ -430,6 +432,7 @@ module ActiveModel
430
432
  match ? attribute_missing(match, *args, &block) : super
431
433
  end
432
434
  end
435
+ ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
433
436
 
434
437
  # +attribute_missing+ is like +method_missing+, but for attributes. When
435
438
  # +method_missing+ is called we check to see if there is a matching
@@ -474,5 +477,43 @@ module ActiveModel
474
477
  def _read_attribute(attr)
475
478
  __send__(attr)
476
479
  end
480
+
481
+ module AttrNames # :nodoc:
482
+ DEF_SAFE_NAME = /\A[a-zA-Z_]\w*\z/
483
+
484
+ # We want to generate the methods via module_eval rather than
485
+ # define_method, because define_method is slower on dispatch.
486
+ # Evaluating many similar methods may use more memory as the instruction
487
+ # sequences are duplicated and cached (in MRI). define_method may
488
+ # be slower on dispatch, but if you're careful about the closure
489
+ # created, then define_method will consume much less memory.
490
+ #
491
+ # But sometimes the database might return columns with
492
+ # characters that are not allowed in normal method names (like
493
+ # 'my_column(omg)'. So to work around this we first define with
494
+ # the __temp__ identifier, and then use alias method to rename
495
+ # it to what we want.
496
+ #
497
+ # We are also defining a constant to hold the frozen string of
498
+ # the attribute name. Using a constant means that we do not have
499
+ # to allocate an object on each call to the attribute method.
500
+ # Making it frozen means that it doesn't get duped when used to
501
+ # key the @attributes in read_attribute.
502
+ def self.define_attribute_accessor_method(mod, attr_name, writer: false)
503
+ method_name = "#{attr_name}#{'=' if writer}"
504
+ if attr_name.ascii_only? && DEF_SAFE_NAME.match?(attr_name)
505
+ yield method_name, "'#{attr_name}'.freeze"
506
+ else
507
+ safe_name = attr_name.unpack1("h*")
508
+ const_name = "ATTR_#{safe_name}"
509
+ const_set(const_name, attr_name) unless const_defined?(const_name)
510
+ temp_method_name = "__temp__#{safe_name}#{'=' if writer}"
511
+ attr_name_expr = "::ActiveModel::AttributeMethods::AttrNames::#{const_name}"
512
+ yield temp_method_name, attr_name_expr
513
+ mod.alias_method method_name, temp_method_name
514
+ mod.undef_method temp_method_name
515
+ end
516
+ end
517
+ end
477
518
  end
478
519
  end