From 907280ddfd78c53e2eb5af6f12512dc38df38bd8 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 18 Feb 2019 03:16:23 +0900 Subject: Fix type cast with values hash for Date type `value_from_multiparameter_assignment` defined by `AcceptsMultiparameterTime` helper requires `default_timezone` method which is defined at `TimeValue` helper. Since `Date` type doesn't include `TimeValue`, I've extracted `Timezone` helper to be shared by `Date`, `DateTime`, and `Time` types. --- activemodel/lib/active_model/type/date.rb | 1 + activemodel/lib/active_model/type/date_time.rb | 1 + activemodel/lib/active_model/type/helpers.rb | 1 + .../lib/active_model/type/helpers/time_value.rb | 12 ------------ activemodel/lib/active_model/type/helpers/timezone.rb | 19 +++++++++++++++++++ activemodel/lib/active_model/type/time.rb | 1 + 6 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 activemodel/lib/active_model/type/helpers/timezone.rb (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/type/date.rb b/activemodel/lib/active_model/type/date.rb index 8ec5deedc4..da12041dee 100644 --- a/activemodel/lib/active_model/type/date.rb +++ b/activemodel/lib/active_model/type/date.rb @@ -3,6 +3,7 @@ module ActiveModel module Type class Date < Value # :nodoc: + include Helpers::Timezone include Helpers::AcceptsMultiparameterTime.new def type diff --git a/activemodel/lib/active_model/type/date_time.rb b/activemodel/lib/active_model/type/date_time.rb index d48598376e..62f28fc4a7 100644 --- a/activemodel/lib/active_model/type/date_time.rb +++ b/activemodel/lib/active_model/type/date_time.rb @@ -3,6 +3,7 @@ module ActiveModel module Type class DateTime < Value # :nodoc: + include Helpers::Timezone include Helpers::TimeValue include Helpers::AcceptsMultiparameterTime.new( defaults: { 4 => 0, 5 => 0 } diff --git a/activemodel/lib/active_model/type/helpers.rb b/activemodel/lib/active_model/type/helpers.rb index 403f0a9e6b..20145d5f0d 100644 --- a/activemodel/lib/active_model/type/helpers.rb +++ b/activemodel/lib/active_model/type/helpers.rb @@ -4,3 +4,4 @@ require "active_model/type/helpers/accepts_multiparameter_time" require "active_model/type/helpers/numeric" require "active_model/type/helpers/mutable" require "active_model/type/helpers/time_value" +require "active_model/type/helpers/timezone" diff --git a/activemodel/lib/active_model/type/helpers/time_value.rb b/activemodel/lib/active_model/type/helpers/time_value.rb index da56073436..735b9a75a6 100644 --- a/activemodel/lib/active_model/type/helpers/time_value.rb +++ b/activemodel/lib/active_model/type/helpers/time_value.rb @@ -21,18 +21,6 @@ module ActiveModel value end - def is_utc? - ::Time.zone_default.nil? || ::Time.zone_default =~ "UTC" - end - - def default_timezone - if is_utc? - :utc - else - :local - end - end - def apply_seconds_precision(value) return value unless precision && value.respond_to?(:usec) number_of_insignificant_digits = 6 - precision diff --git a/activemodel/lib/active_model/type/helpers/timezone.rb b/activemodel/lib/active_model/type/helpers/timezone.rb new file mode 100644 index 0000000000..cf87b9715b --- /dev/null +++ b/activemodel/lib/active_model/type/helpers/timezone.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "active_support/core_ext/time/zones" + +module ActiveModel + module Type + module Helpers # :nodoc: all + module Timezone + def is_utc? + ::Time.zone_default.nil? || ::Time.zone_default =~ "UTC" + end + + def default_timezone + is_utc? ? :utc : :local + end + end + end + end +end diff --git a/activemodel/lib/active_model/type/time.rb b/activemodel/lib/active_model/type/time.rb index 8fba01b86a..61847a4ce7 100644 --- a/activemodel/lib/active_model/type/time.rb +++ b/activemodel/lib/active_model/type/time.rb @@ -3,6 +3,7 @@ module ActiveModel module Type class Time < Value # :nodoc: + include Helpers::Timezone include Helpers::TimeValue include Helpers::AcceptsMultiparameterTime.new( defaults: { 1 => 2000, 2 => 1, 3 => 1, 4 => 0, 5 => 0 } -- cgit v1.2.3