diff options
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/type/date_time.rb | 8 | ||||
-rw-r--r-- | activemodel/lib/active_model/type/helpers/time_value.rb | 13 |
2 files changed, 12 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/type/date_time.rb b/activemodel/lib/active_model/type/date_time.rb index b068cfc672..2f2df4320f 100644 --- a/activemodel/lib/active_model/type/date_time.rb +++ b/activemodel/lib/active_model/type/date_time.rb @@ -12,11 +12,11 @@ module ActiveModel private - def cast_value(string) - return string unless string.is_a?(::String) - return if string.empty? + def cast_value(value) + return apply_seconds_precision(value) unless value.is_a?(::String) + return if value.empty? - fast_string_to_time(string) || fallback_string_to_time(string) + fast_string_to_time(value) || fallback_string_to_time(value) end # '0.123456' -> 123456 diff --git a/activemodel/lib/active_model/type/helpers/time_value.rb b/activemodel/lib/active_model/type/helpers/time_value.rb index 0a0e58654b..63993c0d93 100644 --- a/activemodel/lib/active_model/type/helpers/time_value.rb +++ b/activemodel/lib/active_model/type/helpers/time_value.rb @@ -5,11 +5,7 @@ module ActiveModel 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 + value = apply_seconds_precision(value) if value.acts_like?(:time) zone_conversion_method = is_utc? ? :getutc : :getlocal @@ -34,6 +30,13 @@ module ActiveModel end end + def apply_seconds_precision(value) + return value unless precision && value.respond_to?(:usec) + number_of_insignificant_digits = 6 - precision + round_power = 10 ** number_of_insignificant_digits + value.change(usec: value.usec / round_power * round_power) + end + def type_cast_for_schema(value) "'#{value.to_s(:db)}'" end |