activemodel 7.0.6 → 7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +149 -156
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +9 -9
  5. data/lib/active_model/access.rb +16 -0
  6. data/lib/active_model/api.rb +5 -5
  7. data/lib/active_model/attribute/user_provided_default.rb +4 -0
  8. data/lib/active_model/attribute.rb +26 -1
  9. data/lib/active_model/attribute_assignment.rb +1 -1
  10. data/lib/active_model/attribute_methods.rb +103 -64
  11. data/lib/active_model/attribute_registration.rb +77 -0
  12. data/lib/active_model/attribute_set.rb +9 -0
  13. data/lib/active_model/attributes.rb +62 -45
  14. data/lib/active_model/callbacks.rb +5 -5
  15. data/lib/active_model/conversion.rb +14 -4
  16. data/lib/active_model/deprecator.rb +7 -0
  17. data/lib/active_model/dirty.rb +134 -13
  18. data/lib/active_model/error.rb +5 -4
  19. data/lib/active_model/errors.rb +11 -6
  20. data/lib/active_model/forbidden_attributes_protection.rb +2 -0
  21. data/lib/active_model/gem_version.rb +3 -3
  22. data/lib/active_model/lint.rb +1 -1
  23. data/lib/active_model/locale/en.yml +1 -0
  24. data/lib/active_model/model.rb +26 -2
  25. data/lib/active_model/naming.rb +29 -10
  26. data/lib/active_model/railtie.rb +4 -0
  27. data/lib/active_model/secure_password.rb +61 -23
  28. data/lib/active_model/serialization.rb +3 -3
  29. data/lib/active_model/serializers/json.rb +1 -1
  30. data/lib/active_model/translation.rb +18 -16
  31. data/lib/active_model/type/big_integer.rb +23 -1
  32. data/lib/active_model/type/binary.rb +7 -1
  33. data/lib/active_model/type/boolean.rb +11 -9
  34. data/lib/active_model/type/date.rb +28 -2
  35. data/lib/active_model/type/date_time.rb +45 -3
  36. data/lib/active_model/type/decimal.rb +39 -1
  37. data/lib/active_model/type/float.rb +30 -1
  38. data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +5 -1
  39. data/lib/active_model/type/helpers/mutable.rb +4 -4
  40. data/lib/active_model/type/helpers/numeric.rb +4 -0
  41. data/lib/active_model/type/helpers/time_value.rb +28 -12
  42. data/lib/active_model/type/immutable_string.rb +37 -1
  43. data/lib/active_model/type/integer.rb +44 -1
  44. data/lib/active_model/type/serialize_cast_value.rb +47 -0
  45. data/lib/active_model/type/string.rb +9 -1
  46. data/lib/active_model/type/time.rb +48 -7
  47. data/lib/active_model/type/value.rb +23 -3
  48. data/lib/active_model/type.rb +1 -0
  49. data/lib/active_model/validations/absence.rb +1 -1
  50. data/lib/active_model/validations/acceptance.rb +1 -1
  51. data/lib/active_model/validations/callbacks.rb +4 -4
  52. data/lib/active_model/validations/clusivity.rb +5 -8
  53. data/lib/active_model/validations/comparability.rb +0 -11
  54. data/lib/active_model/validations/comparison.rb +15 -7
  55. data/lib/active_model/validations/format.rb +6 -7
  56. data/lib/active_model/validations/length.rb +10 -8
  57. data/lib/active_model/validations/numericality.rb +35 -23
  58. data/lib/active_model/validations/presence.rb +1 -1
  59. data/lib/active_model/validations/resolve_value.rb +26 -0
  60. data/lib/active_model/validations/validates.rb +3 -3
  61. data/lib/active_model/validations/with.rb +9 -2
  62. data/lib/active_model/validations.rb +44 -9
  63. data/lib/active_model/validator.rb +7 -5
  64. data/lib/active_model/version.rb +1 -1
  65. data/lib/active_model.rb +5 -1
  66. metadata +16 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88150733324b801dc2198570a32ef11184c4865a2cb3fbccd405ac31dad489a6
4
- data.tar.gz: 6d7ce343da16108e59c8362526dc898adba71d6837cbe695a3fd8bd30ad2eb08
3
+ metadata.gz: 191a355e300ba0a18a587294a4ba721be77929a97a780f0a7dfaa519dd31ec2f
4
+ data.tar.gz: f27b9ae46f6e20784e8c6eebc82994cb01cb169d3b57e622c9a772fccce1bf12
5
5
  SHA512:
