aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-08-28 17:23:48 +0900
committerGitHub <noreply@github.com>2018-08-28 17:23:48 +0900
commit37075aa7f5c2c779a981debd21aa8c468cce4d72 (patch)
treed84791f45de533765855ae6b556e3c1e6ea48ca6 /activemodel/lib/active_model
parent1353610ff2ab4d16d022d5c31d5b4e5d908e05a8 (diff)
parent47a6d788ddbab08b2a04c72cd80352aac44090ab (diff)
downloadrails-37075aa7f5c2c779a981debd21aa8c468cce4d72.tar.gz
rails-37075aa7f5c2c779a981debd21aa8c468cce4d72.tar.bz2
rails-37075aa7f5c2c779a981debd21aa8c468cce4d72.zip
Merge pull request #33654 from kamipo/fix_numericality_validator_2
Fix numericality validator to still use value before type cast except Active Record
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/validations/numericality.rb15
1 files changed, 11 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/validations/numericality.rb b/activemodel/lib/active_model/validations/numericality.rb
index 3753040316..126a87ac6e 100644
--- a/activemodel/lib/active_model/validations/numericality.rb
+++ b/activemodel/lib/active_model/validations/numericality.rb
@@ -21,10 +21,17 @@ module ActiveModel
def validate_each(record, attr_name, value)
came_from_user = :"#{attr_name}_came_from_user?"
- if record.respond_to?(came_from_user) && record.public_send(came_from_user)
- raw_value = record.read_attribute_before_type_cast(attr_name)
- elsif record.respond_to?(:read_attribute)
- raw_value = record.read_attribute(attr_name)
+ if record.respond_to?(came_from_user)
+ if record.public_send(came_from_user)
+ raw_value = record.read_attribute_before_type_cast(attr_name)
+ elsif record.respond_to?(:read_attribute)
+ raw_value = record.read_attribute(attr_name)
+ end
+ else
+ before_type_cast = :"#{attr_name}_before_type_cast"
+ if record.respond_to?(before_type_cast)
+ raw_value = record.public_send(before_type_cast)
+ end
end
raw_value ||= value