1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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.
This commit is contained in:
Ryuta Kamizono 2018-05-25 12:05:50 +09:00
parent 17bf62033e
commit fe9547b6fb
2 changed files with 8 additions and 3 deletions

View file

@ -19,9 +19,11 @@ module ActiveModel
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)

View file

@ -6,7 +6,9 @@ require "support/schema_dumping_helper"
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 @@ class PostgresqlMoneyTest < ActiveRecord::PostgreSQLTestCase
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