class Rails::Engine
“app/observers”, it’s added by default.
all folders under “app” are automatically added to the load path. So if you have
Your Application class adds a couple more paths to this set. And as in your Application,
end
paths.config.routes = “config/routes.rb”
paths.config.locales = “config/locales”
paths.config.initializers = “config/initializers”
paths.config = “config”
paths.lib.tasks = “lib/tasks”
paths.lib = “lib”
paths.app.views = “app/views”
paths.app.models = “app/models”
paths.app.helpers = “app/helpers”
paths.app.controllers = “app/controllers”
paths.app = “app”
class MyEngine < Rails::Engine
The available paths in an Engine are:
end
paths.app.controllers << “lib/controllers”
class MyEngine < Rails::Engine
“lib/controllers”:
You can also have your controllers being loaded from both “app/controllers” and
end
paths.app.controllers = “lib/controllers”
class MyEngine < Rails::Engine
you need to do is:
For example, let’s suppose you want to lay your controllers at lib/controllers, all
but in any place which you find convenient.
This means that you are not required to place your controllers at “app/controllers”,
Since Rails 3.0, both your Application and Engines do not have hardcoded paths.
== Paths
end
end
app.middleware.use MyEngine::Middleware
initializer “my_engine.add_middleware” do |app|
config.autoload_paths << File.expand_path(“../lib/some/path”, __FILE__)
# Add a load path for this specific Engine
class MyEngine < Rails::Engine
Example:
which differently from a Railtie, are scoped to the current Engine.
Rails::Engine you can access autoload_paths, eager_load_paths and autoload_once_paths,
Besides the Railtie configuration which is shared across the application, in a
== Configuration
load tasks at “lib/tasks/*”.
inside app, load routes at “config/routes.rb”, load locales at “config/locales/*”,
your Gemfile) and it will automatically load models, controllers and helpers
Then ensure that this file is loaded at the top of your config/application.rb (or in
end
end
class Engine < Rails::Engine
module MyEngine
# lib/my_engine.rb
lib folder (similar with how we spceify a Railtie):
behave as Engine, you have to specify an Engine for it somewhere inside your plugin
this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically
In Rails versions before to 3.0, your gems automatically behaved as Engine, however
== Creating an Engine
generators) and configuration available in the latter can also be used in the former.
Any Rails::Engine is also a Rails::Railtie, so the same methods (like rake_tasks and
more than an Engine, allowing you to share it very easily.
different applications. Since Rails 3.0, every Rails::Application is nothing
Rails::Engine allows you to wrap a specific Rails application and share it accross
def _all_autoload_paths
def _all_autoload_paths @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq end
def _all_load_paths
def _all_load_paths @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq end
def eager_load!
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| require_dependency file.sub(matcher, '\1') end end end
def find_root_with_flag(flag, default=nil)
def find_root_with_flag(flag, default=nil) root_path = self.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 RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? Pathname.new(root).expand_path : Pathname.new(root).realpath end
def inherited(base)
def inherited(base) unless base.abstract_railtie? base.called_from = begin # Remove the line number from backtraces making sure we don't leave anything behind call_stack = caller.map { |p| p.split(':')[0..-2].join(':') } File.dirname(call_stack.detect { |p| p !~ %r[railties[\w\-\.]*/lib/rails|rack[\w\-\.]*/lib/rack] }) end end super end
def load_tasks
def load_tasks super config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) } end