aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-07-17 11:14:11 -0400
committerSean Griffin <sean@seantheprogrammer.com>2017-07-17 11:19:15 -0400
commit1519e976b224871c7f7dd476351930d5d0d7faf6 (patch)
tree994e25c414d75ce825b17cd3c6960b10a3a06c8d /activemodel
parent828b05f5f9f8decf55170ea75bfdc6d37787993d (diff)
downloadrails-1519e976b224871c7f7dd476351930d5d0d7faf6.tar.gz
rails-1519e976b224871c7f7dd476351930d5d0d7faf6.tar.bz2
rails-1519e976b224871c7f7dd476351930d5d0d7faf6.zip
Allow multiparameter assigned attributes to be used with `text_field`
Between 4.2 and 5.0 the behavior of how multiparameter attributes interact with `_before_type_cast` changed. In 4.2 it returns the post-type-cast value. After 5.0, it returns the hash that gets sent to the type. This behavior is correct, but will cause an issue if you then tried to render that value in an input like `text_field` or `hidden_field`. In this case, we want those fields to use the post-type-cast form, instead of the `_before_type_cast` (the main reason it uses `_before_type_cast` at all is to avoid losing data when casting a non-numeric string to integer). I've opted to modify `came_from_user?` rather than introduce a new method for this as I want to avoid complicating that contract further, and technically the multiparameter hash didn't come from assignment, it was constructed internally by AR. Close #27888.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb4
-rw-r--r--activemodel/lib/active_model/type/value.rb4
2 files changed, 8 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb b/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb
index e2a8271142..ad891f841e 100644
--- a/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb
+++ b/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb
@@ -21,6 +21,10 @@ module ActiveModel
end
end
+ define_method(:value_constructed_by_mass_assignment?) do |value|
+ value.is_a?(Hash)
+ end
+
define_method(:value_from_multiparameter_assignment) do |values_hash|
defaults.each do |k, v|
values_hash[k] ||= v
diff --git a/activemodel/lib/active_model/type/value.rb b/activemodel/lib/active_model/type/value.rb
index a484d9958e..a8ea6a2c22 100644
--- a/activemodel/lib/active_model/type/value.rb
+++ b/activemodel/lib/active_model/type/value.rb
@@ -86,6 +86,10 @@ module ActiveModel
false
end
+ def value_constructed_by_mass_assignment?(_value) # :nodoc:
+ false
+ end
+
def map(value) # :nodoc:
yield value
end