mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Don't type cast values that don't respond to to_i to 1
This commit is contained in:
parent
c435feb404
commit
13823a4cf3
2 changed files with 25 additions and 1 deletions
|
@ -95,7 +95,7 @@ module ActiveRecord
|
|||
|
||||
case type
|
||||
when :string, :text then value
|
||||
when :integer then value.to_i rescue value ? 1 : 0
|
||||
when :integer then value.to_i
|
||||
when :float then value.to_f
|
||||
when :decimal then klass.value_to_decimal(value)
|
||||
when :datetime, :timestamp then klass.string_to_time(value)
|
||||
|
|
|
@ -24,6 +24,30 @@ module ActiveRecord
|
|||
assert !column.type_cast('off')
|
||||
assert !column.type_cast('OFF')
|
||||
end
|
||||
|
||||
def test_type_cast_integer
|
||||
column = Column.new("field", nil, "integer")
|
||||
assert_equal 1, column.type_cast(1)
|
||||
assert_equal 1, column.type_cast('1')
|
||||
assert_equal 1, column.type_cast('1ignore')
|
||||
assert_equal 0, column.type_cast('bad1')
|
||||
assert_equal 0, column.type_cast('bad')
|
||||
assert_equal 1, column.type_cast(1.7)
|
||||
assert_nil column.type_cast(nil)
|
||||
end
|
||||
|
||||
def test_type_cast_non_integer_to_integer
|
||||
column = Column.new("field", nil, "integer")
|
||||
assert_raises(NoMethodError) do
|
||||
column.type_cast([])
|
||||
end
|
||||
assert_raises(NoMethodError) do
|
||||
column.type_cast(true)
|
||||
end
|
||||
assert_raises(NoMethodError) do
|
||||
column.type_cast(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue