提交 fe9547b6 编写于 作者: R Ryuta Kamizono

Parse raw value only when a value came from user in numericality validator

Since `parse_raw_value_as_a_number` may not always parse raw value from
database as a number without type casting (e.g. "$150.55" as money
format).

Fixes #32531.
上级 17bf6203
......@@ -19,9 +19,11 @@ def check_validity!
end
def validate_each(record, attr_name, value)
before_type_cast = :"#{attr_name}_before_type_cast"
came_from_user = :"#{attr_name}_came_from_user?"
raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast) && record.send(before_type_cast) != value
if record.respond_to?(came_from_user) && record.public_send(came_from_user)
raw_value = record.read_attribute_before_type_cast(attr_name)
end
raw_value ||= value
if record_attribute_changed_in_place?(record, attr_name)
......
......@@ -6,7 +6,9 @@
class PostgresqlMoneyTest < ActiveRecord::PostgreSQLTestCase
include SchemaDumpingHelper
class PostgresqlMoney < ActiveRecord::Base; end
class PostgresqlMoney < ActiveRecord::Base
validates :depth, numericality: true
end
setup do
@connection = ActiveRecord::Base.connection
......@@ -35,6 +37,7 @@ def test_column
def test_default
assert_equal BigDecimal("150.55"), PostgresqlMoney.column_defaults["depth"]
assert_equal BigDecimal("150.55"), PostgresqlMoney.new.depth
assert_equal "$150.55", PostgresqlMoney.new.depth_before_type_cast
end
def test_money_values
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册