diff options
Diffstat (limited to 'railties/lib/rails/engine.rb')
-rw-r--r-- | railties/lib/rails/engine.rb | 123 |
1 files changed, 59 insertions, 64 deletions
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 <tt>Rails::Railtie.console</tt> 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 <tt>Rails::Railtie.runner</tt> 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 <tt>Rails::Railtie.rake_tasks</tt> 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 <tt>Rails::Railtie.generators</tt> 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 |