activemodel 7.1.4 → 7.2.0.beta1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c65d57cfe07c87ab6784d35d6cad7b867be9b1af79dbd9f94952bc853f1bdb2
4
- data.tar.gz: 7ed7f9189c0b5856f04f00cbad7b4b666c3a04ff00ecb97cb95aca385a8952b0
3
+ metadata.gz: 70a0c28c415bc15bb939ec838af49318530a90d8f42546153450f4cc108c18c8
4
+ data.tar.gz: 57a6406ae772d62fb5b90e3df60de2ecf3f64e3a8c810cea7f24694ad70d16ef
5
5
  SHA512:
6
- metadata.gz: 8c3efab2ebac160a00422547aa31c996135971e742680e26d1ecb93fe96ad95f76ebc6fb52932f58d84a4ee51a888f2701eef1bfe9d70da54fef0cf8a77233e6
7
- data.tar.gz: 66dc340915cd6ffeef7e0a504f7fbcafda2e9af0b728a6679bd23b9e2db8c57f18cf10b64c4b4b2228b62f602e92f16384def45b70ac5f232e78467c8e7b3ab0
6
+ metadata.gz: 54191c069916b9400da52e84d6ba6d874570fc708a89069dd702d59db8fd37f143bc3aa0021d286d6ef9b970cbcfcf828e3111cb9b8c5f7c0502f84a88929a39
7
+ data.tar.gz: 16565d50c780f4a2ca419f3e8fff5a2856db38340ae93dc1ad0412bd0dcdc6fbd44b6559b00150ed083891fc392aac204e273f6511819aa7f12b076172f9178c
data/CHANGELOG.md CHANGED
@@ -1,269 +1,24 @@
1
- ## Rails 7.1.4 (August 22, 2024) ##
1
+ ## Rails 7.2.0.beta1 (May 29, 2024) ##
2
2
 
3
- * No changes.
3
+ * Fix a bug where type casting of string to `Time` and `DateTime` doesn't
4
+ calculate minus minute value in TZ offset correctly.
4
5
 
6
+ *Akira Matsuda*
5
7
 
6
- ## Rails 7.1.3.4 (June 04, 2024) ##
8
+ * Port the `type_for_attribute` method to Active Model. Classes that include
9
+ `ActiveModel::Attributes` will now provide this method. This method behaves
10
+ the same for Active Model as it does for Active Record.
7
11
 
