aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/type/date.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 18:10:54 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-20 18:10:54 -0300
commit52b558695d5a1b33ff21516cbc71d1ae1dee4632 (patch)
tree3bfcd3aaca10539014e352c5f1582175f42375e6 /activerecord/lib/active_record/connection_adapters/type/date.rb
parent90a82cbfb26a24f801864a7fd2664553876b51f3 (diff)
parent8f387995054d9ce79522da8ef65fba28c6ea0785 (diff)
downloadrails-52b558695d5a1b33ff21516cbc71d1ae1dee4632.tar.gz
rails-52b558695d5a1b33ff21516cbc71d1ae1dee4632.tar.bz2
rails-52b558695d5a1b33ff21516cbc71d1ae1dee4632.zip
Merge pull request #15207 from sgrif/sg-inline-column-helpers
Inline typecasting helpers from Column to the appropriate types
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/type/date.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/type/date.rb25
1 files changed, 24 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/type/date.rb b/activerecord/lib/active_record/connection_adapters/type/date.rb
index 9208da6efe..599dbd320c 100644
--- a/activerecord/lib/active_record/connection_adapters/type/date.rb
+++ b/activerecord/lib/active_record/connection_adapters/type/date.rb
@@ -9,7 +9,30 @@ module ActiveRecord
private
def cast_value(value)
- Column.value_to_date(value)
+ if value.is_a?(::String)
+ return 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 fast_string_to_date(string)
+ if string =~ Column::Format::ISO_DATE
+ new_date $1.to_i, $2.to_i, $3.to_i
+ end
+ end
+
+ def fallback_string_to_date(string)
+ new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
+ end
+
+ def new_date(year, mon, mday)
+ if year && year != 0
+ ::Date.new(year, mon, mday) rescue nil
+ end
end
end
end