Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move singleton pattern to Railtie and remove Engine::Configurable and…

… Application::Configurable in favor of unified Railtie::Configurable
  • Loading branch information...
commit 32a5b49911b88e8e410583d382e8253004abce50 1 parent 939d425
@drogus drogus authored
View
29 railties/lib/rails/application.rb
@@ -41,20 +41,6 @@ class Application < Engine
autoload :Railties, 'rails/application/railties'
class << self
- private :new
-
- def instance
- if self == Rails::Application
- if Rails.application
- ActiveSupport::Deprecation.warn "Calling a method in Rails::Application is deprecated, " <<
- "please call it directly in your application constant #{Rails.application.class.name}.", caller
- end
- Rails.application
- else
- @@instance ||= new
- end
- end
-
def inherited(base)
raise "You cannot have more than one Rails::Application" if Rails.application
super
@@ -62,15 +48,8 @@ def inherited(base)
Rails.application.add_lib_to_load_path!
ActiveSupport.run_load_hooks(:before_configuration, base.instance)
end
-
- def respond_to?(*args)
- super || instance.respond_to?(*args)
- end
-
end
- delegate :middleware, :to => :config
-
# This method is called just after an application inherits from Rails::Application,
# allowing the developer to load classes in lib and use them during application
# configuration.
@@ -99,10 +78,6 @@ def eager_load! #:nodoc:
super
end
- def railties
- @railties ||= Railties.new(config)
- end
-
def routes_reloader
@routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! }
end
@@ -163,6 +138,10 @@ def initializers
initializers
end
+ def config
+ @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
+ end
+
protected
def default_middleware_stack
View
19 railties/lib/rails/application/configurable.rb
@@ -1,19 +0,0 @@
-module Rails
- class Application
- module Configurable
- def self.included(base)
- base.extend ClassMethods
- end
-
- module ClassMethods
- def inherited(base)
- raise "You cannot inherit from a Rails::Application child"
- end
- end
-
- def config
- @config ||= Application::Configuration.new(self.class.find_root_with_flag("config.ru", Dir.pwd))
- end
- end
- end
-end
View
2  railties/lib/rails/application/railties.rb
@@ -10,7 +10,7 @@ def all(&block)
end
def railties
- @railties ||= ::Rails::Railtie.subclasses.map(&:new)
+ @railties ||= ::Rails::Railtie.subclasses.map(&:instance)
end
def engines
View
33 railties/lib/rails/engine.rb
@@ -125,19 +125,9 @@ def endpoint(endpoint = nil)
@endpoint = endpoint if endpoint
@endpoint
end
-
- def configure(&block)
- class_eval(&block)
- end
-
- protected
-
- def method_missing(*args, &block)
- instance.send(*args, &block)
- end
end
- delegate :paths, :root, :to => :config
+ delegate :middleware, :root, :paths, :to => :config
def load_tasks
super
@@ -154,7 +144,7 @@ def eager_load!
end
def railties
- @railties ||= Railties.new(config)
+ @railties ||= self.class::Railties.new(config)
end
def app
@@ -183,6 +173,10 @@ def initializers
initializers
end
+ def config
+ @config ||= Engine::Configuration.new(find_root_with_flag("lib"))
+ end
+
# Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path, :before => :bootstrap_hook do
_all_load_paths.reverse_each do |path|
@@ -256,6 +250,21 @@ def initializers
end
protected
+ def find_root_with_flag(flag, default=nil)
+ root_path = self.class.called_from
+
+ while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
+ parent = File.dirname(root_path)
+ root_path = parent != root_path && parent
+ end
+
+ root = File.exist?("#{root_path}/#{flag}") ? root_path : default
+ raise "Could not find root path for #{self}" unless root
+
+ Config::CONFIG['host_os'] =~ /mswin|mingw/ ?
+ Pathname.new(root).expand_path : Pathname.new(root).realpath
+ end
+
def default_middleware_stack
ActionDispatch::MiddlewareStack.new
end
View
31 railties/lib/rails/engine/configurable.rb
@@ -1,31 +0,0 @@
-module Rails
- class Engine
- module Configurable
- def self.included(base)
- base.extend ClassMethods
- base.private_class_method :new
- end
-
- module ClassMethods
- delegate :middleware, :root, :paths, :to => :config
- delegate :call, :to => :instance
-
- def config
- @config ||= Engine::Configuration.new(find_root_with_flag("lib"))
- end
-
- def inherited(base)
- raise "You cannot inherit from a Rails::Engine child"
- end
-
- def instance
- @instance ||= new
- end
- end
-
- def config
- self.class.config
- end
- end
- end
-end
View
8 railties/lib/rails/railtie.rb
@@ -130,13 +130,15 @@ class Railtie
ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Plugin Rails::Engine Rails::Application)
class << self
+ private :new
+
def subclasses
@subclasses ||= []
end
def inherited(base)
unless base.abstract_railtie?
- base.send(:include, self::Configurable)
+ base.send(:include, Railtie::Configurable)
subclasses << base
end
end
@@ -164,6 +166,10 @@ def abstract_railtie?
end
end
+ def config
+ @config ||= Railtie::Configuration.new
+ end
+
def eager_load!
end
View
26 railties/lib/rails/railtie/configurable.rb
@@ -6,17 +6,29 @@ def self.included(base)
end
module ClassMethods
- def config
- @config ||= Railtie::Configuration.new
- end
+ delegate :config, :to => :instance
def inherited(base)
- raise "You cannot inherit from a Rails::Railtie child"
+ raise "You cannot inherit from a #{self.superclass.name} child"
end
- end
- def config
- self.class.config
+ def instance
+ @instance ||= new
+ end
+
+ def respond_to?(*args)
+ super || instance.respond_to?(*args)
+ end
+
+ def configure(&block)
+ class_eval(&block)
+ end
+
+ protected
+
+ def method_missing(*args, &block)
+ instance.send(*args, &block)
+ end
end
end
end
View
13 railties/test/application/configuration_test.rb
@@ -26,18 +26,17 @@ def teardown
FileUtils.rm_rf(new_app) if File.directory?(new_app)
end
- test "Rails::Application.instance is nil until app is initialized" do
+ test "Rails.application is nil until app is initialized" do
require 'rails'
- assert_nil Rails::Application.instance
+ assert_nil Rails.application
require "#{app_path}/config/environment"
- assert_equal AppTemplate::Application.instance, Rails::Application.instance
+ assert_equal AppTemplate::Application.instance, Rails.application
end
- test "Rails::Application responds to all instance methods" do
+ test "Rails.application responds to all instance methods" do
require "#{app_path}/config/environment"
- assert_respond_to Rails::Application, :routes_reloader
- assert_equal Rails::Application.routes_reloader, Rails.application.routes_reloader
- assert_equal Rails::Application.routes_reloader, AppTemplate::Application.routes_reloader
+ assert_respond_to Rails.application, :routes_reloader
+ assert_equal Rails.application.routes_reloader, AppTemplate::Application.routes_reloader
end
test "Rails::Application responds to paths" do
View
8 railties/test/railties/engine_test.rb
@@ -85,12 +85,12 @@ class Engine < ::Rails::Engine
boot_rails
- Rails::Application.routes.draw do |map|
+ Rails.application.routes.draw do |map|
mount(Bukkits::Engine => "/bukkits")
end
env = Rack::MockRequest.env_for("/bukkits")
- response = Rails::Application.call(env)
+ response = Rails.application.call(env)
assert_equal "HELLO WORLD", response[2]
end
@@ -109,12 +109,12 @@ class Engine < ::Rails::Engine
match "/foo" => lambda { |env| [200, {'Content-Type' => 'text/html'}, 'foo'] }
end
- Rails::Application.routes.draw do |map|
+ Rails.application.routes.draw do |map|
mount(Bukkits::Engine => "/bukkits")
end
env = Rack::MockRequest.env_for("/bukkits/foo")
- response = Rails::Application.call(env)
+ response = Rails.application.call(env)
assert_equal "foo", response[2]
end
Please sign in to comment.
Something went wrong with that request. Please try again.