diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-08-19 14:57:05 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-08-24 00:44:02 +0900 |
commit | 47a6d788ddbab08b2a04c72cd80352aac44090ab (patch) | |
tree | a3dbe86af22b7357be32a33a670e408ef1dcad57 /activemodel/test/models | |
parent | a0b57bbb21ce81071220bd8c6cfd8cdda342c6c6 (diff) | |
download | rails-47a6d788ddbab08b2a04c72cd80352aac44090ab.tar.gz rails-47a6d788ddbab08b2a04c72cd80352aac44090ab.tar.bz2 rails-47a6d788ddbab08b2a04c72cd80352aac44090ab.zip |
Fix numericality validator to still use value before type cast except Active Record
The purpose of fe9547b is to work type casting to value from database.
But that was caused not to use the value before type cast even except
Active Record.
There we never guarantees that the value before type cast was going to
the used in this validation, but we should not change the behavior
unless there is some particular reason.
To restore original behavior, still use the value before type cast if
`came_from_user?` is undefined (i.e. except Active Record).
Fixes #33651.
Fixes #33686.
Diffstat (limited to 'activemodel/test/models')
-rw-r--r-- | activemodel/test/models/topic.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/activemodel/test/models/topic.rb b/activemodel/test/models/topic.rb index b0af00ee45..db3284f833 100644 --- a/activemodel/test/models/topic.rb +++ b/activemodel/test/models/topic.rb @@ -3,6 +3,11 @@ class Topic include ActiveModel::Validations include ActiveModel::Validations::Callbacks + include ActiveModel::AttributeMethods + include ActiveSupport::NumberHelper + + attribute_method_suffix "_before_type_cast" + define_attribute_method :price def self._validates_default_keys super | [ :message ] @@ -10,6 +15,7 @@ class Topic attr_accessor :title, :author_name, :content, :approved, :created_at attr_accessor :after_validation_performed + attr_writer :price after_validation :perform_after_validation @@ -38,4 +44,12 @@ class Topic def my_validation_with_arg(attr) errors.add attr, "is missing" unless send(attr) end + + def price + number_to_currency @price + end + + def attribute_before_type_cast(attr) + instance_variable_get(:"@#{attr}") + end end |