提交 8f42372e 编写于 作者: R Rafael Mendonça França

Merge pull request #15814 from sgrif/sg-number-changed

Further simplify `changed?` conditional for numeric types
......@@ -16,26 +16,20 @@ def type_cast(value)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册