6
- metadata.gz: 6fee121b0166ed0a83926f5d575cc700d7267d4c5b87d0dc4cdbd5d0c5eed376e15688c5aa884af7375548a55a20138a16a2fc0c999289c5de0b1b49631213c8
7
- data.tar.gz: c9d3ceceb28ed93fa597d1cd41d727ee63022e74849e79a9ee5f7a9202beeddb41e15e69103009768b46ce8be82cf5f43b84b06e88db32cc7fd82c4e3ffb1efe
6
+ metadata.gz: 9985a97c165f261521b48011e8ac8a63ec61a6e6357a6ea2c9491bbed44838a492f400fccfa48f8362b7cbf53f8600a912420d57fbc473e0dcfdbfa7ed93e882
7
+ data.tar.gz: 5aa0cc11f2366bd7019c10926ae34c2d0fa9e6f3acc5970dee48d25aa876efc23f71b3089e99d58e3b95375de81a17634869d962a3741037b32b7f72e1bc546e
data/CHANGELOG.md CHANGED
@@ -1,239 +1,232 @@
1
- ## Rails 7.0.6 (June 29, 2023) ##
1
+ ## Rails 7.1.1 (October 11, 2023) ##
2
2
 
3
3
  * No changes.
4
4
 
5
5
 
6
- ## Rails 7.0.5.1 (June 26, 2023) ##
6
+ ## Rails 7.1.0 (October 05, 2023) ##
7
7
 
8
8
  * No changes.
9
9
 
10
10
 
11
- ## Rails 7.0.5 (May 24, 2023) ##
11
+ ## Rails 7.1.0.rc2 (October 01, 2023) ##
12
12
 
13
13
  * No changes.
14
14
 
15
15
 
16
- ## Rails 7.0.4.3 (March 13, 2023) ##
16
+ ## Rails 7.1.0.rc1 (September 27, 2023) ##
17
17
 
18
- * No changes.
19
-
20
-
21
- ## Rails 7.0.4.2 (January 24, 2023) ##
22
-
23
- * No changes.
24
-
25
-
26
- ## Rails 7.0.4.1 (January 17, 2023) ##
27
-
28
- * No changes.
29
-
30
-
31
- ## Rails 7.0.4 (September 09, 2022) ##
32
-
33
- * Handle name clashes in attribute methods code generation cache.
34
-
35
- When two distinct attribute methods would generate similar names,
36
- the first implementation would be incorrectly re-used.
37
-
38
- ```ruby
39
- class A
40
- attribute_method_suffix "_changed?"
41
- define_attribute_methods :x
42
- end
43
-
44
- class B
45
- attribute_method_suffix "?"
46
- define_attribute_methods :x_changed
47
- end
48
- ```
49
-
50
- *Jean Boussier*
51
-
52
- ## Rails 7.0.3.1 (July 12, 2022) ##
53
-
54
- * No changes.
55
-
56
-
57
- ## Rails 7.0.3 (May 09, 2022) ##
58
-
59
- * No changes.
18
+ * Remove change in the typography of user facing error messages.
19
+ For example, “can’t be blank” is again “can't be blank”.
60
20
 
21
+ *Rafael Mendonça França*
61
22
 
62
- ## Rails 7.0.2.4 (April 26, 2022) ##
63
-
64
- * No changes.
65
-
66
-
67
- ## Rails 7.0.2.3 (March 08, 2022) ##
68
-
69
- * No changes.
70
-
71
-
72
- ## Rails 7.0.2.2 (February 11, 2022) ##
73
-
74
- * No changes.
75
-
76
-
77
- ## Rails 7.0.2.1 (February 11, 2022) ##
78
-
79
- * No changes.
80
23
 
24
+ ## Rails 7.1.0.beta1 (September 13, 2023) ##
81
25
 
82
- ## Rails 7.0.2 (February 08, 2022) ##
26
+ * Support composite identifiers in `to_key`
83
27
 
84
- * Use different cache namespace for proxy calls
28
+ `to_key` avoids wrapping `#id` value into an `Array` if `#id` already an array
85
29
 
86
- Models can currently have different attribute bodies for the same method
87
- names, leading to conflicts. Adding a new namespace `:active_model_proxy`
88
- fixes the issue.
30
+ *Nikita Vasilevsky*
89
31
 
