aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/engine.rb
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails/engine.rb')
-rw-r--r--railties/lib/rails/engine.rb133
1 files changed, 64 insertions, 69 deletions
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index ee265366ff..2015a944f0 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -92,17 +92,17 @@ module Rails
# The available paths in an engine are:
#
# class MyEngine < Rails::Engine
- # paths["app"] #=> ["app"]
- # paths["app/controllers"] #=> ["app/controllers"]
- # paths["app/helpers"] #=> ["app/helpers"]
- # paths["app/models"] #=> ["app/models"]
- # paths["app/views"] #=> ["app/views"]
- # paths["lib"] #=> ["lib"]
- # paths["lib/tasks"] #=> ["lib/tasks"]
- # paths["config"] #=> ["config"]
- # paths["config/initializers"] #=> ["config/initializers"]
- # paths["config/locales"] #=> ["config/locales"]
- # paths["config/routes"] #=> ["config/routes.rb"]
+ # paths["app"] # => ["app"]
+ # paths["app/controllers"] # => ["app/controllers"]
+ # paths["app/helpers"] # => ["app/helpers"]
+ # paths["app/models"] # => ["app/models"]
+ # paths["app/views"] # => ["app/views"]
+ # paths["lib"] # => ["lib"]
+ # paths["lib/tasks"] # => ["lib/tasks"]
+ # paths["config"] # => ["config"]
+ # paths["config/initializers"] # => ["config/initializers"]
+ # paths["config/locales"] # => ["config/locales"]
+ # paths["config/routes"] # => ["config/routes.rb"]
# end
#
# Your <tt>Application</tt> class adds a couple more paths to this set. And as in your
@@ -171,32 +171,6 @@ module Rails
#
# Now, +Engine+ will get only requests that were not handled by +Application+.
#
- # == Asset path
- #
- # When you use +Engine+ with its own public directory, you will probably want to copy or symlink it
- # to application's public directory. To simplify generating paths for assets, you can set <tt>asset_path</tt>
- # for an engine:
- #
- # module MyEngine
- # class Engine < Rails::Engine
- # config.asset_path = "/my_engine/%s"
- # end
- # end
- #
- # With such a config, asset paths will be automatically modified inside +Engine+:
- #
- # image_path("foo.jpg") #=> "/my_engine/images/foo.jpg"
- #
- # == Serving static files
- #
- # By default, Rails uses <tt>ActionDispatch::Static</tt> to serve static files in development mode. This is ok
- # while you develop your application, but when you want to deploy it, assets from an engine will not be
- # served by default. You should choose one of the two following strategies:
- #
- # * enable serving static files by setting config.serve_static_assets to true
- # * copy engine's public files to application's public folder with <tt>rake ENGINE_NAME:install:assets</tt>, for example
- # <tt>rake my_engine:install:assets</tt>
- #
# == Engine name
#
# There are some places where an Engine's name is used:
@@ -260,14 +234,14 @@ module Rails
# use the prefix "my_engine". In an isolated engine, the prefix will be omitted in url helpers and
# form fields for convenience.
#
- # polymorphic_url(MyEngine::Article.new) #=> "articles_path"
+ # polymorphic_url(MyEngine::Article.new) # => "articles_path"
#
# form_for(MyEngine::Article.new) do
- # text_field :title #=> <input type="text" name="article[title]" id="article_title" />
+ # text_field :title # => <input type="text" name="article[title]" id="article_title" />
# end
#
- # Additionally isolated engine will set its name according to namespace, so
- # MyEngine::Engine.engine_name #=> "my_engine". It will also set MyEngine.table_name_prefix
+ # Additionally an isolated engine will set its name according to namespace, so
+ # MyEngine::Engine.engine_name will be "my_engine". It will also set MyEngine.table_name_prefix
# to "my_engine_", changing MyEngine::Article model to use my_engine_article table.
#
# == Using Engine's routes outside Engine
@@ -276,7 +250,7 @@ module Rails
# <tt>url_helpers</tt> inside +Application+. When you mount an engine in an application's routes, a special helper is
# created to allow you to do that. Consider such a scenario:
#
- # # APP/config/routes.rb
+ # # config/routes.rb
# MyApplication::Application.routes.draw do
# mount MyEngine::Engine => "/my_engine", :as => "my_engine"
# match "/foo" => "foo#index"
@@ -312,6 +286,27 @@ module Rails
#
# This code will use <tt>my_engine.user_path(@user)</tt> to generate the proper route.
#
+ # == Isolated engine's helpers
+ #
+ # Sometimes you may want to isolate engine, but use helpers that are defined for it.
+ # If you want to share just a few specific helpers you can add them to application's
+ # helpers in ApplicationController:
+ #
+ # class ApplicationController < ActionController::Base
+ # helper MyEngine::SharedEngineHelper
+ # end
+ #
+ # If you want to include all of the engine's helpers, you can use #helpers method on egine's
+ # instance:
+ #
+ # class ApplicationController < ActionController::Base
+ # helper MyEngine::Engine.helpers
+ # end
+ #
+ # It will include all of the helpers from engine's directory. Take into account that this does
+ # not include helpers defined in controllers with helper_method or other similar solutions,
+ # only helpers defined in helpers directory will be included.
+ #
# == Migrations & seed data
#
# Engines can have their own migrations. The default path for migrations is exactly the same
@@ -410,6 +405,24 @@ module Rails
@railties ||= self.class::Railties.new(config)
end
+ def helpers
+ @helpers ||= begin
+ helpers = Module.new
+
+ helpers_paths = if config.respond_to?(:helpers_paths)
+ config.helpers_paths
+ else
+ paths["app/helpers"].existent
+ end
+
+ all = ActionController::Base.all_helpers_from_path(helpers_paths)
+ ActionController::Base.modules_for_helpers(all).each do |mod|
+ helpers.send(:include, mod)
+ end
+ helpers
+ end
+ end
+
def app
@app ||= begin
config.middleware = config.middleware.merge_into(default_middleware_stack)
@@ -427,8 +440,7 @@ module Rails
def env_config
@env_config ||= {
- 'action_dispatch.routes' => routes,
- 'action_dispatch.asset_path' => config.asset_path
+ 'action_dispatch.routes' => routes
}
end
@@ -509,13 +521,9 @@ module Rails
require environment if environment
end
- initializer :append_asset_paths do
- config.asset_path ||= default_asset_path
-
- public_path = paths["public"].first
- if config.compiled_asset_path && File.exist?(public_path)
- config.static_asset_paths[config.compiled_asset_path] = public_path
- end
+ initializer :append_assets_path do |app|
+ app.config.assets.paths.unshift *paths["vendor/assets"].existent
+ app.config.assets.paths.unshift *paths["app/assets"].existent
end
initializer :prepend_helpers_path do |app|
@@ -537,42 +545,29 @@ module Rails
rake_tasks do
next if self.is_a?(Rails::Application)
+ next unless has_migrations?
namespace railtie_name do
- desc "Shortcut for running both rake #{railtie_name}:install:migrations and #{railtie_name}:install:assets"
- task :install do
- Rake::Task["#{railtie_name}:install:migrations"].invoke
- Rake::Task["#{railtie_name}:install:assets"].invoke
- end
-
namespace :install do
- # TODO Add assets copying to this list
- # TODO Skip this if there is no paths["db/migrate"] for the engine
desc "Copy migrations from #{railtie_name} to application"
task :migrations do
ENV["FROM"] = railtie_name
Rake::Task["railties:install:migrations"].invoke
end
-
- desc "Copy assets from #{railtie_name} to application"
- task :assets do
- ENV["FROM"] = railtie_name
- Rake::Task["railties:install:assets"].invoke
- end
end
end
end
protected
- def default_asset_path
- "/#{railtie_name}%s"
- end
-
def routes?
defined?(@routes)
end
+ def has_migrations?
+ paths["db/migrate"].first.present?
+ end
+
def find_root_with_flag(flag, default=nil)
root_path = self.class.called_from