From 50b6110106a6e467f5e5f6303700522cebc60a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 29 Jun 2012 16:50:51 +0200 Subject: Remove unused responsibilities and add a few load definitions to engines Since plugins were removed, we can clean up a few methods in engines. We also use this opportunity to move `load_console`, `load_tasks` and `load_runner` to Rails::Engine. This means that, if someone wants to improve script/rails for engines to support console or runner commands, part of the work is already done. --- railties/lib/rails/application.rb | 130 ++++++++++++++++++-------------------- railties/lib/rails/engine.rb | 123 +++++++++++++++++------------------- railties/lib/rails/railtie.rb | 32 +++++----- 3 files changed, 139 insertions(+), 146 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 1a993a25cb..d5ec2cbfd9 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -79,9 +79,11 @@ module Rails @routes_reloader = nil @env_config = nil @ordered_railties = nil + @railties = nil @queue = nil end + # Returns true if the application is initialized. def initialized? @initialized end @@ -98,30 +100,6 @@ module Rails routes_reloader.reload! end - # Load the application and its railties tasks and invoke the registered hooks. - # Check Rails::Railtie.rake_tasks for more info. - def load_tasks(app=self) - initialize_tasks - super - self - end - - # Load the application console and invoke the registered hooks. - # Check Rails::Railtie.console for more info. - def load_console(app=self) - initialize_console - super - self - end - - # Load the application runner and invoke the registered hooks. - # Check Rails::Railtie.runner for more info. - def load_runner(app=self) - initialize_runner - super - self - end - # Stores some of the Rails initial environment parameters which # will be used by middlewares and engines to configure themselves. def env_config @@ -188,32 +166,9 @@ module Rails self end - # Returns the ordered railties for this application considering railties_order. - def ordered_railties #:nodoc: - @ordered_railties ||= begin - order = config.railties_order.map do |railtie| - if railtie == :main_app - self - elsif railtie.respond_to?(:instance) - railtie.instance - else - railtie - end - end - - all = (railties - order) - all.push(self) unless (all + order).include?(self) - order.push(:all) unless order.include?(:all) - - index = order.index(:all) - order[index] = all - order.reverse.flatten - end - end - def initializers #:nodoc: Bootstrap.initializers_for(self) + - super + + railties_initializers(super) + Finisher.initializers_for(self) end @@ -246,6 +201,66 @@ module Rails alias :build_middleware_stack :app + def run_tasks_blocks(app) #:nodoc: + railties.each { |r| r.run_tasks_blocks(app) } + super + require "rails/tasks" + task :environment do + $rails_rake_task = true + require_environment! + end + end + + def run_generators_blocks(app) #:nodoc: + railties.each { |r| r.run_generators_blocks(app) } + super + end + + def run_runner_blocks(app) #:nodoc: + railties.each { |r| r.run_runner_blocks(app) } + super + end + + def run_console_blocks(app) #:nodoc: + railties.each { |r| r.run_console_blocks(app) } + super + end + + # Returns the ordered railties for this application considering railties_order. + def ordered_railties #:nodoc: + @ordered_railties ||= begin + order = config.railties_order.map do |railtie| + if railtie == :main_app + self + elsif railtie.respond_to?(:instance) + railtie.instance + else + railtie + end + end + + all = (railties - order) + all.push(self) unless (all + order).include?(self) + order.push(:all) unless order.include?(:all) + + index = order.index(:all) + order[index] = all + order.reverse.flatten + end + end + + def railties_initializers(current) #:nodoc: + initializers = [] + ordered_railties.each do |r| + if r == self + initializers += current + else + initializers += r.initializers + end + end + initializers + end + def reload_dependencies? #:nodoc: config.reload_classes_only_on_change != true || reloaders.map(&:updated?).any? end @@ -305,25 +320,6 @@ module Rails end end - def initialize_tasks #:nodoc: - self.class.rake_tasks do - require "rails/tasks" - task :environment do - $rails_rake_task = true - require_environment! - end - end - end - - def initialize_console #:nodoc: - require "pp" - require "rails/console/app" - require "rails/console/helpers" - end - - def initialize_runner #:nodoc: - end - def build_original_fullpath(env) #:nodoc: path_info = env["PATH_INFO"] query_string = env["QUERY_STRING"] diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 7db602279a..309d265477 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -338,26 +338,6 @@ module Rails class Engine < Railtie autoload :Configuration, "rails/engine/configuration" - def initialize - @_all_autoload_paths = nil - @_all_load_paths = nil - @app = nil - @config = nil - @env_config = nil - @helpers = nil - @railties = nil - @routes = nil - super - end - - def load_generators(app=self) - initialize_generators - railties.each { |r| r.load_generators(app) } - Rails::Generators.configure!(app.config.generators) - super - self - end - class << self attr_accessor :called_from, :isolated alias :isolated? :isolated @@ -406,7 +386,7 @@ module Rails end unless mod.respond_to?(:railtie_routes_url_helpers) - define_method(:railtie_routes_url_helpers) { railtie.routes_url_helpers } + define_method(:railtie_routes_url_helpers) { railtie.routes.url_helpers } end end end @@ -426,25 +406,54 @@ module Rails delegate :middleware, :root, :paths, :to => :config delegate :engine_name, :isolated?, :to => "self.class" - def load_tasks(app=self) - railties.each { |r| r.load_tasks(app) } + def initialize + @_all_autoload_paths = nil + @_all_load_paths = nil + @app = nil + @config = nil + @env_config = nil + @helpers = nil + @routes = nil super - paths["lib/tasks"].existent.sort.each { |ext| load(ext) } end + # Load console and invoke the registered hooks. + # Check Rails::Railtie.console for more info. def load_console(app=self) - railties.each { |r| r.load_console(app) } - super + require "pp" + require "rails/console/app" + require "rails/console/helpers" + run_console_blocks(app) + self end + # Load Rails runner and invoke the registered hooks. + # Check Rails::Railtie.runner for more info. def load_runner(app=self) - railties.each { |r| r.load_runner(app) } - super + run_runner_blocks(app) + self end - def eager_load! - railties.each(&:eager_load!) + # Load Rake, railties tasks and invoke the registered hooks. + # Check Rails::Railtie.rake_tasks for more info. + def load_tasks(app=self) + require "rake" + run_tasks_blocks(app) + self + end + # Load rails generators and invoke the registered hooks. + # Check Rails::Railtie.generators for more info. + def load_generators(app=self) + require "rails/generators" + run_generators_blocks(app) + Rails::Generators.configure!(app.config.generators) + self + end + + # Eager load the application by loading all ruby + # files inside eager_load paths. + def eager_load! config.eager_load_paths.each do |load_path| matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| @@ -453,10 +462,7 @@ module Rails end end - def railties - @railties ||= [] - end - + # Returns a module with all the helpers defined for the engine. def helpers @helpers ||= begin helpers = Module.new @@ -468,14 +474,12 @@ module Rails end end + # Returns all registered helpers paths. def helpers_paths paths["app/helpers"].existent end - def routes_url_helpers - routes.url_helpers - end - + # Returns the underlying rack application for this engine. def app @app ||= begin config.middleware = config.middleware.merge_into(default_middleware_stack) @@ -483,20 +487,26 @@ module Rails end end + # Returns the endpoint for this engine. If none is registered, + # defaults to an ActionDispatch::Routing::RouteSet. def endpoint self.class.endpoint || routes end + # Define the Rack API for this engine. def call(env) app.call(env.merge!(env_config)) end + # Defines additional Rack env configuration that is added on each call. def env_config @env_config ||= { 'action_dispatch.routes' => routes } end + # Defines the routes for this engine. If a block is given to + # routes, it is appended to the engine. def routes @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes| routes.draw_paths.concat paths["config/routes"].paths @@ -506,22 +516,7 @@ module Rails @routes end - def ordered_railties - railties + [self] - end - - def initializers - initializers = [] - ordered_railties.each do |r| - if r == self - initializers += super - else - initializers += r.initializers - end - end - initializers - end - + # Define the configuration object for the engine. def config @config ||= Engine::Configuration.new(find_root_with_flag("lib")) end @@ -626,7 +621,6 @@ module Rails else Rake::Task["app:railties:install:migrations"].invoke end - end end end @@ -634,19 +628,20 @@ module Rails protected - def initialize_generators - require "rails/generators" + def run_tasks_blocks(*) #:nodoc: + super + paths["lib/tasks"].existent.sort.each { |ext| load(ext) } end - def routes? + def routes? #:nodoc: @routes end - def has_migrations? + def has_migrations? #:nodoc: paths["db/migrate"].existent.any? end - def find_root_with_flag(flag, default=nil) + def find_root_with_flag(flag, default=nil) #:nodoc: root_path = self.class.called_from while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}") @@ -660,19 +655,19 @@ module Rails Pathname.new File.realpath root end - def default_middleware_stack + def default_middleware_stack #:nodoc: ActionDispatch::MiddlewareStack.new end - def _all_autoload_once_paths + def _all_autoload_once_paths #:nodoc: config.autoload_once_paths end - def _all_autoload_paths + def _all_autoload_paths #:nodoc: @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq end - def _all_load_paths + def _all_load_paths #:nodoc: @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq end end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index a06be59759..1cb99463cc 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -181,33 +181,35 @@ module Rails def eager_load! end - def load_console(app=self) + def railtie_namespace + @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) } + end + + protected + + def run_console_blocks(app) #:nodoc: self.class.console.each { |block| block.call(app) } end - def load_runner(app=self) + def run_generators_blocks(app) #:nodoc: + self.class.generators.each { |block| block.call(app) } + end + + def run_runner_blocks(app) #:nodoc: self.class.runner.each { |block| block.call(app) } end - def load_tasks(app=self) - require 'rake' + def run_tasks_blocks(app) #:nodoc: extend Rake::DSL - self.class.rake_tasks.each { |block| self.instance_exec(app, &block) } + self.class.rake_tasks.each { |block| instance_exec(app, &block) } - # load also tasks from all superclasses + # Load also tasks from all superclasses klass = self.class.superclass + while klass.respond_to?(:rake_tasks) - klass.rake_tasks.each { |t| self.instance_exec(app, &t) } + klass.rake_tasks.each { |t| instance_exec(app, &t) } klass = klass.superclass end end - - def load_generators(app=self) - self.class.generators.each { |block| block.call(app) } - end - - def railtie_namespace - @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) } - end end end -- cgit v1.2.3