Permalink
Browse files

lazily instantiate application subclasses

this means we can meaningfully override methods in the subclass
  • Loading branch information...
1 parent 2090615 commit d25fe31c40928712b5e08fe0afb567c3bc88eddf @tenderlove tenderlove committed Aug 7, 2014
Showing with 18 additions and 4 deletions.
  1. +7 −1 railties/lib/rails.rb
  2. +1 −3 railties/lib/rails/application.rb
  3. +10 −0 railties/test/engine_test.rb
View
8 railties/lib/rails.rb
@@ -29,7 +29,13 @@ module Rails
autoload :WelcomeController
class << self
- attr_accessor :application, :cache, :logger
+ @application = @app_class = nil
+
+ attr_writer :application
+ attr_accessor :app_class, :cache, :logger
+ def application
+ @application ||= (app_class.instance if app_class)
+ end
delegate :initialize!, :initialized?, to: :application
View
4 railties/lib/rails/application.rb
@@ -87,7 +87,7 @@ class Application < Engine
class << self
def inherited(base)
super
- base.instance
+ Rails.app_class = base
end
# Makes the +new+ method public.
@@ -117,8 +117,6 @@ 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)
View
10 railties/test/engine_test.rb
@@ -11,4 +11,14 @@ def initialize(*args)
assert !engine.routes?
end
+
+ def test_application_can_be_subclassed
+ klass = Class.new(Rails::Application) do
+ attr_reader :hello
+ def initialize
+ @hello = "world"
+ end
+ end
+ assert_equal "world", klass.instance.hello
+ end
end

1 comment on commit d25fe31

Please sign in to comment.