activemodel 7.0.8.1 → 7.1.4
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 +158 -158
- data/MIT-LICENSE +1 -1
- data/README.rdoc +9 -9
- data/lib/active_model/access.rb +16 -0
- data/lib/active_model/api.rb +5 -5
- data/lib/active_model/attribute/user_provided_default.rb +4 -0
- data/lib/active_model/attribute.rb +26 -1
- data/lib/active_model/attribute_assignment.rb +1 -1
- data/lib/active_model/attribute_methods.rb +129 -73
- data/lib/active_model/attribute_registration.rb +77 -0
- data/lib/active_model/attribute_set.rb +10 -1
- data/lib/active_model/attributes.rb +65 -48
- data/lib/active_model/callbacks.rb +5 -5
- data/lib/active_model/conversion.rb +14 -4
- data/lib/active_model/deprecator.rb +7 -0
- data/lib/active_model/dirty.rb +134 -13
- data/lib/active_model/error.rb +4 -3
- data/lib/active_model/errors.rb +37 -6
- data/lib/active_model/forbidden_attributes_protection.rb +2 -0
- data/lib/active_model/gem_version.rb +4 -4
- data/lib/active_model/lint.rb +1 -1
- data/lib/active_model/locale/en.yml +1 -0
- data/lib/active_model/model.rb +34 -2
- data/lib/active_model/naming.rb +29 -10
- data/lib/active_model/railtie.rb +4 -0
- data/lib/active_model/secure_password.rb +61 -23
- data/lib/active_model/serialization.rb +3 -3
- data/lib/active_model/serializers/json.rb +1 -1
- data/lib/active_model/translation.rb +18 -16
- data/lib/active_model/type/big_integer.rb +23 -1
- data/lib/active_model/type/binary.rb +7 -1
- data/lib/active_model/type/boolean.rb +11 -9
- data/lib/active_model/type/date.rb +28 -2
- data/lib/active_model/type/date_time.rb +45 -3
- data/lib/active_model/type/decimal.rb +39 -1
- data/lib/active_model/type/float.rb +30 -1
- data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +5 -1
- data/lib/active_model/type/helpers/numeric.rb +6 -1
- data/lib/active_model/type/helpers/time_value.rb +28 -12
- data/lib/active_model/type/immutable_string.rb +37 -1
- data/lib/active_model/type/integer.rb +44 -1
- data/lib/active_model/type/serialize_cast_value.rb +47 -0
- data/lib/active_model/type/string.rb +9 -1
- data/lib/active_model/type/time.rb +48 -7
- data/lib/active_model/type/value.rb +17 -1
- data/lib/active_model/type.rb +1 -0
- data/lib/active_model/validations/absence.rb +1 -1
- data/lib/active_model/validations/acceptance.rb +1 -1
- data/lib/active_model/validations/callbacks.rb +4 -4
- data/lib/active_model/validations/clusivity.rb +5 -8
- data/lib/active_model/validations/comparability.rb +0 -11
- data/lib/active_model/validations/comparison.rb +15 -7
- data/lib/active_model/validations/format.rb +6 -7
- data/lib/active_model/validations/length.rb +10 -8
- data/lib/active_model/validations/numericality.rb +35 -23
- data/lib/active_model/validations/presence.rb +1 -1
- data/lib/active_model/validations/resolve_value.rb +26 -0
- data/lib/active_model/validations/validates.rb +4 -4
- data/lib/active_model/validations/with.rb +9 -2
- data/lib/active_model/validations.rb +44 -9
- data/lib/active_model/validator.rb +7 -5
- data/lib/active_model/version.rb +1 -1
- data/lib/active_model.rb +5 -1
- metadata +13 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7c65d57cfe07c87ab6784d35d6cad7b867be9b1af79dbd9f94952bc853f1bdb2
|
|
4
|
+
data.tar.gz: 7ed7f9189c0b5856f04f00cbad7b4b666c3a04ff00ecb97cb95aca385a8952b0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8c3efab2ebac160a00422547aa31c996135971e742680e26d1ecb93fe96ad95f76ebc6fb52932f58d84a4ee51a888f2701eef1bfe9d70da54fef0cf8a77233e6
|
|
7
|
+
data.tar.gz: 66dc340915cd6ffeef7e0a504f7fbcafda2e9af0b728a6679bd23b9e2db8c57f18cf10b64c4b4b2228b62f602e92f16384def45b70ac5f232e78467c8e7b3ab0
|
data/CHANGELOG.md
CHANGED
|
@@ -1,269 +1,269 @@
|
|
|
1
|
-
## Rails 7.
|
|
1
|
+
## Rails 7.1.4 (August 22, 2024) ##
|
|
2
2
|
|
|
3
3
|
* No changes.
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
## Rails 7.
|
|
6
|
+
## Rails 7.1.3.4 (June 04, 2024) ##
|
|
7
7
|
|
|
8
8
|
* No changes.
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
## Rails 7.
|
|
11
|
+
## Rails 7.1.3.3 (May 16, 2024) ##
|
|
12
12
|
|
|
13
13
|
* No changes.
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
## Rails 7.
|
|
16
|
+
## Rails 7.1.3.2 (February 21, 2024) ##
|
|
17
17
|
|
|
18
18
|
* No changes.
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
## Rails 7.
|
|
22
|
-
|
|
23
|
-
* Error.full_message now strips ":base" from the message.
|
|
24
|
-
|
|
25
|
-
*zzak*
|
|
26
|
-
|
|
27
|
-
* Add a load hook for `ActiveModel::Model` (named `active_model`) to match the load hook for
|
|
28
|
-
`ActiveRecord::Base` and allow for overriding aspects of the `ActiveModel::Model` class.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
## Rails 7.0.6 (June 29, 2023) ##
|
|
21
|
+
## Rails 7.1.3.1 (February 21, 2024) ##
|
|
32
22
|
|
|
33
23
|
* No changes.
|
|
34
24
|
|
|
35
25
|
|
|
36
|
-
## Rails 7.
|
|
26
|
+
## Rails 7.1.3 (January 16, 2024) ##
|
|
37
27
|
|
|
38
28
|
* No changes.
|
|
39
29
|
|
|
40
30
|
|
|
41
|
-
## Rails 7.
|
|
31
|
+
## Rails 7.1.2 (November 10, 2023) ##
|
|
42
32
|
|
|
43
|
-
*
|
|
33
|
+
* Make `==(other)` method of AttributeSet safe.
|
|
44
34
|
|
|
35
|
+
*Dmitry Pogrebnoy*
|
|
45
36
|
|
|
46
|
-
## Rails 7.0.4.3 (March 13, 2023) ##
|
|
47
37
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
## Rails 7.0.4.2 (January 24, 2023) ##
|
|
38
|
+
## Rails 7.1.1 (October 11, 2023) ##
|
|
52
39
|
|
|
53
40
|
* No changes.
|
|
54
41
|
|
|
55
42
|
|
|
56
|
-
## Rails 7.0
|
|
43
|
+
## Rails 7.1.0 (October 05, 2023) ##
|
|
57
44
|
|
|
58
45
|
* No changes.
|
|
59
46
|
|
|
60
47
|
|
|
61
|
-
## Rails 7.0.
|
|
62
|
-
|
|
63
|
-
* Handle name clashes in attribute methods code generation cache.
|
|
64
|
-
|
|
65
|
-
When two distinct attribute methods would generate similar names,
|
|
66
|
-
the first implementation would be incorrectly re-used.
|
|
67
|
-
|
|
68
|
-
```ruby
|
|
69
|
-
class A
|
|
70
|
-
attribute_method_suffix "_changed?"
|
|
71
|
-
define_attribute_methods :x
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
class B
|
|
75
|
-
attribute_method_suffix "?"
|
|
76
|
-
define_attribute_methods :x_changed
|
|
77
|
-
end
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
*Jean Boussier*
|
|
81
|
-
|
|
82
|
-
## Rails 7.0.3.1 (July 12, 2022) ##
|
|
48
|
+
## Rails 7.1.0.rc2 (October 01, 2023) ##
|
|
83
49
|
|
|
84
50
|
* No changes.
|
|
85
51
|
|
|
86
52
|
|
|
87
|
-
## Rails 7.0.
|
|
88
|
-
|
|
89
|
-
* No changes.
|
|
90
|
-
|
|
53
|
+
## Rails 7.1.0.rc1 (September 27, 2023) ##
|
|
91
54
|
|
|
92
|
-
|
|
55
|
+
* Remove change in the typography of user facing error messages.
|
|
56
|
+
For example, “can’t be blank” is again “can't be blank”.
|
|
93
57
|
|
|
94
|
-
*
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
## Rails 7.0.2.3 (March 08, 2022) ##
|
|
98
|
-
|
|
99
|
-
* No changes.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
## Rails 7.0.2.2 (February 11, 2022) ##
|
|
103
|
-
|
|
104
|
-
* No changes.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
## Rails 7.0.2.1 (February 11, 2022) ##
|
|
58
|
+
*Rafael Mendonça França*
|
|
108
59
|
|
|
109
|
-
* No changes.
|
|
110
60
|
|
|
61
|
+
## Rails 7.1.0.beta1 (September 13, 2023) ##
|
|
111
62
|
|
|
112
|
-
|
|
63
|
+
* Support composite identifiers in `to_key`
|
|
113
64
|
|
|
114
|
-
|
|
65
|
+
`to_key` avoids wrapping `#id` value into an `Array` if `#id` already an array
|
|
115
66
|
|
|
116
|
-
|
|
117
|
-
names, leading to conflicts. Adding a new namespace `:active_model_proxy`
|
|
118
|
-
fixes the issue.
|
|
67
|
+
*Nikita Vasilevsky*
|
|
119
68
|
|
|
120
|
-
|
|
69
|
+
* Add `ActiveModel::Conversion.param_delimiter` to configure delimiter being used in `to_param`
|
|
121
70
|
|
|
71
|
+
*Nikita Vasilevsky*
|
|
122
72
|
|
|
123
|
-
|
|
73
|
+
* `undefine_attribute_methods` undefines alias attribute methods along with attribute methods.
|
|
124
74
|
|
|
125
|
-
*
|
|
75
|
+
*Nikita Vasilevsky*
|
|
126
76
|
|
|
77
|
+
* Error.full_message now strips ":base" from the message.
|
|
127
78
|
|
|
128
|
-
|
|
79
|
+
*zzak*
|
|
129
80
|
|
|
130
|
-
*
|
|
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.
|
|
131
83
|
|
|
84
|
+
*Lewis Buckley*
|
|
132
85
|
|
|
133
|
-
|
|
86
|
+
* Improve password length validation in ActiveModel::SecurePassword to consider byte size for BCrypt
|
|
87
|
+
compatibility.
|
|
134
88
|
|
|
135
|
-
|
|
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.
|
|
136
92
|
|
|
93
|
+
```ruby
|
|
94
|
+
user = User.new(password: "a" * 73) # 73 characters
|
|
95
|
+
user.valid? # => false
|
|
96
|
+
user.errors[:password] # => ["is too long"]
|
|
137
97
|
|
|
138
|
-
## Rails 7.0.0.rc2 (December 14, 2021) ##
|
|
139
98
|
|
|
140
|
-
*
|
|
99
|
+
user = User.new(password: "あ" * 25) # 25 characters, 75 bytes
|
|
100
|
+
user.valid? # => false
|
|
101
|
+
user.errors[:password] # => ["is too long"]
|
|
102
|
+
```
|
|
141
103
|
|
|
142
|
-
|
|
104
|
+
*ChatGPT*, *Guillermo Iguaran*
|
|
143
105
|
|
|
144
|
-
*
|
|
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`:
|
|
145
109
|
|
|
146
|
-
|
|
110
|
+
```ruby
|
|
111
|
+
class User < ActiveRecord::Base
|
|
112
|
+
has_secure_password
|
|
147
113
|
|
|
148
|
-
|
|
114
|
+
generates_token_for :password_reset, expires_in: 15.minutes do
|
|
115
|
+
password_salt&.last(10)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
```
|
|
149
119
|
|
|
150
|
-
*
|
|
120
|
+
*Lázaro Nixon*
|
|
151
121
|
|
|
152
|
-
*
|
|
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".
|
|
153
125
|
|
|
154
|
-
*
|
|
126
|
+
*Jon Dufresne*
|
|
155
127
|
|
|
156
|
-
*
|
|
128
|
+
* Add class to `ActiveModel::MissingAttributeError` error message.
|
|
157
129
|
|
|
158
|
-
|
|
130
|
+
Show which class is missing the attribute in the error message:
|
|
159
131
|
|
|
160
|
-
|
|
132
|
+
```ruby
|
|
133
|
+
user = User.first
|
|
134
|
+
user.pets.select(:id).first.user_id
|
|
135
|
+
# => ActiveModel::MissingAttributeError: missing attribute 'user_id' for Pet
|
|
136
|
+
```
|
|
161
137
|
|
|
162
|
-
*
|
|
138
|
+
*Petrik de Heus*
|
|
163
139
|
|
|
164
|
-
*
|
|
140
|
+
* Raise `NoMethodError` in `ActiveModel::Type::Value#as_json` to avoid unpredictable
|
|
141
|
+
results.
|
|
165
142
|
|
|
166
|
-
*
|
|
143
|
+
*Vasiliy Ermolovich*
|
|
167
144
|
|
|
168
|
-
*
|
|
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.
|
|
169
148
|
|
|
170
|
-
|
|
149
|
+
For example, with a custom type like the following:
|
|
171
150
|
|
|
172
|
-
|
|
151
|
+
```ruby
|
|
152
|
+
class DowncasedString < ActiveModel::Type::String
|
|
153
|
+
def cast(value)
|
|
154
|
+
super&.downcase
|
|
155
|
+
end
|
|
156
|
+
end
|
|
173
157
|
|
|
174
|
-
|
|
158
|
+
ActiveRecord::Type.register(:downcased_string, DowncasedString)
|
|
175
159
|
|
|
176
|
-
|
|
160
|
+
class User < ActiveRecord::Base
|
|
161
|
+
attribute :email, :downcased_string
|
|
162
|
+
end
|
|
177
163
|
|
|
178
|
-
|
|
164
|
+
user = User.new(email: "FooBar@example.com")
|
|
165
|
+
```
|
|
179
166
|
|
|
180
|
-
|
|
167
|
+
Serializing the `email` attribute for the database will be roughly twice as
|
|
168
|
+
fast. More expensive `cast` operations will likely see greater improvements.
|
|
181
169
|
|
|
182
|
-
*
|
|
170
|
+
*Jonathan Hefner*
|
|
183
171
|
|
|
184
|
-
*
|
|
172
|
+
* `has_secure_password` now supports password challenges via a
|
|
173
|
+
`password_challenge` accessor and validation.
|
|
185
174
|
|
|
186
|
-
|
|
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.
|
|
187
179
|
|
|
188
|
-
|
|
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`).
|
|
189
183
|
|
|
190
|
-
|
|
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.
|
|
191
187
|
|
|
192
|
-
|
|
188
|
+
For example, in the controller, instead of:
|
|
193
189
|
|
|
194
|
-
|
|
190
|
+
```ruby
|
|
191
|
+
password_params = params.require(:password).permit(
|
|
192
|
+
:password_challenge,
|
|
193
|
+
:password,
|
|
194
|
+
:password_confirmation,
|
|
195
|
+
)
|
|
195
196
|
|
|
196
|
-
|
|
197
|
-
|
|
197
|
+
password_challenge = password_params.delete(:password_challenge)
|
|
198
|
+
@password_challenge_failed = !current_user.authenticate(password_challenge)
|
|
198
199
|
|
|
199
|
-
|
|
200
|
+
if !@password_challenge_failed && current_user.update(password_params)
|
|
201
|
+
# ...
|
|
202
|
+
end
|
|
203
|
+
```
|
|
200
204
|
|
|
201
|
-
|
|
205
|
+
You can now write:
|
|
202
206
|
|
|
203
|
-
|
|
204
|
-
|
|
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
|
+
```
|
|
205
218
|
|
|
206
|
-
|
|
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`.
|
|
207
222
|
|
|
208
|
-
*
|
|
223
|
+
*Jonathan Hefner*
|
|
209
224
|
|
|
210
|
-
|
|
225
|
+
* Support infinite ranges for `LengthValidator`s `:in`/`:within` options
|
|
211
226
|
|
|
212
|
-
|
|
227
|
+
```ruby
|
|
228
|
+
validates_length_of :first_name, in: ..30
|
|
229
|
+
```
|
|
213
230
|
|
|
231
|
+
*fatkodima*
|
|
214
232
|
|
|
215
|
-
|
|
233
|
+
* Add support for beginless ranges to inclusivity/exclusivity validators:
|
|
216
234
|
|
|
217
|
-
|
|
235
|
+
```ruby
|
|
236
|
+
validates_inclusion_of :birth_date, in: -> { (..Date.today) }
|
|
237
|
+
```
|
|
218
238
|
|
|
219
|
-
|
|
220
|
-
|
|
239
|
+
```ruby
|
|
240
|
+
validates_exclusion_of :birth_date, in: -> { (..Date.today) }
|
|
241
|
+
```
|
|
221
242
|
|
|
222
|
-
*
|
|
243
|
+
*Bo Jeanes*
|
|
223
244
|
|
|
224
|
-
*
|
|
245
|
+
* Make validators accept lambdas without record argument
|
|
225
246
|
|
|
226
|
-
|
|
227
|
-
|
|
247
|
+
```ruby
|
|
248
|
+
# Before
|
|
249
|
+
validates_comparison_of :birth_date, less_than_or_equal_to: ->(_record) { Date.today }
|
|
228
250
|
|
|
229
|
-
|
|
251
|
+
# After
|
|
252
|
+
validates_comparison_of :birth_date, less_than_or_equal_to: -> { Date.today }
|
|
253
|
+
```
|
|
230
254
|
|
|
231
|
-
*
|
|
255
|
+
*fatkodima*
|
|
232
256
|
|
|
233
|
-
|
|
257
|
+
* Fix casting long strings to `Date`, `Time` or `DateTime`
|
|
234
258
|
|
|
235
|
-
*
|
|
259
|
+
*fatkodima*
|
|
236
260
|
|
|
237
|
-
*
|
|
261
|
+
* Use different cache namespace for proxy calls
|
|
238
262
|
|
|
239
|
-
|
|
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.
|
|
240
266
|
|
|
241
267
|
*Chris Salzberg*
|
|
242
268
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
Passing a last positional argument `{}` would be incorrectly considered as keyword argument.
|
|
246
|
-
|
|
247
|
-
*Benoit Daloze*
|
|
248
|
-
|
|
249
|
-
* Cache and re-use generated attribute methods.
|
|
250
|
-
|
|
251
|
-
Generated methods with identical implementations will now share their instruction sequences
|
|
252
|
-
leading to reduced memory retention, and slightly faster load time.
|
|
253
|
-
|
|
254
|
-
*Jean Boussier*
|
|
255
|
-
|
|
256
|
-
* Add `in: range` parameter to `numericality` validator.
|
|
257
|
-
|
|
258
|
-
*Michal Papis*
|
|
259
|
-
|
|
260
|
-
* Add `locale` argument to `ActiveModel::Name#initialize` to be used to generate the `singular`,
|
|
261
|
-
`plural`, `route_key` and `singular_route_key` values.
|
|
262
|
-
|
|
263
|
-
*Lukas Pokorny*
|
|
264
|
-
|
|
265
|
-
* Make ActiveModel::Errors#inspect slimmer for readability
|
|
266
|
-
|
|
267
|
-
*lulalala*
|
|
268
|
-
|
|
269
|
-
Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/activemodel/CHANGELOG.md) for previous changes.
|
|
269
|
+
Please check [7-0-stable](https://github.com/rails/rails/blob/7-0-stable/activemodel/CHANGELOG.md) for previous changes.
|
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
= Active Model -- model interfaces for Rails
|
|
1
|
+
= Active Model -- model interfaces for \Rails
|
|
2
2
|
|
|
3
3
|
Active Model provides a known set of interfaces for usage in model classes.
|
|
4
4
|
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
|
-
the Rails framework.
|
|
6
|
+
the \Rails framework.
|
|
7
7
|
|
|
8
|
-
You can read more about Active Model in the {Active Model Basics}[https://
|
|
8
|
+
You can read more about Active Model in the {Active Model Basics}[https://guides.rubyonrails.org/active_model_basics.html] guide.
|
|
9
9
|
|
|
10
|
-
Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
|
|
10
|
+
Prior to \Rails 3.0, if a plugin or gem developer wanted to have an object
|
|
11
11
|
interact with Action Pack helpers, it was required to either copy chunks of
|
|
12
|
-
code from Rails, or monkey patch entire helpers to make them handle objects
|
|
12
|
+
code from \Rails, or monkey patch entire helpers to make them handle objects
|
|
13
13
|
that did not exactly conform to the Active Record interface. This would result
|
|
14
14
|
in code duplication and fragile applications that broke on upgrades. Active
|
|
15
15
|
Model solves this by defining an explicit API. You can read more about the
|
|
16
|
-
API in
|
|
16
|
+
API in +ActiveModel::Lint::Tests+.
|
|
17
17
|
|
|
18
18
|
Active Model provides a default module that implements the basic API required
|
|
19
|
-
to integrate with Action Pack out of the box:
|
|
19
|
+
to integrate with Action Pack out of the box: ActiveModel::API.
|
|
20
20
|
|
|
21
21
|
class Person
|
|
22
22
|
include ActiveModel::API
|
|
@@ -32,7 +32,7 @@ to integrate with Action Pack out of the box: <tt>ActiveModel::API</tt>.
|
|
|
32
32
|
|
|
33
33
|
It includes model name introspections, conversions, translations and
|
|
34
34
|
validations, resulting in a class suitable to be used with Action Pack.
|
|
35
|
-
See
|
|
35
|
+
See ActiveModel::API for more examples.
|
|
36
36
|
|
|
37
37
|
Active Model also provides the following functionality to have ORM-like
|
|
38
38
|
behavior out of the box:
|
|
@@ -156,7 +156,7 @@ behavior out of the box:
|
|
|
156
156
|
|
|
157
157
|
* Making objects serializable
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
ActiveModel::Serialization provides a standard interface for your object
|
|
160
160
|
to provide +to_json+ serialization.
|
|
161
161
|
|
|
162
162
|
class SerialPerson
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/enumerable"
|
|
4
|
+
require "active_support/core_ext/hash/indifferent_access"
|
|
5
|
+
|
|
6
|
+
module ActiveModel
|
|
7
|
+
module Access # :nodoc:
|
|
8
|
+
def slice(*methods)
|
|
9
|
+
methods.flatten.index_with { |method| public_send(method) }.with_indifferent_access
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def values_at(*methods)
|
|
13
|
+
methods.flatten.map! { |method| public_send(method) }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/active_model/api.rb
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module ActiveModel
|
|
4
|
-
#
|
|
4
|
+
# = Active \Model \API
|
|
5
5
|
#
|
|
6
6
|
# Includes the required interface for an object to interact with
|
|
7
|
-
# Action Pack and Action View, using different Active Model modules.
|
|
7
|
+
# Action Pack and Action View, using different Active \Model modules.
|
|
8
8
|
# It includes model name introspections, conversions, translations, and
|
|
9
9
|
# validations. Besides that, it allows you to initialize the object with a
|
|
10
10
|
# hash of attributes, pretty much like Active Record does.
|
|
@@ -20,7 +20,7 @@ module ActiveModel
|
|
|
20
20
|
# person.name # => "bob"
|
|
21
21
|
# person.age # => "18"
|
|
22
22
|
#
|
|
23
|
-
# Note that, by default,
|
|
23
|
+
# Note that, by default, +ActiveModel::API+ implements #persisted?
|
|
24
24
|
# to return +false+, which is the most common case. You may want to override
|
|
25
25
|
# it in your class to simulate a different scenario:
|
|
26
26
|
#
|
|
@@ -36,7 +36,7 @@ module ActiveModel
|
|
|
36
36
|
# person = Person.new(id: 1, name: 'bob')
|
|
37
37
|
# person.persisted? # => true
|
|
38
38
|
#
|
|
39
|
-
# Also, if for some reason you need to run code on
|
|
39
|
+
# Also, if for some reason you need to run code on initialize ( ::new ), make
|
|
40
40
|
# sure you call +super+ if you want the attributes hash initialization to
|
|
41
41
|
# happen.
|
|
42
42
|
#
|
|
@@ -54,7 +54,7 @@ module ActiveModel
|
|
|
54
54
|
# person.omg # => true
|
|
55
55
|
#
|
|
56
56
|
# For more detailed information on other functionalities available, please
|
|
57
|
-
# refer to the specific modules included in
|
|
57
|
+
# refer to the specific modules included in +ActiveModel::API+
|
|
58
58
|
# (see below).
|
|
59
59
|
module API
|
|
60
60
|
extend ActiveSupport::Concern
|
|
@@ -4,6 +4,10 @@ require "active_model/attribute"
|
|
|
4
4
|
|
|
5
5
|
module ActiveModel
|
|
6
6
|
class Attribute # :nodoc:
|
|
7
|
+
def with_user_default(value)
|
|
8
|
+
UserProvidedDefault.new(name, value, type, self.is_a?(FromDatabase) ? self : original_attribute)
|
|
9
|
+
end
|
|
10
|
+
|
|
7
11
|
class UserProvidedDefault < FromUser # :nodoc:
|
|
8
12
|
def initialize(name, value, type, database_default)
|
|
9
13
|
@user_provided_value = value
|
|
@@ -53,7 +53,10 @@ module ActiveModel
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def value_for_database
|
|
56
|
-
type.
|
|
56
|
+
if !defined?(@value_for_database) || type.changed_in_place?(@value_for_database, value)
|
|
57
|
+
@value_for_database = _value_for_database
|
|
58
|
+
end
|
|
59
|
+
@value_for_database
|
|
57
60
|
end
|
|
58
61
|
|
|
59
62
|
def serializable?(&block)
|
|
@@ -159,6 +162,10 @@ module ActiveModel
|
|
|
159
162
|
assigned? && type.changed?(original_value, value, value_before_type_cast)
|
|
160
163
|
end
|
|
161
164
|
|
|
165
|
+
def _value_for_database
|
|
166
|
+
type.serialize(value)
|
|
167
|
+
end
|
|
168
|
+
|
|
162
169
|
def _original_value_for_database
|
|
163
170
|
type.serialize(original_value)
|
|
164
171
|
end
|
|
@@ -168,6 +175,19 @@ module ActiveModel
|
|
|
168
175
|
type.deserialize(value)
|
|
169
176
|
end
|
|
170
177
|
|
|
178
|
+
def forgetting_assignment
|
|
179
|
+
# If this attribute was not persisted (with a `value_for_database`
|
|
180
|
+
# that might differ from `value_before_type_cast`) and `value` has not
|
|
181
|
+
# changed in place, we can use the existing `value_before_type_cast`
|
|
182
|
+
# to avoid deserialize / cast / serialize calls from computing the new
|
|
183
|
+
# attribute's `value_before_type_cast`.
|
|
184
|
+
if !defined?(@value_for_database) && !changed_in_place?
|
|
185
|
+
with_value_from_database(value_before_type_cast)
|
|
186
|
+
else
|
|
187
|
+
super
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
171
191
|
private
|
|
172
192
|
def _original_value_for_database
|
|
173
193
|
value_before_type_cast
|
|
@@ -182,6 +202,11 @@ module ActiveModel
|
|
|
182
202
|
def came_from_user?
|
|
183
203
|
!type.value_constructed_by_mass_assignment?(value_before_type_cast)
|
|
184
204
|
end
|
|
205
|
+
|
|
206
|
+
private
|
|
207
|
+
def _value_for_database
|
|
208
|
+
Type::SerializeCastValue.serialize(type, value)
|
|
209
|
+
end
|
|
185
210
|
end
|
|
186
211
|
|
|
187
212
|
class WithCastValue < Attribute # :nodoc:
|
|
@@ -10,7 +10,7 @@ module ActiveModel
|
|
|
10
10
|
# keys matching the attribute names.
|
|
11
11
|
#
|
|
12
12
|
# If the passed hash responds to <tt>permitted?</tt> method and the return value
|
|
13
|
-
# of this method is +false+ an
|
|
13
|
+
# of this method is +false+ an ActiveModel::ForbiddenAttributesError
|
|
14
14
|
# exception is raised.
|
|
15
15
|
#
|
|
16
16
|
# class Cat
|