aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-02-18 03:16:23 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-02-18 04:20:52 +0900
commit907280ddfd78c53e2eb5af6f12512dc38df38bd8 (patch)
tree445db778f4e92d73acec0896e068f978c7ed6383 /activemodel
parent07346c4b35df191cfb05353a0a9f3e2f30faca65 (diff)
downloadrails-907280ddfd78c53e2eb5af6f12512dc38df38bd8.tar.gz
rails-907280ddfd78c53e2eb5af6f12512dc38df38bd8.tar.bz2
rails-907280ddfd78c53e2eb5af6f12512dc38df38bd8.zip
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.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/type/date.rb1
-rw-r--r--activemodel/lib/active_model/type/date_time.rb1
-rw-r--r--activemodel/lib/active_model/type/helpers.rb1
-rw-r--r--activemodel/lib/active_model/type/helpers/time_value.rb12
-rw-r--r--activemodel/lib/active_model/type/helpers/timezone.rb19
-rw-r--r--activemodel/lib/active_model/type/time.rb1
-rw-r--r--activemodel/test/cases/type/date_test.rb5
7 files changed, 27 insertions, 13 deletions
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 }
diff --git a/activemodel/test/cases/type/date_test.rb b/activemodel/test/cases/type/date_test.rb
index e8cf178612..9a393286b7 100644
--- a/activemodel/test/cases/type/date_test.rb
+++ b/activemodel/test/cases/type/date_test.rb
@@ -12,8 +12,11 @@ module ActiveModel
assert_nil type.cast(" ")
assert_nil type.cast("ABC")
- date_string = ::Time.now.utc.strftime("%F")
+ now = ::Time.now.utc
+ values_hash = { 1 => now.year, 2 => now.mon, 3 => now.mday }
+ date_string = now.strftime("%F")
assert_equal date_string, type.cast(date_string).strftime("%F")
+ assert_equal date_string, type.cast(values_hash).strftime("%F")
end
end
end