Permalink
Browse files

Set the default timezone after the initialization since the configura…

…tion

now lives in the application initializers.

Fix #8711
  • Loading branch information...
rafaelfranca committed Jan 4, 2013
1 parent dd3360e commit 39374aa925a7d670b039c0c0c9aa9f4aef19466b
Showing with 27 additions and 15 deletions.
  1. +16 −14 activesupport/lib/active_support/railtie.rb
  2. +11 −1 railties/test/application/configuration_test.rb
@@ -13,20 +13,6 @@ class Railtie < Rails::Railtie # :nodoc:
end
end
- # Sets the default value for Time.zone
- # If assigned value cannot be matched to a TimeZone, an exception will be raised.
- initializer "active_support.initialize_time_zone" do |app|
- require 'active_support/core_ext/time/zones'
- zone_default = Time.find_zone!(app.config.time_zone)
-
- unless zone_default
- raise 'Value assigned to config.time_zone not recognized. ' \
- 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
- end
-
- Time.zone_default = zone_default
- end
-
# Sets the default week start
# If assigned value is not a valid day symbol (e.g. :sunday, :monday, ...), an exception will be raised.
initializer "active_support.initialize_beginning_of_week" do |app|
@@ -42,5 +28,21 @@ class Railtie < Rails::Railtie # :nodoc:
ActiveSupport.send(k, v) if ActiveSupport.respond_to? k
end
end
+
+ # Sets the default value for Time.zone after initialization since the default configuration
+ # lives in application initializers.
+ # If assigned value cannot be matched to a TimeZone, an exception will be raised.
+ config.after_initialize do |app|
+ require 'active_support/core_ext/time/zones'
+ zone_default = Time.find_zone!(app.config.time_zone)
+
+ unless zone_default
+ raise 'Value assigned to config.time_zone not recognized. ' \
+ 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
+ end
+
+ Time.zone_default = zone_default
+ end
+
end
end
@@ -407,7 +407,17 @@ def index
require "#{app_path}/config/environment"
- assert_equal "Wellington", Rails.application.config.time_zone
+ assert_equal Time.find_zone!("Wellington"), Time.zone_default
+ end
+
+ test "timezone can be set on initializers" do
+ app_file "config/initializers/locale.rb", <<-RUBY
+ Rails.application.config.time_zone = "Central Time (US & Canada)"
+ RUBY
+
+ require "#{app_path}/config/environment"
+
+ assert_equal Time.find_zone!("Central Time (US & Canada)"), Time.zone_default
end
test "raises when an invalid timezone is defined in the config" do

4 comments on commit 39374aa

@carlosantoniodasilva

This comment has been minimized.

Show comment Hide comment
@carlosantoniodasilva

carlosantoniodasilva Jan 4, 2013

Member

Could this break the assumption that in an initializer the Time zone would already be set? Just wondering if someone somewhere isn't doing anything crazy with Time zones in an initializer.

Could this break the assumption that in an initializer the Time zone would already be set? Just wondering if someone somewhere isn't doing anything crazy with Time zones in an initializer.

@rafaelfranca

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Jan 4, 2013

Member

Yes. I thought the same, but I didn't saw any case where you would expect time zone to be set in an initializer. Do you have any idea?

Member

rafaelfranca replied Jan 4, 2013

Yes. I thought the same, but I didn't saw any case where you would expect time zone to be set in an initializer. Do you have any idea?

@carlosantoniodasilva

This comment has been minimized.

Show comment Hide comment
@carlosantoniodasilva

carlosantoniodasilva Jan 4, 2013

Member

None actually, but users can do crazy things 😄. We'll have to wait to see I guess.

None actually, but users can do crazy things 😄. We'll have to wait to see I guess.

@rafaelfranca

This comment has been minimized.

Show comment Hide comment
@rafaelfranca

rafaelfranca Jan 4, 2013

Member

😢

Member

rafaelfranca replied Jan 4, 2013

😢

Please sign in to comment.