Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 627 lines (572 sloc) 21.718 kB
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
1 require 'rails/railtie'
812136a @jeremy Fix unstated usage of Pathname
jeremy authored
2 require 'active_support/core_ext/module/delegation'
3 require 'pathname'
53c13f1 @anildigital Use Config::CONFIG['host_os'] instead of RUBY_PLATFORM [#4477 state:r…
anildigital authored
4 require 'rbconfig'
c787bfd @drogus Engine can now load its own plugins
drogus authored
5 require 'rails/engine/railties'
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
6
7 module Rails
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
8 # Rails::Engine allows you to wrap a specific Rails application or subset of
9 # functionality and share it with other applications. Since Rails 3.0, every
10 # <tt>Rails::Application</tt> is just an engine, which allows for simple
11 # feature and application sharing.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
12 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
13 # Any <tt>Rails::Engine</tt> is also a <tt>Rails::Railtie</tt>, so the same
14 # methods (like <tt>rake_tasks</tt> and +generators+) and configuration
15 # options that are available in railties can also be used in engines.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
16 #
17 # == Creating an Engine
18 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
19 # In Rails versions prior to 3.0, your gems automatically behaved as engines, however,
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
20 # this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
21 # behave as an engine, you have to specify an +Engine+ for it somewhere inside
22 # your plugin's +lib+ folder (similar to how we specify a +Railtie+):
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
23 #
24 # # lib/my_engine.rb
25 # module MyEngine
26 # class Engine < Rails::Engine
27 # end
28 # end
29 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
30 # Then ensure that this file is loaded at the top of your <tt>config/application.rb</tt>
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
31 # (or in your +Gemfile+) and it will automatically load models, controllers and helpers
32 # inside +app+, load routes at <tt>config/routes.rb</tt>, load locales at
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
33 # <tt>config/locales/*</tt>, and load tasks at <tt>lib/tasks/*</tt>.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
34 #
35 # == Configuration
36 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
37 # Besides the +Railtie+ configuration which is shared across the application, in a
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
38 # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt>, <tt>eager_load_paths</tt>
39 # and <tt>autoload_once_paths</tt>, which, differently from a <tt>Railtie</tt>, are scoped to
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
40 # the current engine.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
41 #
42 # Example:
43 #
44 # class MyEngine < Rails::Engine
45 # # Add a load path for this specific Engine
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
46 # config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)
4bacc2a @josevalim Update the documentation for Engine and Railtie.
josevalim authored
47 #
48 # initializer "my_engine.add_middleware" do |app|
20f0e9f @jeremy Fix docs typo: app.middlewares -> app.middleware
jeremy authored
49 # app.middleware.use MyEngine::Middleware
4bacc2a @josevalim Update the documentation for Engine and Railtie.
josevalim authored
50 # end
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
51 # end
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
52 #
adfd43a @drogus Add documentation on app_generators
drogus authored
53 # == Generators
54 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
55 # You can set up generators for engines with <tt>config.generators</tt> method:
adfd43a @drogus Add documentation on app_generators
drogus authored
56 #
57 # class MyEngine < Rails::Engine
58 # config.generators do |g|
59 # g.orm :active_record
60 # g.template_engine :erb
61 # g.test_framework :test_unit
62 # end
63 # end
64 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
65 # You can also set generators for an application by using <tt>config.app_generators</tt>:
adfd43a @drogus Add documentation on app_generators
drogus authored
66 #
67 # class MyEngine < Rails::Engine
68 # # note that you can also pass block to app_generators in the same way you
69 # # can pass it to generators method
70 # config.app_generators.orm :datamapper
71 # end
72 #
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
73 # == Paths
74 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
75 # Since Rails 3.0, applications and engines have more flexible path configuration (as
46be69a @vijaydev spacing fix
vijaydev authored
76 # opposed to the previous hardcoded path configuration). This means that you are not
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
77 # required to place your controllers at <tt>app/controllers</tt>, but in any place
78 # which you find convenient.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
79 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
80 # For example, let's suppose you want to place your controllers in <tt>lib/controllers</tt>.
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
81 # You can set that as an option:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
82 #
83 # class MyEngine < Rails::Engine
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
84 # paths["app/controllers"] = "lib/controllers"
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
85 # end
86 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
87 # You can also have your controllers loaded from both <tt>app/controllers</tt> and
88 # <tt>lib/controllers</tt>:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
89 #
90 # class MyEngine < Rails::Engine
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
91 # paths["app/controllers"] << "lib/controllers"
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
92 # end
93 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
94 # The available paths in an engine are:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
95 #
96 # class MyEngine < Rails::Engine
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
97 # paths["app"] # => ["app"]
98 # paths["app/controllers"] # => ["app/controllers"]
99 # paths["app/helpers"] # => ["app/helpers"]
100 # paths["app/models"] # => ["app/models"]
101 # paths["app/views"] # => ["app/views"]
102 # paths["lib"] # => ["lib"]
103 # paths["lib/tasks"] # => ["lib/tasks"]
104 # paths["config"] # => ["config"]
105 # paths["config/initializers"] # => ["config/initializers"]
106 # paths["config/locales"] # => ["config/locales"]
107 # paths["config/routes"] # => ["config/routes.rb"]
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
108 # end
109 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
110 # The <tt>Application</tt> class adds a couple more paths to this set. And as in your
111 # <tt>Application</tt>, all folders under +app+ are automatically added to the load path.
112 # If you have an <tt>app/observers</tt> folder for example, it will be added by default.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
113 #
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
114 # == Endpoint
115 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
116 # An engine can be also a rack application. It can be useful if you have a rack application that
117 # you would like to wrap with +Engine+ and provide some of the +Engine+'s features.
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
118 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
119 # To do that, use the +endpoint+ method:
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
120 #
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
121 # module MyEngine
122 # class Engine < Rails::Engine
123 # endpoint MyRackApplication
124 # end
125 # end
126 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
127 # Now you can mount your engine in application's routes just like that:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
128 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
129 # MyRailsApp::Application.routes.draw do
130 # mount MyEngine::Engine => "/engine"
131 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
132 #
133 # == Middleware stack
134 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
135 # As an engine can now be a rack endpoint, it can also have a middleware
136 # stack. The usage is exactly the same as in <tt>Application</tt>:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
137 #
138 # module MyEngine
139 # class Engine < Rails::Engine
140 # middleware.use SomeMiddleware
141 # end
142 # end
143 #
144 # == Routes
145 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
146 # If you don't specify an endpoint, routes will be used as the default
147 # endpoint. You can use them just like you use an application's routes:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
148 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
149 # # ENGINE/config/routes.rb
150 # MyEngine::Engine.routes.draw do
151 # match "/" => "posts#index"
152 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
153 #
154 # == Mount priority
155 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
156 # Note that now there can be more than one router in your application, and it's better to avoid
157 # passing requests through many routers. Consider this situation:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
158 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
159 # MyRailsApp::Application.routes.draw do
160 # mount MyEngine::Engine => "/blog"
161 # match "/blog/omg" => "main#omg"
162 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
163 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
164 # +MyEngine+ is mounted at <tt>/blog</tt>, and <tt>/blog/omg</tt> points to application's
165 # controller. In such a situation, requests to <tt>/blog/omg</tt> will go through +MyEngine+,
166 # and if there is no such route in +Engine+'s routes, it will be dispatched to <tt>main#omg</tt>.
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
167 # It's much better to swap that:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
168 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
169 # MyRailsApp::Application.routes.draw do
170 # match "/blog/omg" => "main#omg"
171 # mount MyEngine::Engine => "/blog"
172 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
173 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
174 # Now, +Engine+ will get only requests that were not handled by +Application+.
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
175 #
1a161c7 @drogus Document engine_name
drogus authored
176 # == Engine name
177 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
178 # There are some places where an Engine's name is used:
179 # * routes: when you mount an Engine with <tt>mount(MyEngine::Engine => '/my_engine')</tt>,
180 # it's used as default :as option
181 # * some of the rake tasks are based on engine name, e.g. <tt>my_engine:install:migrations</tt>,
182 # <tt>my_engine:install:assets</tt>
1a161c7 @drogus Document engine_name
drogus authored
183 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
184 # Engine name is set by default based on class name. For <tt>MyEngine::Engine</tt> it will be
ad3e057 @ugisozols Remove duplicate words.
ugisozols authored
185 # <tt>my_engine_engine</tt>. You can change it manually using the <tt>engine_name</tt> method:
1a161c7 @drogus Document engine_name
drogus authored
186 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
187 # module MyEngine
188 # class Engine < Rails::Engine
189 # engine_name "my_engine"
190 # end
1a161c7 @drogus Document engine_name
drogus authored
191 # end
192 #
8e955c5 @drogus Should be isolated engine instead of namespaced engine
drogus authored
193 # == Isolated Engine
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
194 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
195 # Normally when you create controllers, helpers and models inside an engine, they are treated
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
196 # as if they were created inside the application itself. This means that all helpers and
197 # named routes from the application will be available to your engine's controllers as well.
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
198 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
199 # However, sometimes you want to isolate your engine from the application, especially if your engine
200 # has its own router. To do that, you simply need to call +isolate_namespace+. This method requires
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
201 # you to pass a module where all your controllers, helpers and models should be nested to:
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
202 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
203 # module MyEngine
204 # class Engine < Rails::Engine
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
205 # isolate_namespace MyEngine
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
206 # end
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
207 # end
208 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
209 # With such an engine, everything that is inside the +MyEngine+ module will be isolated from
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
210 # the application.
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
211 #
212 # Consider such controller:
213 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
214 # module MyEngine
215 # class FooController < ActionController::Base
216 # end
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
217 # end
218 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
219 # If an engine is marked as isolated, +FooController+ has access only to helpers from +Engine+ and
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
220 # <tt>url_helpers</tt> from <tt>MyEngine::Engine.routes</tt>.
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
221 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
222 # The next thing that changes in isolated engines is the behaviour of routes. Normally, when you namespace
223 # your controllers, you also need to do namespace all your routes. With an isolated engine,
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
224 # the namespace is applied by default, so you can ignore it in routes:
225 #
226 # MyEngine::Engine.routes.draw do
227 # resources :articles
228 # end
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
229 #
230 # The routes above will automatically point to <tt>MyEngine::ApplicationContoller</tt>. Furthermore, you don't
737abe4 @rtlechow Railties typos.
rtlechow authored
231 # need to use longer url helpers like <tt>my_engine_articles_path</tt>. Instead, you should simply use
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
232 # <tt>articles_path</tt> as you would do with your application.
233 #
234 # To make that behaviour consistent with other parts of the framework, an isolated engine also has influence on
235 # <tt>ActiveModel::Naming</tt>. When you use a namespaced model, like <tt>MyEngine::Article</tt>, it will normally
737abe4 @rtlechow Railties typos.
rtlechow authored
236 # use the prefix "my_engine". In an isolated engine, the prefix will be omitted in url helpers and
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
237 # form fields for convenience.
07411ca @drogus List all of isolated engine changes
drogus authored
238 #
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
239 # polymorphic_url(MyEngine::Article.new) # => "articles_path"
07411ca @drogus List all of isolated engine changes
drogus authored
240 #
241 # form_for(MyEngine::Article.new) do
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
242 # text_field :title # => <input type="text" name="article[title]" id="article_title" />
07411ca @drogus List all of isolated engine changes
drogus authored
243 # end
244 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
245 # Additionally, an isolated engine will set its name according to namespace, so
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
246 # MyEngine::Engine.engine_name will be "my_engine". It will also set MyEngine.table_name_prefix
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
247 # to "my_engine_", changing the MyEngine::Article model to use the my_engine_article table.
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
248 #
434139f @drogus Documented mounted helpers
drogus authored
249 # == Using Engine's routes outside Engine
250 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
251 # Since you can now mount an engine inside application's routes, you do not have direct access to +Engine+'s
252 # <tt>url_helpers</tt> inside +Application+. When you mount an engine in an application's routes, a special helper is
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
253 # created to allow you to do that. Consider such a scenario:
434139f @drogus Documented mounted helpers
drogus authored
254 #
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
255 # # config/routes.rb
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
256 # MyApplication::Application.routes.draw do
257 # mount MyEngine::Engine => "/my_engine", :as => "my_engine"
258 # match "/foo" => "foo#index"
259 # end
434139f @drogus Documented mounted helpers
drogus authored
260 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
261 # Now, you can use the <tt>my_engine</tt> helper inside your application:
434139f @drogus Documented mounted helpers
drogus authored
262 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
263 # class FooController < ApplicationController
264 # def index
265 # my_engine.root_url #=> /my_engine/
266 # end
434139f @drogus Documented mounted helpers
drogus authored
267 # end
268 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
269 # There is also a <tt>main_app</tt> helper that gives you access to application's routes inside Engine:
434139f @drogus Documented mounted helpers
drogus authored
270 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
271 # module MyEngine
272 # class BarController
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
273 # def index
274 # main_app.foo_path #=> /foo
275 # end
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
276 # end
434139f @drogus Documented mounted helpers
drogus authored
277 # end
278 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
279 # Note that the <tt>:as</tt> option given to mount takes the <tt>engine_name</tt> as default, so most of the time
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
280 # you can simply omit it.
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
281 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
282 # Finally, if you want to generate a url to an engine's route using
283 # <tt>polymorphic_url</tt>, you also need to pass the engine helper. Let's
284 # say that you want to create a form pointing to one of the engine's routes.
285 # All you need to do is pass the helper as the first element in array with
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
286 # attributes for url:
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
287 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
288 # form_for([my_engine, @user])
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
289 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
290 # This code will use <tt>my_engine.user_path(@user)</tt> to generate the proper route.
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
291 #
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
292 # == Isolated engine's helpers
293 #
294 # Sometimes you may want to isolate engine, but use helpers that are defined for it.
295 # If you want to share just a few specific helpers you can add them to application's
296 # helpers in ApplicationController:
297 #
298 # class ApplicationController < ActionController::Base
299 # helper MyEngine::SharedEngineHelper
300 # end
301 #
cb76771 @MDaubs Minor typo describing engine helpers
MDaubs authored
302 # If you want to include all of the engine's helpers, you can use #helpers method on an engine's
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
303 # instance:
304 #
305 # class ApplicationController < ActionController::Base
306 # helper MyEngine::Engine.helpers
307 # end
308 #
309 # It will include all of the helpers from engine's directory. Take into account that this does
310 # not include helpers defined in controllers with helper_method or other similar solutions,
cb76771 @MDaubs Minor typo describing engine helpers
MDaubs authored
311 # only helpers defined in the helpers directory will be included.
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
312 #
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
313 # == Migrations & seed data
314 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
315 # Engines can have their own migrations. The default path for migrations is exactly the same
316 # as in application: <tt>db/migrate</tt>
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
317 #
318 # To use engine's migrations in application you can use rake task, which copies them to
319 # application's dir:
320 #
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
321 # rake ENGINE_NAME:install:migrations
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
322 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
323 # Note that some of the migrations may be skipped if a migration with the same name already exists
324 # in application. In such a situation you must decide whether to leave that migration or rename the
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
325 # migration in the application and rerun copying migrations.
a2c52f1 @drogus Update documentation for new tasks
drogus authored
326 #
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
327 # If your engine has migrations, you may also want to prepare data for the database in
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
328 # the <tt>seeds.rb</tt> file. You can load that data using the <tt>load_seed</tt> method, e.g.
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
329 #
330 # MyEngine::Engine.load_seed
331 #
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
332 class Engine < Railtie
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
333 autoload :Configuration, "rails/engine/configuration"
37d3266 @josevalim Add missing autoload.
josevalim authored
334 autoload :Railties, "rails/engine/railties"
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
335
60281af @flippingbits Move load_generators from Rails::Application to Rails::Engine
flippingbits authored
336 def load_generators(app=self)
337 initialize_generators
338 railties.all { |r| r.load_generators(app) }
339 Rails::Generators.configure!(app.config.generators)
340 super
341 self
342 end
343
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
344 class << self
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
345 attr_accessor :called_from, :isolated
ef4afed @drogus There is no need to make isolated? method with bang-bang, just alias …
drogus authored
346 alias :isolated? :isolated
2068b8c @drogus Added tests for railtie_name and aliased it in engine as engine_name
drogus authored
347 alias :engine_name :railtie_name
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
348
349 def inherited(base)
f5ee855 @josevalim Improve heuristic for railties default name, otherwise railties may b…
josevalim authored
350 unless base.abstract_railtie?
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
351 base.called_from = begin
a5684df @josevalim Ensure config.after_initializer is executed before building the middl…
josevalim authored
352 # Remove the line number from backtraces making sure we don't leave anything behind
4bfe30c @spastorino delete from :number until the end
spastorino authored
353 call_stack = caller.map { |p| p.sub(/:\d+.*/, '') }
21ff884 @fxn in regexps, the dot in a character class is not a metacharacter
fxn authored
354 File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] })
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
355 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
356 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
357
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
358 super
359 end
360
3939d6b @drogus Removed ActionDispatch::Static, but left empty MiddlewareStack to uni…
drogus authored
361 def endpoint(endpoint = nil)
362 @endpoint = endpoint if endpoint
363 @endpoint
364 end
c7664d1 @drogus Include application's helpers and router helpers by default, but incl…
drogus authored
365
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
366 def isolate_namespace(mod)
db8a864 @drogus Add table_name_prefix to Engine's namespace automatically
drogus authored
367 engine_name(generate_railtie_name(mod))
368
0a28073 Engines that use isolate_namespace with nested modules should set cor…
Rolf Timmermans authored
369 self.routes.default_scope = { :module => ActiveSupport::Inflector.underscore(mod.name) }
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
370 self.isolated = true
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
371
372 unless mod.respond_to?(:_railtie)
0a28073 Engines that use isolate_namespace with nested modules should set cor…
Rolf Timmermans authored
373 name = engine_name
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
374 _railtie = self
375 mod.singleton_class.instance_eval do
376 define_method(:_railtie) do
377 _railtie
378 end
379
f821055 @drogus Do not create table_name_prefix method on isolated module if the meth…
drogus authored
380 unless mod.respond_to?(:table_name_prefix)
381 define_method(:table_name_prefix) do
382 "#{name}_"
383 end
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
384 end
385 end
386 end
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
387 end
051127d @drogus Rails::Engine.find(path) - method to find engine by path
drogus authored
388
389 # Finds engine with given path
390 def find(path)
c7cfdd0 @dcrec1 refactored Engine.#find by giving an explicit name to a variable and …
dcrec1 authored
391 expanded_path = File.expand_path path.to_s
392 Rails::Engine::Railties.engines.find { |engine|
393 File.expand_path(engine.root.to_s) == expanded_path
1db4969 @tenderlove only compute path.to_s once
tenderlove authored
394 }
051127d @drogus Rails::Engine.find(path) - method to find engine by path
drogus authored
395 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
396 end
397
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
398 delegate :middleware, :root, :paths, :to => :config
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
399 delegate :engine_name, :isolated?, :to => "self.class"
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
400
245dba0 @jhtwong Cherry-picking patch for https://github.com/rails/rails/issues/1460
jhtwong authored
401 def load_tasks(app=self)
402 railties.all { |r| r.load_tasks(app) }
7317d9e @josh Remove implicit controller namespacing from new dsl
josh authored
403 super
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
404 paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
405 end
245dba0 @jhtwong Cherry-picking patch for https://github.com/rails/rails/issues/1460
jhtwong authored
406
407 def load_console(app=self)
408 railties.all { |r| r.load_console(app) }
409 super
410 end
411
351816f @josevalim Ensure that eager_load actually takes place just after the middleware…
josevalim authored
412 def eager_load!
245dba0 @jhtwong Cherry-picking patch for https://github.com/rails/rails/issues/1460
jhtwong authored
413 railties.all(&:eager_load!)
414
351816f @josevalim Ensure that eager_load actually takes place just after the middleware…
josevalim authored
415 config.eager_load_paths.each do |load_path|
416 matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
417 Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
418 require_dependency file.sub(matcher, '\1')
419 end
420 end
421 end
422
c787bfd @drogus Engine can now load its own plugins
drogus authored
423 def railties
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
424 @railties ||= self.class::Railties.new(config)
c787bfd @drogus Engine can now load its own plugins
drogus authored
425 end
426
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
427 def helpers
428 @helpers ||= begin
429 helpers = Module.new
430
431 helpers_paths = if config.respond_to?(:helpers_paths)
432 config.helpers_paths
433 else
434 paths["app/helpers"].existent
435 end
436
d4bea35 @drogus Make ActionController::Base.modules_for_helpers and ActionController:…
drogus authored
437 all = ActionController::Base.all_helpers_from_path(helpers_paths)
438 ActionController::Base.modules_for_helpers(all).each do |mod|
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
439 helpers.send(:include, mod)
440 end
441 helpers
442 end
443 end
444
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
445 def app
092b148 @drogus Engine can now serve files with ActionDispatch::Static
drogus authored
446 @app ||= begin
447 config.middleware = config.middleware.merge_into(default_middleware_stack)
448 config.middleware.build(endpoint)
449 end
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
450 end
451
452 def endpoint
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
453 self.class.endpoint || routes
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
454 end
455
456 def call(env)
a132229 @drogus Added ability to set asset_path for engines
drogus authored
457 app.call(env.merge!(env_config))
458 end
459
460 def env_config
461 @env_config ||= {
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
462 'action_dispatch.routes' => routes
a132229 @drogus Added ability to set asset_path for engines
drogus authored
463 }
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
464 end
465
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
466 def routes
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
467 @routes ||= ActionDispatch::Routing::RouteSet.new
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
468 @routes.append(&Proc.new) if block_given?
ec5d846 @drogus Properly reload routes defined in class definition
drogus authored
469 @routes
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
470 end
471
675f3ea @drogus Gather initializers from railties in engines to get rid of additional…
drogus authored
472 def initializers
473 initializers = []
474 railties.all { |r| initializers += r.initializers }
475 initializers += super
476 initializers
477 end
478
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
479 def config
937f419 @drogus Engine's assets are now served with ActionDispatch::Static
drogus authored
480 @config ||= Engine::Configuration.new(find_root_with_flag("lib"))
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
481 end
482
d475de7 @drogus Add helper for loading seed data for engine and application
drogus authored
483 # Load data from db/seeds.rb file. It can be used in to load engines'
484 # seeds, e.g.:
485 #
486 # Blog::Engine.load_seed
487 def load_seed
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
488 seed_file = paths["db/seeds"].existent.first
d475de7 @drogus Add helper for loading seed data for engine and application
drogus authored
489 load(seed_file) if File.exist?(seed_file)
490 end
491
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
492 # Add configured load paths to ruby load paths and remove duplicates.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
493 initializer :set_load_path, :before => :bootstrap_hook do
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
494 _all_load_paths.reverse_each do |path|
4ae7936 @josevalim Got tests working once again.
josevalim authored
495 $LOAD_PATH.unshift(path) if File.directory?(path)
496 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
497 $LOAD_PATH.uniq!
498 end
499
500 # Set the paths from which Rails will automatically load source files,
501 # and the load_once paths.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
502 #
503 # This needs to be an initializer, since it needs to run once
504 # per engine and get the engine as a block parameter
505 initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
506 ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
507 ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
508
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
509 # Freeze so future modifications will fail rather than do nothing mysteriously
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
510 config.autoload_paths.freeze
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
511 config.eager_load_paths.freeze
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
512 config.autoload_once_paths.freeze
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
513 end
514
226d8e7 @josevalim Refactor MetalLoader and RoutesReloader to rely less on class configu…
josevalim authored
515 initializer :add_routing_paths do |app|
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
516 paths = self.paths["config/routes"].existent
517
518 if routes? || paths.any?
519 app.routes_reloader.paths.unshift(*paths)
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
520 app.routes_reloader.route_sets << routes
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
521 end
522 end
523
1177a40 @josevalim Fix i18n locales order test.
josevalim authored
524 # I18n load paths are a special case since the ones added
525 # later have higher priority.
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
526 initializer :add_locales do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
527 config.i18n.railties_load_path.concat(paths["config/locales"].existent)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
528 end
529
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
530 initializer :add_view_paths do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
531 views = paths["app/views"].existent
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
532 unless views.empty?
533 ActiveSupport.on_load(:action_controller){ prepend_view_path(views) }
534 ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) }
4aded43 @wycats Replace the placeholder base_hook API with on_load. To specify some c…
wycats authored
535 end
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
536 end
537
7cccfed @drogus Allow Engines loading its own environment file from config/environments
drogus authored
538 initializer :load_environment_config, :before => :load_environment_hook do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
539 environment = paths["config/environments"].existent.first
7cccfed @drogus Allow Engines loading its own environment file from config/environments
drogus authored
540 require environment if environment
541 end
542
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
543 initializer :append_assets_path do |app|
266b80c @chriseppstein OrderedOptions must implement respond_to? if it implements method_mis…
chriseppstein authored
544 app.config.assets.paths.unshift(*paths["vendor/assets"].existent)
545 app.config.assets.paths.unshift(*paths["lib/assets"].existent)
546 app.config.assets.paths.unshift(*paths["app/assets"].existent)
2fe70c1 @josevalim Booya, sprockets now works from Engines.
josevalim authored
547 end
548
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
549 initializer :prepend_helpers_path do |app|
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
550 if !isolated? || (app == self)
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
551 app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
552 end
553 end
554
9af189a @drogus I've changed that test along the way, it should actually stay without…
drogus authored
555 initializer :load_config_initializers do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
556 config.paths["config/initializers"].existent.sort.each do |initializer|
9af189a @drogus I've changed that test along the way, it should actually stay without…
drogus authored
557 load(initializer)
558 end
559 end
560
b43b686 @drogus engines_blank_point should always be the last initializer in Engine
drogus authored
561 initializer :engines_blank_point do
562 # We need this initializer so all extra initializers added in engines are
563 # consistently executed after all the initializers above across all engines.
564 end
565
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
566 rake_tasks do
567 next if self.is_a?(Rails::Application)
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
568 next unless has_migrations?
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
569
570 namespace railtie_name do
571 namespace :install do
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
572 desc "Copy migrations from #{railtie_name} to application"
573 task :migrations do
574 ENV["FROM"] = railtie_name
575 Rake::Task["railties:install:migrations"].invoke
959f857 @drogus Added foo:install:assets task that copies assets from plugins public …
drogus authored
576 end
f07cbec @drogus Do not treat information about skipped migrations as WARNINGs but as …
drogus authored
577 end
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
578 end
579 end
580
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
581 protected
1860d87 @josevalim Ensure asset_path defaults to nil for application
josevalim authored
582
60281af @flippingbits Move load_generators from Rails::Application to Rails::Engine
flippingbits authored
583 def initialize_generators
584 require "rails/generators"
585 end
586
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
587 def routes?
588 defined?(@routes)
589 end
590
d84b2f3 @josevalim Fix tests.
josevalim authored
591 def has_migrations?
a7a58af @spohlenz Fix has_migrations? check in Rails::Engine
spohlenz authored
592 paths["db/migrate"].existent.any?
d84b2f3 @josevalim Fix tests.
josevalim authored
593 end
594
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
595 def find_root_with_flag(flag, default=nil)
596 root_path = self.class.called_from
597
598 while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
599 parent = File.dirname(root_path)
600 root_path = parent != root_path && parent
601 end
602
603 root = File.exist?("#{root_path}/#{flag}") ? root_path : default
604 raise "Could not find root path for #{self}" unless root
605
4a4ff14 @josevalim Use RbConfig instead of Config for 1.9.3 compatibility.
josevalim authored
606 RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ?
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
607 Pathname.new(root).expand_path : Pathname.new(root).realpath
608 end
609
3939d6b @drogus Removed ActionDispatch::Static, but left empty MiddlewareStack to uni…
drogus authored
610 def default_middleware_stack
611 ActionDispatch::MiddlewareStack.new
612 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
613
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
614 def _all_autoload_once_paths
615 config.autoload_once_paths
616 end
617
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
618 def _all_autoload_paths
619 @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
620 end
621
622 def _all_load_paths
623 @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
624 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
625 end
6324eee @jeremy plugin rails/init.rb deprecation message
jeremy authored
626 end
Something went wrong with that request. Please try again.