From 32a5b49911b88e8e410583d382e8253004abce50 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 19 Jul 2010 17:53:14 +0200 Subject: [PATCH] Move singleton pattern to Railtie and remove Engine::Configurable and Application::Configurable in favor of unified Railtie::Configurable --- railties/lib/rails/application.rb | 29 +++------------- .../lib/rails/application/configurable.rb | 19 ----------- railties/lib/rails/application/railties.rb | 2 +- railties/lib/rails/engine.rb | 33 ++++++++++++------- railties/lib/rails/engine/configurable.rb | 31 ----------------- railties/lib/rails/railtie.rb | 8 ++++- railties/lib/rails/railtie/configurable.rb | 26 +++++++++++---- .../test/application/configuration_test.rb | 13 ++++---- railties/test/railties/engine_test.rb | 8 ++--- 9 files changed, 62 insertions(+), 107 deletions(-) delete mode 100644 railties/lib/rails/application/configurable.rb delete mode 100644 railties/lib/rails/engine/configurable.rb diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index cbb1aad68f..fee99faf43 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -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 diff --git a/railties/lib/rails/application/configurable.rb b/railties/lib/rails/application/configurable.rb deleted file mode 100644 index f598e33965..0000000000 --- a/railties/lib/rails/application/configurable.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/railties/lib/rails/application/railties.rb b/railties/lib/rails/application/railties.rb index 67352f537e..c1d2de571f 100644 --- a/railties/lib/rails/application/railties.rb +++ b/railties/lib/rails/application/railties.rb @@ -10,7 +10,7 @@ module Rails end def railties - @railties ||= ::Rails::Railtie.subclasses.map(&:new) + @railties ||= ::Rails::Railtie.subclasses.map(&:instance) end def engines diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 22e3e931b1..41b0a764e2 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -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 diff --git a/railties/lib/rails/engine/configurable.rb b/railties/lib/rails/engine/configurable.rb deleted file mode 100644 index e668911913..0000000000 --- a/railties/lib/rails/engine/configurable.rb +++ /dev/null @@ -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 diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 7126ec1699..0514e425fd 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -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 diff --git a/railties/lib/rails/railtie/configurable.rb b/railties/lib/rails/railtie/configurable.rb index a2eb938c5a..b6d4ed2312 100644 --- a/railties/lib/rails/railtie/configurable.rb +++ b/railties/lib/rails/railtie/configurable.rb @@ -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 diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 2da695b78e..63d53fff90 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -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 diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 09f2b5baa0..e8675daef0 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -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