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

Per #9999 revert the revert changing so columns are only tested for empty? as opposed to blank?

This is both faster and more correct, added tests to make sure this is not reverted again.
This commit is contained in:
Sam 2013-04-03 12:14:48 +11:00
parent fc11375584
commit 0a57f34601
2 changed files with 29 additions and 15 deletions

View file

@ -161,7 +161,7 @@ module ActiveRecord
def value_to_date(value)
if value.is_a?(String)
return nil if value.blank?
return nil if value.empty?
fast_string_to_date(value) || fallback_string_to_date(value)
elsif value.respond_to?(:to_date)
value.to_date
@ -172,14 +172,14 @@ module ActiveRecord
def string_to_time(string)
return string unless string.is_a?(String)
return nil if string.blank?
return nil if string.empty?
fast_string_to_time(string) || fallback_string_to_time(string)
end
def string_to_dummy_time(string)
return string unless string.is_a?(String)
return nil if string.blank?
return nil if string.empty?
dummy_time_string = "2000-01-01 #{string}"
@ -192,7 +192,7 @@ module ActiveRecord
# convert something to a boolean
def value_to_boolean(value)
if value.is_a?(String) && value.blank?
if value.is_a?(String) && value.empty?
nil
else
TRUE_VALUES.include?(value)

View file

@ -6,6 +6,9 @@ module ActiveRecord
class ColumnTest < ActiveRecord::TestCase
def test_type_cast_boolean
column = Column.new("field", nil, "boolean")
assert column.type_cast('').nil?
assert column.type_cast(nil).nil?
assert column.type_cast(true)
assert column.type_cast(1)
assert column.type_cast('1')
@ -15,15 +18,21 @@ module ActiveRecord
assert column.type_cast('TRUE')
assert column.type_cast('on')
assert column.type_cast('ON')
assert !column.type_cast(false)
assert !column.type_cast(0)
assert !column.type_cast('0')
assert !column.type_cast('f')
assert !column.type_cast('F')
assert !column.type_cast('false')
assert !column.type_cast('FALSE')
assert !column.type_cast('off')
assert !column.type_cast('OFF')
# explicitly check for false vs nil
assert_equal false, column.type_cast(false)
assert_equal false, column.type_cast(0)
assert_equal false, column.type_cast('0')
assert_equal false, column.type_cast('f')
assert_equal false, column.type_cast('F')
assert_equal false, column.type_cast('false')
assert_equal false, column.type_cast('FALSE')
assert_equal false, column.type_cast('off')
assert_equal false, column.type_cast('OFF')
assert_equal false, column.type_cast(' ')
assert_equal false, column.type_cast("\u3000\r\n")
assert_equal false, column.type_cast("\u0000")
assert_equal false, column.type_cast('SOMETHING RANDOM')
end
def test_type_cast_integer
@ -65,8 +74,9 @@ module ActiveRecord
def test_type_cast_time
column = Column.new("field", nil, "time")
assert_equal nil, column.type_cast(nil)
assert_equal nil, column.type_cast('')
assert_equal nil, column.type_cast(' ')
assert_equal nil, column.type_cast('ABC')
time_string = Time.now.utc.strftime("%T")
assert_equal time_string, column.type_cast(time_string).strftime("%T")
@ -74,8 +84,10 @@ module ActiveRecord
def test_type_cast_datetime_and_timestamp
[Column.new("field", nil, "datetime"), Column.new("field", nil, "timestamp")].each do |column|
assert_equal nil, column.type_cast(nil)
assert_equal nil, column.type_cast('')
assert_equal nil, column.type_cast(' ')
assert_equal nil, column.type_cast('ABC')
datetime_string = Time.now.utc.strftime("%FT%T")
assert_equal datetime_string, column.type_cast(datetime_string).strftime("%FT%T")
@ -84,8 +96,10 @@ module ActiveRecord
def test_type_cast_date
column = Column.new("field", nil, "date")
assert_equal nil, column.type_cast(nil)
assert_equal nil, column.type_cast('')
assert_equal nil, column.type_cast(' ')
assert_equal nil, column.type_cast(' ')
assert_equal nil, column.type_cast('ABC')
date_string = Time.now.utc.strftime("%F")
assert_equal date_string, column.type_cast(date_string).strftime("%F")