8
- * No changes.
12
+ ```ruby
13
+ class MyModel
14
+ include ActiveModel::Attributes
9
15
 
10
-
11
- ## Rails 7.1.3.3 (May 16, 2024) ##
12
-
13
- * No changes.
14
-
15
-
16
- ## Rails 7.1.3.2 (February 21, 2024) ##
17
-
18
- * No changes.
19
-
20
-
21
- ## Rails 7.1.3.1 (February 21, 2024) ##
22
-
23
- * No changes.
24
-
25
-
26
- ## Rails 7.1.3 (January 16, 2024) ##
27
-
28
- * No changes.
29
-
30
-
31
- ## Rails 7.1.2 (November 10, 2023) ##
32
-
33
- * Make `==(other)` method of AttributeSet safe.
34
-
35
- *Dmitry Pogrebnoy*
36
-
37
-
38
- ## Rails 7.1.1 (October 11, 2023) ##
39
-
40
- * No changes.
41
-
42
-
43
- ## Rails 7.1.0 (October 05, 2023) ##
44
-
45
- * No changes.
46
-
47
-
48
- ## Rails 7.1.0.rc2 (October 01, 2023) ##
49
-
50
- * No changes.
51
-
52
-
53
- ## Rails 7.1.0.rc1 (September 27, 2023) ##
54
-
55
- * Remove change in the typography of user facing error messages.
56
- For example, “can’t be blank” is again “can't be blank”.
57
-
58
- *Rafael Mendonça França*
59
-
60
-
61
- ## Rails 7.1.0.beta1 (September 13, 2023) ##
62
-
63
- * Support composite identifiers in `to_key`
64
-
65
- `to_key` avoids wrapping `#id` value into an `Array` if `#id` already an array
66
-
67
- *Nikita Vasilevsky*
68
-
69
- * Add `ActiveModel::Conversion.param_delimiter` to configure delimiter being used in `to_param`
70
-
71
- *Nikita Vasilevsky*
72
-
73
- * `undefine_attribute_methods` undefines alias attribute methods along with attribute methods.
74
-
75
- *Nikita Vasilevsky*
76
-
77
- * Error.full_message now strips ":base" from the message.
78
-
79
- *zzak*
80
-
81
- * Add a load hook for `ActiveModel::Model` (named `active_model`) to match the load hook for
82
- `ActiveRecord::Base` and allow for overriding aspects of the `ActiveModel::Model` class.
83
-
84
- *Lewis Buckley*
85
-
86
- * Improve password length validation in ActiveModel::SecurePassword to consider byte size for BCrypt
87
- compatibility.
88
-
89
- The previous password length validation only considered the character count, which may not
90
- accurately reflect the 72-byte size limit imposed by BCrypt. This change updates the validation
91
- to consider both character count and byte size while keeping the character length validation in place.
92
-
93
- ```ruby
94
- user = User.new(password: "a" * 73) # 73 characters
95
- user.valid? # => false
96
- user.errors[:password] # => ["is too long"]
97
-
98
-
99
- user = User.new(password: "あ" * 25) # 25 characters, 75 bytes
100
- user.valid? # => false
101
- user.errors[:password] # => ["is too long"]
102
- ```
103
-
104
- *ChatGPT*, *Guillermo Iguaran*
105
-
106
- * `has_secure_password` now generates an `#{attribute}_salt` method that returns the salt
107
- used to compute the password digest. The salt will change whenever the password is changed,
108
- so it can be used to create single-use password reset tokens with `generates_token_for`:
109
-
110
- ```ruby
111
- class User < ActiveRecord::Base
112
- has_secure_password
113
-
114
- generates_token_for :password_reset, expires_in: 15.minutes do
115
- password_salt&.last(10)
116
- end
117
- end
118
- ```
119
-
120
- *Lázaro Nixon*
121
-
122
- * Improve typography of user facing error messages. In English contractions,
123
- the Unicode APOSTROPHE (`U+0027`) is now RIGHT SINGLE QUOTATION MARK
124
- (`U+2019`). For example, "can't be blank" is now "can’t be blank".
125
-
126
- *Jon Dufresne*
127
-
128
- * Add class to `ActiveModel::MissingAttributeError` error message.
129
-
130
- Show which class is missing the attribute in the error message:
131
-
132
- ```ruby
133
- user = User.first
134
- user.pets.select(:id).first.user_id
135
- # => ActiveModel::MissingAttributeError: missing attribute 'user_id' for Pet
136
- ```
137
-
138
- *Petrik de Heus*
139
-
140
- * Raise `NoMethodError` in `ActiveModel::Type::Value#as_json` to avoid unpredictable
141
- results.
142
-
143
- *Vasiliy Ermolovich*
144
-
145
- * Custom attribute types that inherit from Active Model built-in types and do
146
- not override the `serialize` method will now benefit from an optimization
147
- when serializing attribute values for the database.
148
-
149
- For example, with a custom type like the following:
150
-
151
- ```ruby
152
- class DowncasedString < ActiveModel::Type::String
153
- def cast(value)
154
- super&.downcase
16
+ attribute :my_attribute, :integer
155
17
  end
156
- end
157
-
158
- ActiveRecord::Type.register(:downcased_string, DowncasedString)
159
-
160
- class User < ActiveRecord::Base
161
- attribute :email, :downcased_string
162
- end
163
-
164
- user = User.new(email: "FooBar@example.com")
165
- ```
166
-
167
- Serializing the `email` attribute for the database will be roughly twice as
168
- fast. More expensive `cast` operations will likely see greater improvements.
169
-
170
- *Jonathan Hefner*
171
-
172
- * `has_secure_password` now supports password challenges via a
173
- `password_challenge` accessor and validation.
174
-
175
- A password challenge is a safeguard to verify that the current user is
176
- actually the password owner. It can be used when changing sensitive model
177
- fields, such as the password itself. It is different than a password
178
- confirmation, which is used to prevent password typos.
179
-
180
- When `password_challenge` is set, the validation checks that the value's
181
- digest matches the *currently persisted* `password_digest` (i.e.
182
- `password_digest_was`).
183
18
 
184
- This allows a password challenge to be done as part of a typical `update`
185
- call, just like a password confirmation. It also allows a password
186
- challenge error to be handled in the same way as other validation errors.
187
-
188
- For example, in the controller, instead of:
189
-
190
- ```ruby
191
- password_params = params.require(:password).permit(
192
- :password_challenge,
193
- :password,
194
- :password_confirmation,
195
- )
196
-
197
- password_challenge = password_params.delete(:password_challenge)
198
- @password_challenge_failed = !current_user.authenticate(password_challenge)
199
-
200
- if !@password_challenge_failed && current_user.update(password_params)
201
- # ...
202
- end
203
- ```
204
-
205
- You can now write:
206
-
207
- ```ruby
208
- password_params = params.require(:password).permit(
209
- :password_challenge,
210
- :password,
211
- :password_confirmation,
212
- ).with_defaults(password_challenge: "")
213
-
214
- if current_user.update(password_params)
215
- # ...
216
- end
217
- ```
218
-
219
- And, in the view, instead of checking `@password_challenge_failed`, you can
220
- render an error for the `password_challenge` field just as you would for
221
- other form fields, including utilizing `config.action_view.field_error_proc`.
19
+ MyModel.type_for_attribute(:my_attribute) # => #<ActiveModel::Type::Integer ...>
20
+ ```
222
21
 
223
22
  *Jonathan Hefner*
224
23
 
225
- * Support infinite ranges for `LengthValidator`s `:in`/`:within` options
226
-
227
- ```ruby
228
- validates_length_of :first_name, in: ..30
229
- ```
230
-
231
- *fatkodima*
232
-
233
- * Add support for beginless ranges to inclusivity/exclusivity validators:
234
-
235
- ```ruby
236
- validates_inclusion_of :birth_date, in: -> { (..Date.today) }
237
- ```
238
-
239
- ```ruby
240
- validates_exclusion_of :birth_date, in: -> { (..Date.today) }
241
- ```
242
-
243
- *Bo Jeanes*
244
-
245
- * Make validators accept lambdas without record argument
246
-
247
- ```ruby
248
- # Before
249
- validates_comparison_of :birth_date, less_than_or_equal_to: ->(_record) { Date.today }
250
-
251
- # After
252
- validates_comparison_of :birth_date, less_than_or_equal_to: -> { Date.today }
253
- ```
254
-
255
- *fatkodima*
256
-
257
- * Fix casting long strings to `Date`, `Time` or `DateTime`
258
-
259
- *fatkodima*
260
-
261
- * Use different cache namespace for proxy calls
262
-
263
- Models can currently have different attribute bodies for the same method
264
- names, leading to conflicts. Adding a new namespace `:active_model_proxy`
265
- fixes the issue.
266
-
267
- *Chris Salzberg*
268
-
269
- Please check [7-0-stable](https://github.com/rails/rails/blob/7-0-stable/activemodel/CHANGELOG.md) for previous changes.
24
+ Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/activemodel/CHANGELOG.md) for previous changes.
data/README.rdoc CHANGED
@@ -56,7 +56,7 @@ behavior out of the box:
56
56
  person.clear_name
57
57
  person.clear_age
58
58
 
59
- {Learn more}[link:classes/ActiveModel/AttributeMethods.html]
59
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html]
60
60
 
61
61
  * Callbacks for certain operations
62
62
 
@@ -74,7 +74,7 @@ behavior out of the box:
74
74
  This generates +before_create+, +around_create+ and +after_create+
75
75
  class methods that wrap your create method.
76
76
 
77
- {Learn more}[link:classes/ActiveModel/Callbacks.html]
77
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Callbacks.html]
78
78
 
79
79
  * Tracking value changes
80
80
 
@@ -110,7 +110,7 @@ behavior out of the box:
110
110
  person.save
111
111
  person.previous_changes # => {'name' => ['bob, 'robert']}
112
112
 
113
- {Learn more}[link:classes/ActiveModel/Dirty.html]
113
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Dirty.html]
114
114
 
115
115
  * Adding +errors+ interface to objects
116
116
 
@@ -141,7 +141,7 @@ behavior out of the box:
141
141
  person.errors.full_messages
142
142
  # => ["Name cannot be nil"]
143
143
 
144
- {Learn more}[link:classes/ActiveModel/Errors.html]
144
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Errors.html]
145
145
 
146
146
  * Model name introspection
147
147
 
@@ -152,7 +152,7 @@ behavior out of the box:
152
152
  NamedPerson.model_name.name # => "NamedPerson"
153
153
  NamedPerson.model_name.human # => "Named person"
154
154
 
155
- {Learn more}[link:classes/ActiveModel/Naming.html]
155
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Naming.html]
156
156
 
157
157
  * Making objects serializable
158
158
 
@@ -179,7 +179,7 @@ behavior out of the box:
179
179
  s = SerialPerson.new
180
180
  s.to_json # => "{\"name\":null}"
181
181
 
182
- {Learn more}[link:classes/ActiveModel/Serialization.html]
182
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Serialization.html]
183
183
 
184
184
  * Internationalization (i18n) support
185
185
 
@@ -190,7 +190,7 @@ behavior out of the box:
190
190
  Person.human_attribute_name('my_attribute')
191
191
  # => "My attribute"
192
192
 
193
- {Learn more}[link:classes/ActiveModel/Translation.html]
193
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Translation.html]
194
194
 
195
195
  * Validation support
196
196
 
@@ -208,7 +208,7 @@ behavior out of the box:
208
208
  person.first_name = 'zoolander'
209
209
  person.valid? # => false
210
210
 
211
- {Learn more}[link:classes/ActiveModel/Validations.html]
211
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Validations.html]
212
212
 
213
213
  * Custom validators
214
214
 
@@ -230,7 +230,7 @@ behavior out of the box:
230
230
  p.name = "Bob"
231
231
  p.valid? # => true
232
232
 
233
- {Learn more}[link:classes/ActiveModel/Validator.html]
233
+ {Learn more}[https://api.rubyonrails.org/classes/ActiveModel/Validator.html]
234
234
 
235
235
 
236
236
  == Download and installation
@@ -153,7 +153,7 @@ module ActiveModel
153
153
  alias :assigned? :original_attribute
154
154
 
155
155
  def initialize_dup(other)
156
- if defined?(@value) && @value.duplicable?
156
+ if @value&.duplicable?
157
157
  @value = @value.dup
158
158
  end
159
159
  end
@@ -45,8 +45,10 @@ module ActiveModel
45
45
 
46
46
  def _assign_attribute(k, v)
47
47
  setter = :"#{k}="
48
+ public_send(setter, v)
49
+ rescue NoMethodError
48
50
  if respond_to?(setter)
49
- public_send(setter, v)
51
+ raise
50
52
  else
51
53
  raise UnknownAttributeError.new(self, k.to_s)
52
54
  end
@@ -66,7 +66,6 @@ module ActiveModel
66
66
 
67
67
  NAME_COMPILABLE_REGEXP = /\A[a-zA-Z_]\w*[!?=]?\z/
68
68
  CALL_COMPILABLE_REGEXP = /\A[a-zA-Z_]\w*[!?]?\z/
69
- FORWARD_PARAMETERS = "*args"
70
69
 
71
70
  included do
72
71
  class_attribute :attribute_aliases, instance_writer: false, default: {}
@@ -215,36 +214,23 @@ module ActiveModel
215
214
  end
216
215
  end
217
216
 
218
- def generate_alias_attribute_methods(code_generator, new_name, old_name) # :nodoc:
219
- define_attribute_method(old_name, _owner: code_generator, as: new_name)
217
+ def generate_alias_attribute_methods(code_generator, new_name, old_name)
218
+ attribute_method_patterns.each do |pattern|
219
+ alias_attribute_method_definition(code_generator, pattern, new_name, old_name)
220
+ end
220
221
  end
221
222
 
222
223
  def alias_attribute_method_definition(code_generator, pattern, new_name, old_name) # :nodoc:
223
224
  method_name = pattern.method_name(new_name).to_s
224
225
  target_name = pattern.method_name(old_name).to_s
225
226
  parameters = pattern.parameters
226
- mangled_name = target_name
227
227
 
228
- unless NAME_COMPILABLE_REGEXP.match?(target_name)
229
- mangled_name = "__temp__#{target_name.unpack1("h*")}"
230
- end
228
+ mangled_name = build_mangled_name(target_name)
231
229
 
232
- code_generator.define_cached_method(mangled_name, as: method_name, namespace: :alias_attribute) do |batch|
233
- body = if CALL_COMPILABLE_REGEXP.match?(target_name)
234
- "self.#{target_name}(#{parameters || ''})"
235
- else
236
- call_args = [":'#{target_name}'"]
237
- call_args << parameters if parameters
238
- "send(#{call_args.join(", ")})"
239
- end
230
+ call_args = []
231
+ call_args << parameters if parameters
240
232
 
241
- modifier = parameters == FORWARD_PARAMETERS ? "ruby2_keywords " : ""
242
-
243
- batch <<
244
- "#{modifier}def #{mangled_name}(#{parameters || ''})" <<
245
- body <<
246
- "end"
247
- end
233
+ define_call(code_generator, method_name, target_name, mangled_name, parameters, call_args, namespace: :alias_attribute)
248
234
  end
249
235
 
250
236
  # Is +new_name+ an alias?
@@ -319,41 +305,22 @@ module ActiveModel
319
305
  # person.name = 'Bob'
320
306
  # person.name # => "Bob"
321
307
  # person.name_short? # => true
322
- def define_attribute_method(attr_name, _owner: generated_attribute_methods, as: attr_name)
308
+ def define_attribute_method(attr_name, _owner: generated_attribute_methods)
323
309
  ActiveSupport::CodeGenerator.batch(_owner, __FILE__, __LINE__) do |owner|
324
310
  attribute_method_patterns.each do |pattern|
325
- define_attribute_method_pattern(pattern, attr_name, owner: owner, as: as)
326
- end
327
- attribute_method_patterns_cache.clear
328
- end
329
- end
311
+ method_name = pattern.method_name(attr_name)
330
312
 
331
- def define_attribute_method_pattern(pattern, attr_name, owner:, as:, override: false) # :nodoc:
332
- canonical_method_name = pattern.method_name(attr_name)
333
- public_method_name = pattern.method_name(as)
334
-
335
- # If defining a regular attribute method, we don't override methods that are explictly
336
- # defined in parrent classes.
337
- if instance_method_already_implemented?(public_method_name)
338
- # However, for `alias_attribute`, we always define the method.
339
- # We check for override second because `instance_method_already_implemented?`
340
- # also check for dangerous methods.
341
- return unless override
342
- end
313
+ unless instance_method_already_implemented?(method_name)
314
+ generate_method = "define_method_#{pattern.proxy_target}"
343
315
 
344
- generate_method = "define_method_#{pattern.proxy_target}"
345
- if respond_to?(generate_method, true)
346
- send(generate_method, attr_name.to_s, owner: owner, as: as)
347
- else
348
- define_proxy_call(
349
- owner,
350
- canonical_method_name,
351
- pattern.proxy_target,
352
- pattern.parameters,
353
- attr_name.to_s,
354
- namespace: :active_model_proxy,
355
- as: public_method_name,
356
- )
316
+ if respond_to?(generate_method, true)
317
+ send(generate_method, attr_name.to_s, owner: owner)
318
+ else
319
+ define_proxy_call(owner, method_name, pattern.proxy_target, pattern.parameters, attr_name.to_s, namespace: :active_model_proxy)
320
+ end
321
+ end
322
+ end
323
+ attribute_method_patterns_cache.clear
357
324
  end
358
325
  end
359
326
 
@@ -398,6 +365,8 @@ module ActiveModel
398
365
  super
399
366
  base.class_eval do
400
367
  @attribute_method_patterns_cache = nil
368
+ @aliases_by_attribute_name = nil
369
+ @generated_attribute_methods = nil
401
370
  end
402
371
  end
403
372
 
@@ -435,28 +404,37 @@ module ActiveModel
435
404
  # Define a method `name` in `mod` that dispatches to `send`
436
405
  # using the given `extra` args. This falls back on `send`
437
406
  # if the called name cannot be compiled.
438
- def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args, namespace:, as: name)
407
+ def define_proxy_call(code_generator, name, proxy_target, parameters, *call_args, namespace:)
408
+ mangled_name = build_mangled_name(name)
409
+
410
+ call_args.map!(&:inspect)
411
+ call_args << parameters if parameters
412
+ namespace = :"#{namespace}_#{proxy_target}_#{call_args.join("_")}}"
413
+
414
+ define_call(code_generator, name, proxy_target, mangled_name, parameters, call_args, namespace: namespace)
415
+ end
416
+
417
+ def build_mangled_name(name)
439
418
  mangled_name = name
419
+
440
420
  unless NAME_COMPILABLE_REGEXP.match?(name)
441
421
  mangled_name = "__temp__#{name.unpack1("h*")}"
442
422
  end
443
423
 
444
- call_args.map!(&:inspect)
445
- call_args << parameters if parameters
446
- namespace = :"#{namespace}_#{proxy_target}"
424
+ mangled_name
425
+ end
447
426
 
448
- code_generator.define_cached_method(mangled_name, as: as, namespace: namespace) do |batch|
449
- body = if CALL_COMPILABLE_REGEXP.match?(proxy_target)
450
- "self.#{proxy_target}(#{call_args.join(", ")})"
427
+ def define_call(code_generator, name, target_name, mangled_name, parameters, call_args, namespace:)
428
+ code_generator.define_cached_method(name, as: mangled_name, namespace: namespace) do |batch|
429
+ body = if CALL_COMPILABLE_REGEXP.match?(target_name)
430
+ "self.#{target_name}(#{call_args.join(", ")})"
451
431
  else
452
- call_args.unshift(":'#{proxy_target}'")
432
+ call_args.unshift(":'#{target_name}'")
453
433
  "send(#{call_args.join(", ")})"
454
434
  end
455
435
 
456
- modifier = parameters == FORWARD_PARAMETERS ? "ruby2_keywords " : ""
457
-
458
436
  batch <<
459
- "#{modifier}def #{mangled_name}(#{parameters || ''})" <<
437
+ "def #{mangled_name}(#{parameters || ''})" <<
460
438
  body <<
461
439
  "end"
462
440
  end
@@ -470,7 +448,7 @@ module ActiveModel
470
448
  def initialize(prefix: "", suffix: "", parameters: nil)
471
449
  @prefix = prefix
472
450
  @suffix = suffix
473
- @parameters = parameters.nil? ? FORWARD_PARAMETERS : parameters
451
+ @parameters = parameters.nil? ? "..." : parameters
474
452
  @regex = /\A(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})\z/
475
453
  @proxy_target = "#{@prefix}attribute#{@suffix}"
476
454
  @method_name = "#{prefix}%s#{suffix}"
@@ -498,24 +476,22 @@ module ActiveModel
498
476
  # It's also possible to instantiate related objects, so a <tt>Client</tt>
499
477
  # class belonging to the +clients+ table with a +master_id+ foreign key
500
478
  # can instantiate master through <tt>Client#master</tt>.
501
- def method_missing(method, *args, &block)
479
+ def method_missing(method, ...)
502
480
  if respond_to_without_attributes?(method, true)
503
481
  super
504
482
  else
505
- match = matched_attribute_method(method.to_s)
506
- match ? attribute_missing(match, *args, &block) : super
483
+ match = matched_attribute_method(method.name)
484
+ match ? attribute_missing(match, ...) : super
507
485
  end
508
486
  end
509
- ruby2_keywords(:method_missing)
510
487
 
511
488
  # +attribute_missing+ is like +method_missing+, but for attributes. When
512
489
  # +method_missing+ is called we check to see if there is a matching
513
490
  # attribute method. If so, we tell +attribute_missing+ to dispatch the
514
491
  # attribute. This method can be overloaded to customize the behavior.
515
- def attribute_missing(match, *args, &block)
516
- __send__(match.proxy_target, match.attr_name, *args, &block)
492
+ def attribute_missing(match, ...)
493
+ __send__(match.proxy_target, match.attr_name, ...)
517
494
  end
518
- ruby2_keywords(:attribute_missing)
519
495
 
520
496
  # A +Person+ instance with a +name+ attribute can ask
521
497
  # <tt>person.respond_to?(:name)</tt>, <tt>person.respond_to?(:name=)</tt>,
@@ -10,17 +10,28 @@ module ActiveModel
10
10
 
11
11
  module ClassMethods # :nodoc:
12
12
  def attribute(name, type = nil, default: (no_default = true), **options)
13
+ name = resolve_attribute_name(name)
13
14
  type = resolve_type_name(type, **options) if type.is_a?(Symbol)
15
+ type = hook_attribute_type(name, type) if type
14
16
 
15
- pending = pending_attribute(name)
16
- pending.type = type if type
17
- pending.default = default unless no_default
17
+ pending_attribute_modifications << PendingType.new(name, type) if type || no_default
18
+ pending_attribute_modifications << PendingDefault.new(name, default) unless no_default
19
+
20
+ reset_default_attributes
21
+ end
22
+
23
+ def decorate_attributes(names = nil, &decorator) # :nodoc:
24
+ names = names&.map { |name| resolve_attribute_name(name) }
25
+
26
+ pending_attribute_modifications << PendingDecorator.new(names, decorator)
18
27
 
19
28
  reset_default_attributes
20
29
  end
21
30
 
22
31
  def _default_attributes # :nodoc:
23
- @default_attributes ||= build_default_attributes
32
+ @default_attributes ||= AttributeSet.new({}).tap do |attribute_set|
33
+ apply_pending_attribute_modifications(attribute_set)
34
+ end
24
35
  end
25
36
 
26
37
  def attribute_types # :nodoc:
@@ -29,40 +40,62 @@ module ActiveModel
29
40
  end
30
41
  end
31
42
 
43
+ def type_for_attribute(attribute_name, &block)
44
+ attribute_name = resolve_attribute_name(attribute_name)
45
+
46
+ if block
47
+ attribute_types.fetch(attribute_name, &block)
48
+ else
49
+ attribute_types[attribute_name]
50
+ end
51
+ end
52
+
32
53
  private
33
- class PendingAttribute # :nodoc:
34
- attr_accessor :type, :default
54
+ PendingType = Struct.new(:name, :type) do # :nodoc:
55
+ def apply_to(attribute_set)
56
+ attribute = attribute_set[name]
57
+ attribute_set[name] = attribute.with_type(type || attribute.type)
58
+ end
59
+ end
35
60
 
36
- def apply_to(attribute)
37
- attribute = attribute.with_type(type || attribute.type)
38
- attribute = attribute.with_user_default(default) if defined?(@default)
39
- attribute
61
+ PendingDefault = Struct.new(:name, :default) do # :nodoc:
62
+ def apply_to(attribute_set)
63
+ attribute_set[name] = attribute_set[name].with_user_default(default)
40
64
  end
41
65
  end
42
66
 
43
- def pending_attribute(name)
44
- @pending_attributes ||= {}
45
- @pending_attributes[resolve_attribute_name(name)] ||= PendingAttribute.new
67
+ PendingDecorator = Struct.new(:names, :decorator) do # :nodoc:
68
+ def apply_to(attribute_set)
69
+ (names || attribute_set.keys).each do |name|
70
+ attribute = attribute_set[name]
71
+ type = decorator.call(name, attribute.type)
72
+ attribute_set[name] = attribute.with_type(type) if type
73
+ end
74
+ end
46
75
  end
47
76
 
48
- def apply_pending_attributes(attribute_set)
49
- superclass.send(__method__, attribute_set) if superclass.respond_to?(__method__, true)
77
+ def pending_attribute_modifications
78
+ @pending_attribute_modifications ||= []
79
+ end
50
80
 
51
- defined?(@pending_attributes) && @pending_attributes.each do |name, pending|
52
- attribute_set[name] = pending.apply_to(attribute_set[name])
81
+ def apply_pending_attribute_modifications(attribute_set)
82
+ if superclass.respond_to?(:apply_pending_attribute_modifications, true)
83
+ superclass.send(:apply_pending_attribute_modifications, attribute_set)
53
84
  end
54
85
 
55
- attribute_set
86
+ pending_attribute_modifications.each do |modification|
87
+ modification.apply_to(attribute_set)
88
+ end
56
89
  end
57
90
 
58
- def build_default_attributes
59
- apply_pending_attributes(AttributeSet.new({}))
91
+ def reset_default_attributes
92
+ reset_default_attributes!
93
+ subclasses.each { |subclass| subclass.send(:reset_default_attributes) }
60
94
  end
61
95
 
62
- def reset_default_attributes
96
+ def reset_default_attributes!
63
97
  @default_attributes = nil
64
98
  @attribute_types = nil
65
- subclasses.each { |subclass| subclass.send(__method__) }
66
99
  end
67
100
 
68
101
  def resolve_attribute_name(name)
@@ -72,6 +105,13 @@ module ActiveModel
72
105
  def resolve_type_name(name, **options)
73
106
  Type.lookup(name, **options)
74
107
  end
108
+
109
+ # Hook for other modules to override. The attribute type is passed
110
+ # through this method immediately after it is resolved, before any type
111
+ # decorations are applied.
112
+ def hook_attribute_type(attribute, type)
113
+ type
114
+ end
75
115
  end
76
116
  end
77
117
  end
@@ -75,12 +75,25 @@ module ActiveModel
75
75
  attribute_types.keys
76
76
  end
77
77
 
78
+ ##
79
+ # :method: type_for_attribute
80
+ # :call-seq: type_for_attribute(attribute_name, &block)
81
+ #
82
+ # Returns the type of the specified attribute after applying any
83
+ # modifiers. This method is the only valid source of information for
84
+ # anything related to the types of a model's attributes. The return value
85
+ # of this method will implement the interface described by
86
+ # ActiveModel::Type::Value (though the object itself may not subclass it).
87
+ #--
88
+ # Implemented by ActiveModel::AttributeRegistration::ClassMethods#type_for_attribute.
89
+
90
+ ##
78
91
  private
79
- def define_method_attribute=(canonical_name, owner:, as: canonical_name)
92
+ def define_method_attribute=(name, owner:)
80
93
  ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
81
- owner, canonical_name, writer: true,
94
+ owner, name, writer: true,
82
95
  ) do |temp_method_name, attr_name_expr|
83
- owner.define_cached_method(temp_method_name, as: "#{as}=", namespace: :active_model) do |batch|
96
+ owner.define_cached_method("#{name}=", as: temp_method_name, namespace: :active_model) do |batch|
84
97
  batch <<
85
98
  "def #{temp_method_name}(value)" <<
86
99
  " _write_attribute(#{attr_name_expr}, value)" <<
@@ -60,7 +60,7 @@ module ActiveModel
60
60
  # Would only create the +after_create+ and +before_create+ callback methods in
61
61
  # your class.
62
62
  #
63
- # NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
63
+ # NOTE: Defining the same callback multiple times will overwrite previous callback definitions.
64
64
  #
65
65
  module Callbacks
66
66
  def self.extended(base) # :nodoc:
@@ -8,9 +8,9 @@ module ActiveModel
8
8
 
9
9
  module VERSION
10
10
  MAJOR = 7
11
- MINOR = 1
12
- TINY = 4
13
- PRE = nil
11
+ MINOR = 2
12
+ TINY = 0
13
+ PRE = "beta1"
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
16
  end
@@ -41,7 +41,7 @@ module ActiveModel
41
41
  #
42
42
  # To use +has_secure_password+, add bcrypt (~> 3.1.7) to your Gemfile:
43
43
  #
44
- # gem 'bcrypt', '~> 3.1.7'
44
+ # gem "bcrypt", "~> 3.1.7"
45
45
  #
46
46
  # ==== Examples
47
47
  #
@@ -94,7 +94,13 @@ module ActiveModel
94
94
  end
95
95
 
96
96
  if $8
97
- offset = $8 == "Z" ? 0 : $8.to_i * 3600 + $9.to_i * 60
97
+ offset = \
98
+ if $8 == "Z"
99
+ 0
100
+ else
101
+ offset_h, offset_m = $8.to_i, $9.to_i
102
+ offset_h.to_i * 3600 + (offset_h.negative? ? -1 : 1) * offset_m * 60
103
+ end
98
104
  end
99
105
 
100
106
  new_time($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, usec, offset)
@@ -20,16 +20,15 @@ module ActiveModel
20
20
  registrations[type_name] = block
21
21
  end
22
22
 
23
- def lookup(symbol, *args)
23
+ def lookup(symbol, ...)
24
24
  registration = registrations[symbol]
25
25
 
26
26
  if registration
27
- registration.call(symbol, *args)
27
+ registration.call(symbol, ...)
28
28
  else
29
29
  raise ArgumentError, "Unknown type #{symbol.inspect}"
30
30
  end
31
31
  end
32
- ruby2_keywords(:lookup)
33
32
 
34
33
  private
35
34
  attr_reader :registrations
@@ -101,7 +101,7 @@ module ActiveModel
101
101
  options[:on] = Array(options[:on])
102
102
  options[:if] = [
103
103
  ->(o) {
104
- !(options[:on] & Array(o.validation_context)).empty?
104
+ options[:on].intersect?(Array(o.validation_context))
105
105
  },
106
106
  *options[:if]
107
107
  ]
@@ -10,7 +10,7 @@ module ActiveModel
10
10
  include ResolveValue
11
11
 
12
12
  def check_validity!
13
- unless (options.keys & COMPARE_CHECKS.keys).any?
13
+ unless options.keys.intersect?(COMPARE_CHECKS.keys)
14
14
  raise ArgumentError, "Expected one of :greater_than, :greater_than_or_equal_to, "\
15
15
  ":equal_to, :less_than, :less_than_or_equal_to, or :other_than option to be supplied."
16
16
  end
@@ -10,7 +10,7 @@ module ActiveModel
10
10
  # validators can be overridden inside specific classes by creating
11
11
  # custom validator classes in their place such as PresenceValidator.
12
12
  #
13
- # Examples of using the default \Rails validators:
13
+ # Examples of using the default Rails validators:
14
14
  #
15
15
  # validates :username, absence: true
16
16
  # validates :terms, acceptance: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.4
4
+ version: 7.2.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-22 00:00:00.000000000 Z
11
+ date: 2024-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 7.1.4
19
+ version: 7.2.0.beta1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 7.1.4
26
+ version: 7.2.0.beta1
27
27
  description: A toolkit for building modeling frameworks like Active Record. Rich support
28
28
  for attributes, callbacks, validations, serialization, internationalization, and
29
29
  testing.
@@ -112,10 +112,10 @@ licenses:
112
112
  - MIT
113
113
  metadata:
114
114
  bug_tracker_uri: https://github.com/rails/rails/issues
115
- changelog_uri: https://github.com/rails/rails/blob/v7.1.4/activemodel/CHANGELOG.md
116
- documentation_uri: https://api.rubyonrails.org/v7.1.4/
115
+ changelog_uri: https://github.com/rails/rails/blob/v7.2.0.beta1/activemodel/CHANGELOG.md
116
+ documentation_uri: https://api.rubyonrails.org/v7.2.0.beta1/
117
117
  mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
118
- source_code_uri: https://github.com/rails/rails/tree/v7.1.4/activemodel
118
+ source_code_uri: https://github.com/rails/rails/tree/v7.2.0.beta1/activemodel
119
119
  rubygems_mfa_required: 'true'
120
120
  post_install_message:
121
121
  rdoc_options: []
@@ -125,14 +125,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
125
  requirements:
126
126
  - - ">="
127
127
  - !ruby/object:Gem::Version
128
- version: 2.7.0
128
+ version: 3.1.0
129
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - ">="
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
134
  requirements: []
135
- rubygems_version: 3.5.11
135
+ rubygems_version: 3.5.10
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: A toolkit for building modeling frameworks (part of Rails).