From e1404ec824d263d25717468c6f4b73ee7b2bdf4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 7 Apr 2014 17:33:40 -0300 Subject: [PATCH] Rails.application should be set inside before_configuration hook Fixes #14620 --- railties/lib/rails/application.rb | 4 +++- railties/test/application/multiple_applications_test.rb | 6 ++++++ railties/test/railties/railtie_test.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 4be90a3f6c..8d080feb04 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -87,7 +87,7 @@ class Application < Engine class << self def inherited(base) super - Rails.application ||= base.instance + base.instance end # Makes the +new+ method public. @@ -117,6 +117,8 @@ def initialize(initial_variable_values = {}, &block) @railties = nil @message_verifiers = {} + Rails.application ||= self + add_lib_to_load_path! ActiveSupport.run_load_hooks(:before_configuration, self) diff --git a/railties/test/application/multiple_applications_test.rb b/railties/test/application/multiple_applications_test.rb index 5bfea599e0..42b319178d 100644 --- a/railties/test/application/multiple_applications_test.rb +++ b/railties/test/application/multiple_applications_test.rb @@ -21,6 +21,12 @@ def test_cloning_an_application_makes_a_shallow_copy_of_config assert_equal Rails.application.config.secret_key_base, clone.config.secret_key_base, "The base secret key on the config should be the same" end + def test_inheriting_multiple_times_from_application + new_application_class = Class.new(Rails::Application) + + assert_not_equal Rails.application.object_id, new_application_class.instance.object_id + end + def test_initialization_of_multiple_copies_of_same_application application1 = AppTemplate::Application.new application2 = AppTemplate::Application.new diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index 4cbd4822be..a458240d2f 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -72,6 +72,14 @@ class Foo < Rails::Railtie ; config.to_prepare { $to_prepare = true } ; end assert $to_prepare end + test "railtie have access to application in before_configuration callbacks" do + $after_initialize = false + class Foo < Rails::Railtie ; config.before_configuration { $before_configuration = Rails.root.to_path } ; end + assert_not $before_configuration + require "#{app_path}/config/environment" + assert_equal app_path, $before_configuration + end + test "railtie can add after_initialize callbacks" do $after_initialize = false class Foo < Rails::Railtie ; config.after_initialize { $after_initialize = true } ; end