Skip to content

Commit

Permalink
lazily instantiate application subclasses
Browse files Browse the repository at this point in the history
this means we can meaningfully override methods in the subclass
  • Loading branch information
tenderlove committed Aug 7, 2014
1 parent 2090615 commit d25fe31
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 7 additions & 1 deletion railties/lib/rails.rb
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions railties/lib/rails/application.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions railties/test/engine_test.rb
Expand Up @@ -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

0 comments on commit d25fe31

Please sign in to comment.