diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-19 16:53:44 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-19 16:53:44 -0300 |
commit | 8f42372ef1d102bc1b7fe067e181d9ff4dca70b1 (patch) | |
tree | 2003c7b05cad6a50a63e31128e4b6ef0472368e5 /activerecord | |
parent | d4ca0a44c7564f939bc3df2f770083d709c13e95 (diff) | |
parent | 0aecb473c34b5df3b020ef4ede6d8e36f9009fd7 (diff) | |
download | rails-8f42372ef1d102bc1b7fe067e181d9ff4dca70b1.tar.gz rails-8f42372ef1d102bc1b7fe067e181d9ff4dca70b1.tar.bz2 rails-8f42372ef1d102bc1b7fe067e181d9ff4dca70b1.zip |
Merge pull request #15814 from sgrif/sg-number-changed
Further simplify `changed?` conditional for numeric types
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/type/numeric.rb | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/type/numeric.rb b/activerecord/lib/active_record/type/numeric.rb index 137c9e4c99..a7bf0657b9 100644 --- a/activerecord/lib/active_record/type/numeric.rb +++ b/activerecord/lib/active_record/type/numeric.rb @@ -16,26 +16,20 @@ module ActiveRecord end def changed?(old_value, _new_value, new_value_before_type_cast) # :nodoc: - # 0 => 'wibble' should mark as changed so numericality validations run - if nil_or_zero?(old_value) && non_numeric_string?(new_value_before_type_cast) - # nil => '' should not mark as changed - old_value != new_value_before_type_cast.presence - else - super - end + super || zero_to_non_number?(old_value, new_value_before_type_cast) end private + def zero_to_non_number?(old_value, new_value_before_type_cast) + old_value == 0 && non_numeric_string?(new_value_before_type_cast) + end + def non_numeric_string?(value) # 'wibble'.to_i will give zero, we want to make sure # that we aren't marking int zero to string zero as # changed. - value !~ /\A\d+\.?\d*\z/ - end - - def nil_or_zero?(value) - value.nil? || value == 0 + value.to_s !~ /\A\d+\.?\d*\z/ end end end |