aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-12-20 09:42:17 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-12-20 09:42:17 -0800
commit3ef20aa62c11b2fbb709fcbf7aaa2fc982d27d5e (patch)
tree1fde65288c83ca1dfe6626c7e6aa8966d5476149 /activerecord/lib/active_record
parent7ba28d434ceb0401f937e77f19d1c31e2a43f1ac (diff)
parente2d19d60182b017bb092a018564d329c4f52b6a3 (diff)
downloadrails-3ef20aa62c11b2fbb709fcbf7aaa2fc982d27d5e.tar.gz
rails-3ef20aa62c11b2fbb709fcbf7aaa2fc982d27d5e.tar.bz2
rails-3ef20aa62c11b2fbb709fcbf7aaa2fc982d27d5e.zip
Merge pull request #4035 from lest/date-type-cast
handle not only strings in date type cast
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb18
1 files changed, 11 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index a7856539b7..0214154c51 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -80,7 +80,7 @@ module ActiveRecord
when :decimal then klass.value_to_decimal(value)
when :datetime, :timestamp then klass.string_to_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 :boolean then klass.value_to_boolean(value)
else value
@@ -97,7 +97,7 @@ module ActiveRecord
when :decimal then "#{klass}.value_to_decimal(#{var_name})"
when :datetime, :timestamp then "#{klass}.string_to_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 :boolean then "#{klass}.value_to_boolean(#{var_name})"
else var_name
@@ -132,11 +132,15 @@ module ActiveRecord
value
end
- def string_to_date(string)
- return string unless string.is_a?(String)
- return nil if string.empty?
-
- fast_string_to_date(string) || fallback_string_to_date(string)
+ def value_to_date(value)
+ if value.is_a?(String)
+ return nil if value.empty?
+ fast_string_to_date(value) || fallback_string_to_date(value)
+ elsif value.respond_to?(:to_date)
+ value.to_date
+ else
+ value
+ end
end
def string_to_time(string)