mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
handle not only strings in date type cast
This commit is contained in:
parent
3c1884e7dd
commit
e2d19d6018
2 changed files with 19 additions and 7 deletions
|
@ -80,7 +80,7 @@ module ActiveRecord
|
||||||
when :decimal then klass.value_to_decimal(value)
|
when :decimal then klass.value_to_decimal(value)
|
||||||
when :datetime, :timestamp then klass.string_to_time(value)
|
when :datetime, :timestamp then klass.string_to_time(value)
|
||||||
when :time then klass.string_to_dummy_time(value)
|
when :time then klass.string_to_dummy_time(value)
|
||||||
when :date then klass.string_to_date(value)
|
when :date then klass.value_to_date(value)
|
||||||
when :binary then klass.binary_to_string(value)
|
when :binary then klass.binary_to_string(value)
|
||||||
when :boolean then klass.value_to_boolean(value)
|
when :boolean then klass.value_to_boolean(value)
|
||||||
else value
|
else value
|
||||||
|
@ -97,7 +97,7 @@ module ActiveRecord
|
||||||
when :decimal then "#{klass}.value_to_decimal(#{var_name})"
|
when :decimal then "#{klass}.value_to_decimal(#{var_name})"
|
||||||
when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
|
when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
|
||||||
when :time then "#{klass}.string_to_dummy_time(#{var_name})"
|
when :time then "#{klass}.string_to_dummy_time(#{var_name})"
|
||||||
when :date then "#{klass}.string_to_date(#{var_name})"
|
when :date then "#{klass}.value_to_date(#{var_name})"
|
||||||
when :binary then "#{klass}.binary_to_string(#{var_name})"
|
when :binary then "#{klass}.binary_to_string(#{var_name})"
|
||||||
when :boolean then "#{klass}.value_to_boolean(#{var_name})"
|
when :boolean then "#{klass}.value_to_boolean(#{var_name})"
|
||||||
else var_name
|
else var_name
|
||||||
|
@ -132,11 +132,15 @@ module ActiveRecord
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
def string_to_date(string)
|
def value_to_date(value)
|
||||||
return string unless string.is_a?(String)
|
if value.is_a?(String)
|
||||||
return nil if string.empty?
|
return nil if value.empty?
|
||||||
|
fast_string_to_date(value) || fallback_string_to_date(value)
|
||||||
fast_string_to_date(string) || fallback_string_to_date(string)
|
elsif value.respond_to?(:to_date)
|
||||||
|
value.to_date
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def string_to_time(string)
|
def string_to_time(string)
|
||||||
|
|
|
@ -518,6 +518,14 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_write_time_to_date_attributes
|
||||||
|
in_time_zone "Pacific Time (US & Canada)" do
|
||||||
|
record = @target.new
|
||||||
|
record.last_read = Time.utc(2010, 1, 1, 10)
|
||||||
|
assert_equal Date.civil(2010, 1, 1), record.last_read
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_time_attributes_are_retrieved_in_current_time_zone
|
def test_time_attributes_are_retrieved_in_current_time_zone
|
||||||
in_time_zone "Pacific Time (US & Canada)" do
|
in_time_zone "Pacific Time (US & Canada)" do
|
||||||
utc_time = Time.utc(2008, 1, 1)
|
utc_time = Time.utc(2008, 1, 1)
|
||||||
|
|
Loading…
Reference in a new issue