1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move singleton pattern to Railtie and remove Engine::Configurable and Application::Configurable in favor of unified Railtie::Configurable

This commit is contained in:
Piotr Sarnacki 2010-07-19 17:53:14 +02:00
parent 939d4255e6
commit 32a5b49911
9 changed files with 62 additions and 107 deletions

View file

@ -41,20 +41,6 @@ module Rails
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 @@ module Rails
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 @@ module Rails
super
end
def railties
@railties ||= Railties.new(config)
end
def routes_reloader
@routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! }
end
@ -163,6 +138,10 @@ module Rails
initializers
end
def config
@config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd))
end
protected
def default_middleware_stack

View file

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

@ -10,7 +10,7 @@ module Rails
end
def railties
@railties ||= ::Rails::Railtie.subclasses.map(&:new)
@railties ||= ::Rails::Railtie.subclasses.map(&:instance)
end
def engines

View file

@ -125,19 +125,9 @@ module Rails
@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 @@ module Rails
end
def railties
@railties ||= Railties.new(config)
@railties ||= self.class::Railties.new(config)
end
def app
@ -183,6 +173,10 @@ module Rails
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 @@ module Rails
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 file

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

@ -130,13 +130,15 @@ module Rails
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 @@ module Rails
end
end
def config
@config ||= Railtie::Configuration.new
end
def eager_load!
end

View file

@ -6,17 +6,29 @@ module Rails
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 file

@ -26,18 +26,17 @@ module ApplicationTests
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 file

@ -85,12 +85,12 @@ module RailtiesTest
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 @@ module RailtiesTest
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