diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2015-02-13 03:04:47 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-02-20 10:25:31 +0900 |
commit | a088ee96918a938159cbeee1a3fe822f0bb46b5d (patch) | |
tree | 9b09d02933d2bc2cbed740e9daa1cc68ccad328a /activerecord/lib/active_record | |
parent | 9ef870c0426f573355a10b04fd9740550492cd00 (diff) | |
download | rails-a088ee96918a938159cbeee1a3fe822f0bb46b5d.tar.gz rails-a088ee96918a938159cbeee1a3fe822f0bb46b5d.tar.bz2 rails-a088ee96918a938159cbeee1a3fe822f0bb46b5d.zip |
Format the time string according to the precision of the time column
It is also necessary to format a time column like a datetime column.
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 20 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index c11f0c93b4..8db4bcd7e3 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -75,7 +75,7 @@ module ActiveRecord def prepare_column_options(column) spec = super - spec.delete(:precision) if column.type == :datetime && column.precision == 0 + spec.delete(:precision) if /time/ === column.sql_type && column.precision == 0 spec end @@ -733,7 +733,7 @@ module ActiveRecord end def extract_precision(sql_type) - if /datetime/ === sql_type + if /time/ === sql_type super || 0 else super diff --git a/activerecord/lib/active_record/type/date_time.rb b/activerecord/lib/active_record/type/date_time.rb index 05d2af3808..a5199959b9 100644 --- a/activerecord/lib/active_record/type/date_time.rb +++ b/activerecord/lib/active_record/type/date_time.rb @@ -10,24 +10,6 @@ module ActiveRecord :datetime end - def serialize(value) - if precision && value.respond_to?(:usec) - number_of_insignificant_digits = 6 - precision - round_power = 10 ** number_of_insignificant_digits - value = value.change(usec: value.usec / round_power * round_power) - end - - if value.acts_like?(:time) - zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal - - if value.respond_to?(zone_conversion_method) - value = value.send(zone_conversion_method) - end - end - - value - end - private def cast_value(string) diff --git a/activerecord/lib/active_record/type/helpers/time_value.rb b/activerecord/lib/active_record/type/helpers/time_value.rb index 6e14c3a9b5..7eb41557cb 100644 --- a/activerecord/lib/active_record/type/helpers/time_value.rb +++ b/activerecord/lib/active_record/type/helpers/time_value.rb @@ -2,6 +2,24 @@ module ActiveRecord module Type module Helpers module TimeValue # :nodoc: + def serialize(value) + if precision && value.respond_to?(:usec) + number_of_insignificant_digits = 6 - precision + round_power = 10 ** number_of_insignificant_digits + value = value.change(usec: value.usec / round_power * round_power) + end + + if value.acts_like?(:time) + zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal + + if value.respond_to?(zone_conversion_method) + value = value.send(zone_conversion_method) + end + end + + value + end + def type_cast_for_schema(value) "'#{value.to_s(:db)}'" end |