90
- *Chris Salzberg*
32
+ * Add `ActiveModel::Conversion.param_delimiter` to configure delimiter being used in `to_param`
91
33
 
34
+ *Nikita Vasilevsky*
92
35
 
93
- ## Rails 7.0.1 (January 06, 2022) ##
36
+ * `undefine_attribute_methods` undefines alias attribute methods along with attribute methods.
94
37
 
95
- * No changes.
38
+ *Nikita Vasilevsky*
96
39
 
40
+ * Error.full_message now strips ":base" from the message.
97
41
 
98
- ## Rails 7.0.0 (December 15, 2021) ##
42
+ *zzak*
99
43
 
100
- * No changes.
44
+ * Add a load hook for `ActiveModel::Model` (named `active_model`) to match the load hook for
45
+ `ActiveRecord::Base` and allow for overriding aspects of the `ActiveModel::Model` class.
101
46
 
47
+ *Lewis Buckley*
102
48
 
103
- ## Rails 7.0.0.rc3 (December 14, 2021) ##
49
+ * Improve password length validation in ActiveModel::SecurePassword to consider byte size for BCrypt
50
+ compatibility.
104
51
 
105
- * No changes.
52
+ The previous password length validation only considered the character count, which may not
53
+ accurately reflect the 72-byte size limit imposed by BCrypt. This change updates the validation
54
+ to consider both character count and byte size while keeping the character length validation in place.
106
55
 
56
+ ```ruby
57
+ user = User.new(password: "a" * 73) # 73 characters
58
+ user.valid? # => false
59
+ user.errors[:password] # => ["is too long"]
107
60
 
108
- ## Rails 7.0.0.rc2 (December 14, 2021) ##
109
61
 
110
- * No changes.
62
+ user = User.new(password: "あ" * 25) # 25 characters, 75 bytes
63
+ user.valid? # => false
64
+ user.errors[:password] # => ["is too long"]
65
+ ```
111
66
 
112
- ## Rails 7.0.0.rc1 (December 06, 2021) ##
67
+ *ChatGPT*, *Guillermo Iguaran*
113
68
 
114
- * Remove support to Marshal load Rails 5.x `ActiveModel::AttributeSet` format.
69
+ * `has_secure_password` now generates an `#{attribute}_salt` method that returns the salt
70
+ used to compute the password digest. The salt will change whenever the password is changed,
71
+ so it can be used to create single-use password reset tokens with `generates_token_for`:
115
72
 
116
- *Rafael Mendonça França*
73
+ ```ruby
74
+ class User < ActiveRecord::Base
75
+ has_secure_password
117
76
 
118
- * Remove support to Marshal and YAML load Rails 5.x error format.
77
+ generates_token_for :password_reset, expires_in: 15.minutes do
78
+ password_salt&.last(10)
79
+ end
80
+ end
81
+ ```
119
82
 
120
- *Rafael Mendonça França*
83
+ *Lázaro Nixon*
121
84
 
122
- * Remove deprecated support to use `[]=` in `ActiveModel::Errors#messages`.
85
+ * Improve typography of user facing error messages. In English contractions,
86
+ the Unicode APOSTROPHE (`U+0027`) is now RIGHT SINGLE QUOTATION MARK
87
+ (`U+2019`). For example, "can't be blank" is now "can’t be blank".
123
88
 
124
- *Rafael Mendonça França*
89
+ *Jon Dufresne*
125
90
 
126
- * Remove deprecated support to `delete` errors from `ActiveModel::Errors#messages`.
91
+ * Add class to `ActiveModel::MissingAttributeError` error message.
127
92
 
128
- *Rafael Mendonça França*
93
+ Show which class is missing the attribute in the error message:
129
94
 
130
- * Remove deprecated support to `clear` errors from `ActiveModel::Errors#messages`.
95
+ ```ruby
96
+ user = User.first
97
+ user.pets.select(:id).first.user_id
98
+ # => ActiveModel::MissingAttributeError: missing attribute 'user_id' for Pet
99
+ ```
131
100
 
132
- *Rafael Mendonça França*
101
+ *Petrik de Heus*
133
102
 
134
- * Remove deprecated support concat errors to `ActiveModel::Errors#messages`.
103
+ * Raise `NoMethodError` in `ActiveModel::Type::Value#as_json` to avoid unpredictable
104
+ results.
135
105
 
