diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2016-04-13 22:30:35 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2016-04-13 22:30:35 -0700 |
commit | 53ab1ee5652ead8d5e52e5a9c177a6f8a32c75b1 (patch) | |
tree | 5376680dc357962506b982cc9b4f6f82d2d263ab /activerecord/lib | |
parent | 128923ca14eeae032c2ed72b043e2bbcda09ab9c (diff) | |
parent | 28ec8c4a57197a43e4369bfbdfa92625bd592fe0 (diff) | |
download | rails-53ab1ee5652ead8d5e52e5a9c177a6f8a32c75b1.tar.gz rails-53ab1ee5652ead8d5e52e5a9c177a6f8a32c75b1.tar.bz2 rails-53ab1ee5652ead8d5e52e5a9c177a6f8a32c75b1.zip |
Merge pull request #24542 from kamipo/add_quoted_time
Add `quoted_time` for truncating the date part of a time column value
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/quoting.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/type/time.rb | 12 |
2 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 2eeefb13d7..860ef17dca 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -146,6 +146,10 @@ module ActiveRecord end end + def quoted_time(value) # :nodoc: + quoted_date(value).sub(/\A2000-01-01 /, '') + end + def prepare_binds_for_database(binds) # :nodoc: binds.map(&:value_for_database) end @@ -166,6 +170,7 @@ module ActiveRecord # BigDecimals need to be put in a non-normalized form and quoted. when BigDecimal then value.to_s('F') when Numeric, ActiveSupport::Duration then value.to_s + when Type::Time::Value then "'#{quoted_time(value)}'" when Date, Time then "'#{quoted_date(value)}'" when Symbol then "'#{quote_string(value.to_s)}'" when Class then "'#{value}'" @@ -181,6 +186,7 @@ module ActiveRecord when false then unquoted_false # BigDecimals need to be put in a non-normalized form and quoted. when BigDecimal then value.to_s('F') + when Type::Time::Value then quoted_time(value) when Date, Time then quoted_date(value) when *types_which_need_no_typecasting value diff --git a/activerecord/lib/active_record/type/time.rb b/activerecord/lib/active_record/type/time.rb index 70988d84ff..7da49e43c7 100644 --- a/activerecord/lib/active_record/type/time.rb +++ b/activerecord/lib/active_record/type/time.rb @@ -2,6 +2,18 @@ module ActiveRecord module Type class Time < ActiveModel::Type::Time include Internal::Timezone + + class Value < DelegateClass(::Time) # :nodoc: + end + + def serialize(value) + case value = super + when ::Time + Value.new(value) + else + value + end + end end end end |