Add validation for ActiveRecord.default_timezone
This commit is contained in:
parent
3272335f8f
commit
6100526e94
|
@ -185,12 +185,19 @@ module ActiveRecord
|
||||||
singleton_class.attr_accessor :legacy_connection_handling
|
singleton_class.attr_accessor :legacy_connection_handling
|
||||||
self.legacy_connection_handling = true
|
self.legacy_connection_handling = true
|
||||||
|
|
||||||
##
|
|
||||||
# :singleton-method:
|
|
||||||
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
|
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
|
||||||
# dates and times from the database. This is set to :utc by default.
|
# dates and times from the database. This is set to :utc by default.
|
||||||
singleton_class.attr_accessor :default_timezone
|
def self.default_timezone=(default_timezone)
|
||||||
self.default_timezone = :utc
|
unless %i[local utc].include?(default_timezone)
|
||||||
|
raise ArgumentError, 'default_timezone must be either :utc (default) or :local'
|
||||||
|
end
|
||||||
|
|
||||||
|
@default_timezone = default_timezone
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.default_timezone
|
||||||
|
@default_timezone ||= :utc
|
||||||
|
end
|
||||||
|
|
||||||
singleton_class.attr_accessor :writing_role
|
singleton_class.attr_accessor :writing_role
|
||||||
self.writing_role = :writing
|
self.writing_role = :writing
|
||||||
|
|
|
@ -7,6 +7,16 @@ require "models/task"
|
||||||
class DateTimeTest < ActiveRecord::TestCase
|
class DateTimeTest < ActiveRecord::TestCase
|
||||||
include InTimeZone
|
include InTimeZone
|
||||||
|
|
||||||
|
def test_default_timezone_validation
|
||||||
|
assert_raises ArgumentError do
|
||||||
|
ActiveRecord.default_timezone = 'UTC'
|
||||||
|
end
|
||||||
|
|
||||||
|
# These values should not raise errors
|
||||||
|
ActiveRecord.default_timezone = :local
|
||||||
|
ActiveRecord.default_timezone = :utc
|
||||||
|
end
|
||||||
|
|
||||||
def test_saves_both_date_and_time
|
def test_saves_both_date_and_time
|
||||||
with_env_tz "America/New_York" do
|
with_env_tz "America/New_York" do
|
||||||
with_timezone_config default: :utc do
|
with_timezone_config default: :utc do
|
||||||
|
|
Loading…
Reference in New Issue