136
- *Rafael Mendonça França*
106
+ *Vasiliy Ermolovich*
137
107
 
138
- * Remove deprecated `ActiveModel::Errors#to_xml`.
108
+ * Custom attribute types that inherit from Active Model built-in types and do
109
+ not override the `serialize` method will now benefit from an optimization
110
+ when serializing attribute values for the database.
139
111
 
140
- *Rafael Mendonça França*
112
+ For example, with a custom type like the following:
141
113
 
142
- * Remove deprecated `ActiveModel::Errors#keys`.
114
+ ```ruby
115
+ class DowncasedString < ActiveModel::Type::String
116
+ def cast(value)
117
+ super&.downcase
118
+ end
119
+ end
143
120
 
144
- *Rafael Mendonça França*
121
+ ActiveRecord::Type.register(:downcased_string, DowncasedString)
145
122
 
146
- * Remove deprecated `ActiveModel::Errors#values`.
123
+ class User < ActiveRecord::Base
124
+ attribute :email, :downcased_string
125
+ end
147
126
 
148
- *Rafael Mendonça França*
127
+ user = User.new(email: "FooBar@example.com")
128
+ ```
149
129
 
150
- * Remove deprecated `ActiveModel::Errors#slice!`.
130
+ Serializing the `email` attribute for the database will be roughly twice as
131
+ fast. More expensive `cast` operations will likely see greater improvements.
151
132
 
152
- *Rafael Mendonça França*
133
+ *Jonathan Hefner*
153
134
 
154
- * Remove deprecated `ActiveModel::Errors#to_h`.
135
+ * `has_secure_password` now supports password challenges via a
136
+ `password_challenge` accessor and validation.
155
137
 
156
- *Rafael Mendonça França*
138
+ A password challenge is a safeguard to verify that the current user is
139
+ actually the password owner. It can be used when changing sensitive model
140
+ fields, such as the password itself. It is different than a password
141
+ confirmation, which is used to prevent password typos.
157
142
 
158
- * Remove deprecated enumeration of `ActiveModel::Errors` instances as a Hash.
143
+ When `password_challenge` is set, the validation checks that the value's
144
+ digest matches the *currently persisted* `password_digest` (i.e.
145
+ `password_digest_was`).
159
146
 
160
- *Rafael Mendonça França*
147
+ This allows a password challenge to be done as part of a typical `update`
148
+ call, just like a password confirmation. It also allows a password
149
+ challenge error to be handled in the same way as other validation errors.
161
150
 
162
- * Clear secure password cache if password is set to `nil`
151
+ For example, in the controller, instead of:
163
152
 
164
- Before:
153
+ ```ruby
154
+ password_params = params.require(:password).permit(
155
+ :password_challenge,
156
+ :password,
157
+ :password_confirmation,
158
+ )
165
159
 
166
- user.password = 'something'
167
- user.password = nil
160
+ password_challenge = password_params.delete(:password_challenge)
161
+ @password_challenge_failed = !current_user.authenticate(password_challenge)
168
162
 
169
- user.password # => 'something'
163
+ if !@password_challenge_failed && current_user.update(password_params)
164
+ # ...
165
+ end
166
+ ```
170
167
 
171
- Now:
168
+ You can now write:
172
169
 
173
- user.password = 'something'
174
- user.password = nil
170
+ ```ruby
171
+ password_params = params.require(:password).permit(
172
+ :password_challenge,
173
+ :password,
174
+ :password_confirmation,
175
+ ).with_defaults(password_challenge: "")
176
+
177
+ if current_user.update(password_params)
178
+ # ...
179
+ end
180
+ ```
175
181
 
176
- user.password # => nil
182
+ And, in the view, instead of checking `@password_challenge_failed`, you can
183
+ render an error for the `password_challenge` field just as you would for
184
+ other form fields, including utilizing `config.action_view.field_error_proc`.
177
185
 
178
- *Markus Doits*
186
+ *Jonathan Hefner*
179
187
 
180
- ## Rails 7.0.0.alpha2 (September 15, 2021) ##
188
+ * Support infinite ranges for `LengthValidator`s `:in`/`:within` options
181
189
 
182
- * No changes.
190
+ ```ruby
191
+ validates_length_of :first_name, in: ..30
192
+ ```
183
193
 
194
+ *fatkodima*
184
195
 
