lazily instantiate application subclasses

this means we can meaningfully override methods in the subclass
This commit is contained in:
Aaron Patterson 2014-08-06 18:27:16 -07:00
parent 2090615d39
commit d25fe31c40
3 changed files with 18 additions and 4 deletions

View File

@ -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 File

@ -87,7 +87,7 @@ module Rails
class << self
def inherited(base)
super
base.instance
Rails.app_class = base
end
# Makes the +new+ method public.
@ -117,8 +117,6 @@ module Rails
@railties = nil
@message_verifiers = {}
Rails.application ||= self
add_lib_to_load_path!
ActiveSupport.run_load_hooks(:before_configuration, self)

View File

@ -11,4 +11,14 @@ class EngineTest < ActiveSupport::TestCase
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