185
- ## Rails 7.0.0.alpha1 (September 15, 2021) ##
196
+ * Add support for beginless ranges to inclusivity/exclusivity validators:
186
197
 
187
- * Introduce `ActiveModel::API`.
198
+ ```ruby
199
+ validates_inclusion_of :birth_date, in: -> { (..Date.today) }
200
+ ```
188
201
 
189
- Make `ActiveModel::API` the minimum API to talk with Action Pack and Action View.
190
- This will allow adding more functionality to `ActiveModel::Model`.
202
+ ```ruby
203
+ validates_exclusion_of :birth_date, in: -> { (..Date.today) }
204
+ ```
191
205
 
192
- *Petrik de Heus*, *Nathaniel Watts*
206
+ *Bo Jeanes*
193
207
 
194
- * Fix dirty check for Float::NaN and BigDecimal::NaN.
208
+ * Make validators accept lambdas without record argument
195
209
 
196
- Float::NaN and BigDecimal::NaN in Ruby are [special values](https://bugs.ruby-lang.org/issues/1720)
197
- and can't be compared with `==`.
210
+ ```ruby
211
+ # Before
212
+ validates_comparison_of :birth_date, less_than_or_equal_to: ->(_record) { Date.today }
198
213
 
199
- *Marcelo Lauxen*
214
+ # After
215
+ validates_comparison_of :birth_date, less_than_or_equal_to: -> { Date.today }
216
+ ```
200
217
 
201
- * Fix `to_json` for `ActiveModel::Dirty` object.
218
+ *fatkodima*
202
219
 
203
- Exclude `mutations_from_database` attribute from json as it lead to recursion.
220
+ * Fix casting long strings to `Date`, `Time` or `DateTime`
204
221
 
205
- *Anil Maurya*
222
+ *fatkodima*
206
223
 
207
- * Add `ActiveModel::AttributeSet#values_for_database`.
224
+ * Use different cache namespace for proxy calls
208
225
 
209
- Returns attributes with values for assignment to the database.
226
+ Models can currently have different attribute bodies for the same method
227
+ names, leading to conflicts. Adding a new namespace `:active_model_proxy`
228
+ fixes the issue.
210
229
 
211
230
  *Chris Salzberg*
212
231
 
213
- * Fix delegation in ActiveModel::Type::Registry#lookup and ActiveModel::Type.lookup.
214
-
215
- Passing a last positional argument `{}` would be incorrectly considered as keyword argument.
216
-
217
- *Benoit Daloze*
218
-
219
- * Cache and re-use generated attribute methods.
220
-
221
- Generated methods with identical implementations will now share their instruction sequences
222
- leading to reduced memory retention, and slightly faster load time.
223
-
224
- *Jean Boussier*
225
-
226
- * Add `in: range` parameter to `numericality` validator.
227
-
228
- *Michal Papis*
229
-
230
- * Add `locale` argument to `ActiveModel::Name#initialize` to be used to generate the `singular`,
231
- `plural`, `route_key` and `singular_route_key` values.
232
-
233
- *Lukas Pokorny*
234
-
235
- * Make ActiveModel::Errors#inspect slimmer for readability
236
-
237
- *lulalala*
238
-
239
- Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/activemodel/CHANGELOG.md) for previous changes.
232
+ Please check [7-0-stable](https://github.com/rails/rails/blob/7-0-stable/activemodel/CHANGELOG.md) for previous changes.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2004-2022 David Heinemeier Hansson
1
+ Copyright (c) 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
@@ -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://edgeguides.rubyonrails.org/active_model_basics.html] guide.
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 <tt>ActiveModel::Lint::Tests</tt>.
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: <tt>ActiveModel::API</tt>.
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 <tt>ActiveModel::API</tt> for more examples.
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
- <tt>ActiveModel::Serialization</tt> provides a standard interface for your object
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
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveModel
4
- # == Active \Model \API
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, <tt>ActiveModel::API</tt> implements <tt>persisted?</tt>
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 <tt>initialize</tt>, make
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 <tt>ActiveModel::API</tt>
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.serialize(value)
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 simply dup this attribute to avoid
182
+ # 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
+ dup
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 <tt>ActiveModel::ForbiddenAttributesError</tt>
13
+ # of this method is +false+ an ActiveModel::ForbiddenAttributesError
14
14
  # exception is raised.
15
15
  #
16
16
  # class Cat