Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 696 lines (633 sloc) 24.392 kb
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
1 require 'rails/railtie'
45aabe6 @spastorino There's no need to access Railties through self.class
spastorino authored
2 require 'rails/engine/railties'
812136a @jeremy Fix unstated usage of Pathname
jeremy authored
3 require 'active_support/core_ext/module/delegation'
4 require 'pathname'
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
5
6 module Rails
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
7 # <tt>Rails::Engine</tt> allows you to wrap a specific Rails application or subset of
c15e423 @shageman Updated description of what engines are used for
shageman authored
8 # functionality and share it with other applications or within a larger packaged application.
271a552 @claudiob [ci skip] Remove comments about Rails 3.1
claudiob authored
9 # Every <tt>Rails::Application</tt> is just an engine, which allows for simple
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
10 # feature and application sharing.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
11 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
12 # Any <tt>Rails::Engine</tt> is also a <tt>Rails::Railtie</tt>, so the same
13 # methods (like <tt>rake_tasks</tt> and +generators+) and configuration
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
14 # options that are available in railties can also be used in engines.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
15 #
16 # == Creating an Engine
17 #
271a552 @claudiob [ci skip] Remove comments about Rails 3.1
claudiob authored
18 # If you want a gem to behave as an engine, you have to specify an +Engine+
19 # for it somewhere inside your plugin's +lib+ folder (similar to how we
20 # specify a +Railtie+):
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
21 #
22 # # lib/my_engine.rb
23 # module MyEngine
24 # class Engine < Rails::Engine
25 # end
26 # end
27 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
28 # 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
29 # (or in your +Gemfile+) and it will automatically load models, controllers and helpers
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
30 # inside +app+, load routes at <tt>config/routes.rb</tt>, load locales at
31 # <tt>config/locales/*</tt>, and load tasks at <tt>lib/tasks/*</tt>.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
32 #
33 # == Configuration
34 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
35 # Besides the +Railtie+ configuration which is shared across the application, in a
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
36 # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt>, <tt>eager_load_paths</tt>
37 # and <tt>autoload_once_paths</tt>, which, differently from a <tt>Railtie</tt>, are scoped to
9d9b22f @pixeltrix Revert "Deprecate the `eager_load_paths` configuration"
pixeltrix authored
38 # the current engine.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
39 #
40 # class MyEngine < Rails::Engine
41 # # Add a load path for this specific Engine
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
42 # config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)
4bacc2a @josevalim Update the documentation for Engine and Railtie.
josevalim authored
43 #
44 # initializer "my_engine.add_middleware" do |app|
20f0e9f @jeremy Fix docs typo: app.middlewares -> app.middleware
jeremy authored
45 # app.middleware.use MyEngine::Middleware
4bacc2a @josevalim Update the documentation for Engine and Railtie.
josevalim authored
46 # end
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
47 # end
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
48 #
adfd43a @drogus Add documentation on app_generators
drogus authored
49 # == Generators
50 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
51 # You can set up generators for engines with <tt>config.generators</tt> method:
adfd43a @drogus Add documentation on app_generators
drogus authored
52 #
53 # class MyEngine < Rails::Engine
54 # config.generators do |g|
55 # g.orm :active_record
56 # g.template_engine :erb
57 # g.test_framework :test_unit
58 # end
59 # end
60 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
61 # You can also set generators for an application by using <tt>config.app_generators</tt>:
adfd43a @drogus Add documentation on app_generators
drogus authored
62 #
63 # class MyEngine < Rails::Engine
64 # # note that you can also pass block to app_generators in the same way you
65 # # can pass it to generators method
66 # config.app_generators.orm :datamapper
67 # end
68 #
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
69 # == Paths
70 #
271a552 @claudiob [ci skip] Remove comments about Rails 3.1
claudiob authored
71 # Applications and engines have flexible path configuration, meaning that you
72 # are not required to place your controllers at <tt>app/controllers</tt>, but
73 # in any place which you find convenient.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
74 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
75 # 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
76 # You can set that as an option:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
77 #
78 # class MyEngine < Rails::Engine
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
79 # paths["app/controllers"] = "lib/controllers"
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
80 # end
81 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
82 # You can also have your controllers loaded from both <tt>app/controllers</tt> and
83 # <tt>lib/controllers</tt>:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
84 #
85 # class MyEngine < Rails::Engine
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
86 # paths["app/controllers"] << "lib/controllers"
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
87 # end
88 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
89 # The available paths in an engine are:
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
90 #
91 # class MyEngine < Rails::Engine
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
92 # paths["app"] # => ["app"]
93 # paths["app/controllers"] # => ["app/controllers"]
94 # paths["app/helpers"] # => ["app/helpers"]
95 # paths["app/models"] # => ["app/models"]
96 # paths["app/views"] # => ["app/views"]
97 # paths["lib"] # => ["lib"]
98 # paths["lib/tasks"] # => ["lib/tasks"]
99 # paths["config"] # => ["config"]
100 # paths["config/initializers"] # => ["config/initializers"]
101 # paths["config/locales"] # => ["config/locales"]
d1f33e0 @semaperepelitsa Fix typo in file path, should include extension.
semaperepelitsa authored
102 # paths["config/routes.rb"] # => ["config/routes.rb"]
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
103 # end
104 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
105 # The <tt>Application</tt> class adds a couple more paths to this set. And as in your
106 # <tt>Application</tt>, all folders under +app+ are automatically added to the load path.
107 # If you have an <tt>app/services</tt> folder for example, it will be added by default.
781d0a9 @josevalim Add docs for Railtie, Engine, Plugin and Application.
josevalim authored
108 #
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
109 # == Endpoint
110 #
6f57e12 @georgemillo Minor English fixes in docs [ci skip]
georgemillo authored
111 # An engine can also be a rack application. It can be useful if you have a rack application that
112 # you would like to wrap with +Engine+ and provide with some of the +Engine+'s features.
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
113 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
114 # To do that, use the +endpoint+ method:
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
115 #
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
116 # module MyEngine
117 # class Engine < Rails::Engine
118 # endpoint MyRackApplication
119 # end
120 # end
121 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
122 # 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
123 #
9703d67 @wangjohn Removing the app constant and replacing it with Rails.application
wangjohn authored
124 # Rails.application.routes.draw do
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
125 # mount MyEngine::Engine => "/engine"
126 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
127 #
128 # == Middleware stack
129 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
130 # As an engine can now be a rack endpoint, it can also have a middleware
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
131 # 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
132 #
133 # module MyEngine
134 # class Engine < Rails::Engine
135 # middleware.use SomeMiddleware
136 # end
137 # end
138 #
139 # == Routes
140 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
141 # If you don't specify an endpoint, routes will be used as the default
142 # 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
143 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
144 # # ENGINE/config/routes.rb
145 # MyEngine::Engine.routes.draw do
56cdc81 @wycats Remove default match without specified method
wycats authored
146 # get "/" => "posts#index"
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
147 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
148 #
149 # == Mount priority
150 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
151 # Note that now there can be more than one router in your application, and it's better to avoid
152 # passing requests through many routers. Consider this situation:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
153 #
9703d67 @wangjohn Removing the app constant and replacing it with Rails.application
wangjohn authored
154 # Rails.application.routes.draw do
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
155 # mount MyEngine::Engine => "/blog"
56cdc81 @wycats Remove default match without specified method
wycats authored
156 # get "/blog/omg" => "main#omg"
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
157 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
158 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
159 # +MyEngine+ is mounted at <tt>/blog</tt>, and <tt>/blog/omg</tt> points to application's
160 # controller. In such a situation, requests to <tt>/blog/omg</tt> will go through +MyEngine+,
161 # 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
162 # It's much better to swap that:
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
163 #
9703d67 @wangjohn Removing the app constant and replacing it with Rails.application
wangjohn authored
164 # Rails.application.routes.draw do
56cdc81 @wycats Remove default match without specified method
wycats authored
165 # get "/blog/omg" => "main#omg"
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
166 # mount MyEngine::Engine => "/blog"
167 # end
153df92 @drogus Added documentation on endpoint, middeware stack and routes for Engine
drogus authored
168 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
169 # 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
170 #
1a161c7 @drogus Document engine_name
drogus authored
171 # == Engine name
172 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
173 # There are some places where an Engine's name is used:
38310ab @fxn little details seen while doing a pass through what's new in docrails
fxn authored
174 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
175 # * routes: when you mount an Engine with <tt>mount(MyEngine::Engine => '/my_engine')</tt>,
176 # it's used as default <tt>:as</tt> option
177 # * rake task for installing migrations <tt>my_engine:install:migrations</tt>
1a161c7 @drogus Document engine_name
drogus authored
178 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
179 # Engine name is set by default based on class name. For <tt>MyEngine::Engine</tt> it will be
180 # <tt>my_engine_engine</tt>. You can change it manually using the <tt>engine_name</tt> method:
1a161c7 @drogus Document engine_name
drogus authored
181 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
182 # module MyEngine
183 # class Engine < Rails::Engine
184 # engine_name "my_engine"
185 # end
1a161c7 @drogus Document engine_name
drogus authored
186 # end
187 #
8e955c5 @drogus Should be isolated engine instead of namespaced engine
drogus authored
188 # == Isolated Engine
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
189 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
190 # 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
191 # as if they were created inside the application itself. This means that all helpers and
192 # 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
193 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
194 # However, sometimes you want to isolate your engine from the application, especially if your engine
195 # 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
196 # 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
197 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
198 # module MyEngine
199 # class Engine < Rails::Engine
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
200 # isolate_namespace MyEngine
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
201 # end
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
202 # end
203 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
204 # 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
205 # the application.
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
206 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
207 # Consider this controller:
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
208 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
209 # module MyEngine
210 # class FooController < ActionController::Base
211 # end
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
212 # end
213 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
214 # If the +MyEngine+ engine is marked as isolated, +FooController+ only has
215 # access to helpers from +MyEngine+, and <tt>url_helpers</tt> from
216 # <tt>MyEngine::Engine.routes</tt>.
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
217 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
218 # The next thing that changes in isolated engines is the behavior of routes.
219 # Normally, when you namespace your controllers, you also need to namespace
220 # the related routes. With an isolated engine, the engine's namespace is
221 # automatically applied, so you don't need to specify it explicity in your
222 # routes:
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
223 #
224 # MyEngine::Engine.routes.draw do
225 # resources :articles
226 # end
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
227 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
228 # If +MyEngine+ is isolated, The routes above will point to
229 # <tt>MyEngine::ArticlesController</tt>. You also don't need to use longer
230 # url helpers like +my_engine_articles_path+. Instead, you should simply use
231 # +articles_path+, like you would do with your main application.
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
232 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
233 # To make this behavior consistent with other parts of the framework,
234 # isolated engines also have an effect on <tt>ActiveModel::Naming</tt>. In a
235 # normal Rails app, when you use a namespaced model such as
236 # <tt>Namespace::Article</tt>, <tt>ActiveModel::Naming</tt> will generate
237 # names with the prefix "namespace". In an isolated engine, the prefix will
238 # be omitted in url helpers and form fields, for convenience.
07411ca @drogus List all of isolated engine changes
drogus authored
239 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
240 # polymorphic_url(MyEngine::Article.new)
241 # # => "articles_path" # not "my_engine_articles_path"
07411ca @drogus List all of isolated engine changes
drogus authored
242 #
243 # form_for(MyEngine::Article.new) do
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
244 # text_field :title # => <input type="text" name="article[title]" id="article_title" />
07411ca @drogus List all of isolated engine changes
drogus authored
245 # end
246 #
82dc25d @georgemillo fixing English in Rails::Engine docs
georgemillo authored
247 # Additionally, an isolated engine will set its own name according to its
248 # namespace, so <tt>MyEngine::Engine.engine_name</tt> will return
249 # "my_engine". It will also set +MyEngine.table_name_prefix+ to "my_engine_",
250 # meaning for example that <tt>MyEngine::Article</tt> will use the
251 # +my_engine_articles+ database table by default.
2e4e1e7 @drogus Added documentation for namespaced Engine
drogus authored
252 #
434139f @drogus Documented mounted helpers
drogus authored
253 # == Using Engine's routes outside Engine
254 #
2373eed @techpeace Make all references to engines lowercase in docs.
techpeace authored
255 # Since you can now mount an engine inside application's routes, you do not have direct access to +Engine+'s
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
256 # <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
257 # created to allow you to do that. Consider such a scenario:
434139f @drogus Documented mounted helpers
drogus authored
258 #
3e7a8cc @vijaydev formatting results in comments
vijaydev authored
259 # # config/routes.rb
9703d67 @wangjohn Removing the app constant and replacing it with Rails.application
wangjohn authored
260 # Rails.application.routes.draw do
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
261 # mount MyEngine::Engine => "/my_engine", as: "my_engine"
56cdc81 @wycats Remove default match without specified method
wycats authored
262 # get "/foo" => "foo#index"
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
263 # end
434139f @drogus Documented mounted helpers
drogus authored
264 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
265 # Now, you can use the <tt>my_engine</tt> helper inside your application:
434139f @drogus Documented mounted helpers
drogus authored
266 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
267 # class FooController < ApplicationController
268 # def index
efff6c1 @sikachu Change syntax format for example returned values
sikachu authored
269 # my_engine.root_url # => /my_engine/
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
270 # end
434139f @drogus Documented mounted helpers
drogus authored
271 # end
272 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
273 # 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
274 #
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
275 # module MyEngine
276 # class BarController
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
277 # def index
efff6c1 @sikachu Change syntax format for example returned values
sikachu authored
278 # main_app.foo_path # => /foo
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
279 # end
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
280 # end
434139f @drogus Documented mounted helpers
drogus authored
281 # end
282 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
283 # 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
284 # you can simply omit it.
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
285 #
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
286 # Finally, if you want to generate a url to an engine's route using
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
287 # <tt>polymorphic_url</tt>, you also need to pass the engine helper. Let's
2f30fb0 @mjankowski misc grammar and clarity changes for rails/engine docs
mjankowski authored
288 # say that you want to create a form pointing to one of the engine's routes.
289 # 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
290 # attributes for url:
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
291 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
292 # form_for([my_engine, @user])
34cd8a6 @drogus Add some more docs on polymorphic_url with routes proxy
drogus authored
293 #
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
294 # 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
295 #
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
296 # == Isolated engine's helpers
297 #
298 # Sometimes you may want to isolate engine, but use helpers that are defined for it.
299 # If you want to share just a few specific helpers you can add them to application's
300 # helpers in ApplicationController:
301 #
9ce5d51 @morgoth fixed example code indentation
morgoth authored
302 # class ApplicationController < ActionController::Base
303 # helper MyEngine::SharedEngineHelper
304 # end
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
305 #
a723519 @robertzk Fix some typos in railties/lib/rails/engine.rb [ci-skip]
robertzk authored
306 # If you want to include all of the engine's helpers, you can use the #helper method on an engine's
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
307 # instance:
308 #
9ce5d51 @morgoth fixed example code indentation
morgoth authored
309 # class ApplicationController < ActionController::Base
310 # helper MyEngine::Engine.helpers
311 # end
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
312 #
313 # It will include all of the helpers from engine's directory. Take into account that this does
314 # not include helpers defined in controllers with helper_method or other similar solutions,
cb76771 @MDaubs Minor typo describing engine helpers
MDaubs authored
315 # 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
316 #
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
317 # == Migrations & seed data
318 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
319 # Engines can have their own migrations. The default path for migrations is exactly the same
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
320 # as in application: <tt>db/migrate</tt>
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
321 #
a723519 @robertzk Fix some typos in railties/lib/rails/engine.rb [ci-skip]
robertzk authored
322 # To use engine's migrations in application you can use the rake task below, which copies them to
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
323 # application's dir:
324 #
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
325 # rake ENGINE_NAME:install:migrations
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
326 #
5f5e3b7 @techpeace Copy-edit and format Rails::Engine docs.
techpeace authored
327 # Note that some of the migrations may be skipped if a migration with the same name already exists
328 # 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
329 # migration in the application and rerun copying migrations.
a2c52f1 @drogus Update documentation for new tasks
drogus authored
330 #
dfac9b1 @drogus Strengthen documantation on engine's migrations and seed data
drogus authored
331 # If your engine has migrations, you may also want to prepare data for the database in
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
332 # the <tt>db/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
333 #
334 # MyEngine::Engine.load_seed
335 #
40b19e0 @drogus Allow to change engine's loading priority with config.railties_order=
drogus authored
336 # == Loading priority
337 #
a723519 @robertzk Fix some typos in railties/lib/rails/engine.rb [ci-skip]
robertzk authored
338 # In order to change engine's priority you can use +config.railties_order+ in the main application.
40b19e0 @drogus Allow to change engine's loading priority with config.railties_order=
drogus authored
339 # It will affect the priority of loading views, helpers, assets and all the other files
340 # related to engine or application.
341 #
342 # # load Blog::Engine with highest priority, followed by application and other railties
343 # config.railties_order = [Blog::Engine, :main_app, :all]
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
344 class Engine < Railtie
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
345 autoload :Configuration, "rails/engine/configuration"
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
346
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
347 class << self
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
348 attr_accessor :called_from, :isolated
2801786 @josevalim Get rid of config.preload_frameworks in favor of config.eager_load_na…
josevalim authored
349
ef4afed @drogus There is no need to make isolated? method with bang-bang, just alias …
drogus authored
350 alias :isolated? :isolated
2068b8c @drogus Added tests for railtie_name and aliased it in engine as engine_name
drogus authored
351 alias :engine_name :railtie_name
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
352
2801786 @josevalim Get rid of config.preload_frameworks in favor of config.eager_load_na…
josevalim authored
353 delegate :eager_load!, to: :instance
354
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
355 def inherited(base)
f5ee855 @josevalim Improve heuristic for railties default name, otherwise railties may b…
josevalim authored
356 unless base.abstract_railtie?
2801786 @josevalim Get rid of config.preload_frameworks in favor of config.eager_load_na…
josevalim authored
357 Rails::Railtie::Configuration.eager_load_namespaces << base
358
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
359 base.called_from = begin
5374960 @amatsuda Use Ruby 2.0 caller_locations instead of caller if available
amatsuda authored
360 call_stack = if Kernel.respond_to?(:caller_locations)
6edc26f @SamSaffron FIX: absolute_path may be nil for code added via instance_eval
SamSaffron authored
361 caller_locations.map { |l| l.absolute_path || l.path }
5374960 @amatsuda Use Ruby 2.0 caller_locations instead of caller if available
amatsuda authored
362 else
363 # Remove the line number from backtraces making sure we don't leave anything behind
364 caller.map { |p| p.sub(/:\d+.*/, '') }
365 end
366
21ff884 @fxn in regexps, the dot in a character class is not a metacharacter
fxn authored
367 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
368 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
369 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
370
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
371 super
372 end
373
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
374 def find_root(from)
375 find_root_with_flag "lib", from
376 end
377
3939d6b @drogus Removed ActionDispatch::Static, but left empty MiddlewareStack to uni…
drogus authored
378 def endpoint(endpoint = nil)
44f0709 @tenderlove dealing with some -w warnings on startup
tenderlove authored
379 @endpoint ||= nil
3939d6b @drogus Removed ActionDispatch::Static, but left empty MiddlewareStack to uni…
drogus authored
380 @endpoint = endpoint if endpoint
381 @endpoint
382 end
c7664d1 @drogus Include application's helpers and router helpers by default, but incl…
drogus authored
383
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
384 def isolate_namespace(mod)
59ec456 @tenderlove pass strings to the underscore method
tenderlove authored
385 engine_name(generate_railtie_name(mod.name))
db8a864 @drogus Add table_name_prefix to Engine's namespace automatically
drogus authored
386
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
387 self.routes.default_scope = { module: ActiveSupport::Inflector.underscore(mod.name) }
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
388 self.isolated = true
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
389
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
390 unless mod.respond_to?(:railtie_namespace)
391 name, railtie = engine_name, self
392
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
393 mod.singleton_class.instance_eval do
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
394 define_method(:railtie_namespace) { railtie }
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
395
f821055 @drogus Do not create table_name_prefix method on isolated module if the meth…
drogus authored
396 unless mod.respond_to?(:table_name_prefix)
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
397 define_method(:table_name_prefix) { "#{name}_" }
398 end
399
400 unless mod.respond_to?(:use_relative_model_naming?)
401 class_eval "def use_relative_model_naming?; true; end", __FILE__, __LINE__
402 end
403
404 unless mod.respond_to?(:railtie_helpers_paths)
405 define_method(:railtie_helpers_paths) { railtie.helpers_paths }
406 end
407
408 unless mod.respond_to?(:railtie_routes_url_helpers)
2bbcca0 @sgrif Deprecate `*_path` methods in mailers
sgrif authored
409 define_method(:railtie_routes_url_helpers) {|include_path_helpers = true| railtie.routes.url_helpers(include_path_helpers) }
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
410 end
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
411 end
74598fe @drogus Do not overwrite _railtie method on namespace while creating isolated…
drogus authored
412 end
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
413 end
051127d @drogus Rails::Engine.find(path) - method to find engine by path
drogus authored
414
3110c98 @prathamesh-sonpatki Added missing fullstop in engine API docs [ci skip]
prathamesh-sonpatki authored
415 # Finds engine with given path.
051127d @drogus Rails::Engine.find(path) - method to find engine by path
drogus authored
416 def find(path)
4001835 @tenderlove removing more pathnameisms
tenderlove authored
417 expanded_path = File.expand_path path
0470ddc @josevalim Remove unnecessary Railties structure now that plugins are gone
josevalim authored
418 Rails::Engine.subclasses.each do |klass|
419 engine = klass.instance
420 return engine if File.expand_path(engine.root) == expanded_path
421 end
422 nil
051127d @drogus Rails::Engine.find(path) - method to find engine by path
drogus authored
423 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
424 end
425
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
426 delegate :middleware, :root, :paths, to: :config
7a439d2 @goshakkk delegate to :class rather than 'self.class'
goshakkk authored
427 delegate :engine_name, :isolated?, to: :class
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
428
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
429 def initialize
430 @_all_autoload_paths = nil
431 @_all_load_paths = nil
432 @app = nil
433 @config = nil
434 @env_config = nil
435 @helpers = nil
436 @routes = nil
7317d9e @josh Remove implicit controller namespacing from new dsl
josh authored
437 super
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
438 end
71d18ce @oem Changed a few instances of of words in the API docs written in Britis…
oem authored
439
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
440 # Load console and invoke the registered hooks.
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
441 # Check <tt>Rails::Railtie.console</tt> for more info.
245dba0 @jhtwong Cherry-picking patch for https://github.com/rails/rails/issues/1460
jhtwong authored
442 def load_console(app=self)
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
443 require "rails/console/app"
444 require "rails/console/helpers"
445 run_console_blocks(app)
446 self
245dba0 @jhtwong Cherry-picking patch for https://github.com/rails/rails/issues/1460
jhtwong authored
447 end
71d18ce @oem Changed a few instances of of words in the API docs written in Britis…
oem authored
448
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
449 # Load Rails runner and invoke the registered hooks.
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
450 # Check <tt>Rails::Railtie.runner</tt> for more info.
f8e7904 @kennyj Add support runner hook.
kennyj authored
451 def load_runner(app=self)
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
452 run_runner_blocks(app)
453 self
f8e7904 @kennyj Add support runner hook.
kennyj authored
454 end
455
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
456 # Load Rake, railties tasks and invoke the registered hooks.
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
457 # Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
458 def load_tasks(app=self)
459 require "rake"
460 run_tasks_blocks(app)
461 self
462 end
71d18ce @oem Changed a few instances of of words in the API docs written in Britis…
oem authored
463
39b8b8f @prathamesh-sonpatki rails -> Rails [ci skip]
prathamesh-sonpatki authored
464 # Load Rails generators and invoke the registered hooks.
86f2061 @AvnerCohen Revert "Removing obsolete html tags in favor of markdown code block"
AvnerCohen authored
465 # Check <tt>Rails::Railtie.generators</tt> for more info.
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
466 def load_generators(app=self)
467 require "rails/generators"
468 run_generators_blocks(app)
469 Rails::Generators.configure!(app.config.generators)
470 self
471 end
472
473 # Eager load the application by loading all ruby
9d9b22f @pixeltrix Revert "Deprecate the `eager_load_paths` configuration"
pixeltrix authored
474 # files inside eager_load paths.
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
475 def eager_load!
9d9b22f @pixeltrix Revert "Deprecate the `eager_load_paths` configuration"
pixeltrix authored
476 config.eager_load_paths.each do |load_path|
ec31347 @mikepack Allow Pathnames to be added to eager load paths
mikepack authored
477 matcher = /\A#{Regexp.escape(load_path.to_s)}\/(.*)\.rb\Z/
351816f @josevalim Ensure that eager_load actually takes place just after the middleware…
josevalim authored
478 Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
479 require_dependency file.sub(matcher, '\1')
480 end
481 end
482 end
483
48d5a33 @timraymond Adding Back Rails::Engine::Railties#engines
timraymond authored
484 def railties
45aabe6 @spastorino There's no need to access Railties through self.class
spastorino authored
485 @railties ||= Railties.new
48d5a33 @timraymond Adding Back Rails::Engine::Railties#engines
timraymond authored
486 end
487
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
488 # Returns a module with all the helpers defined for the engine.
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
489 def helpers
490 @helpers ||= begin
491 helpers = Module.new
d4bea35 @drogus Make ActionController::Base.modules_for_helpers and ActionController:…
drogus authored
492 all = ActionController::Base.all_helpers_from_path(helpers_paths)
493 ActionController::Base.modules_for_helpers(all).each do |mod|
ce8efcf @robertomiranda Use public Module#include, in favor of https://bugs.ruby-lang.org/iss…
robertomiranda authored
494 helpers.include(mod)
e38b443 @drogus Add Engine#helpers method which loads all the engine's helpers
drogus authored
495 end
496 helpers
497 end
498 end
499
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
500 # Returns all registered helpers paths.
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
501 def helpers_paths
502 paths["app/helpers"].existent
503 end
504
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
505 # Returns the underlying rack application for this engine.
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
506 def app
092b148 @drogus Engine can now serve files with ActionDispatch::Static
drogus authored
507 @app ||= begin
508 config.middleware = config.middleware.merge_into(default_middleware_stack)
509 config.middleware.build(endpoint)
510 end
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
511 end
512
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
513 # Returns the endpoint for this engine. If none is registered,
514 # defaults to an ActionDispatch::Routing::RouteSet.
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
515 def endpoint
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
516 self.class.endpoint || routes
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
517 end
518
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
519 # Define the Rack API for this engine.
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
520 def call(env)
5b3bb61 @drogus Fix handling SCRIPT_NAME from within mounted engine's
drogus authored
521 env.merge!(env_config)
522 if env['SCRIPT_NAME']
37654d1 @tenderlove ask the routes objects for its Rack env key
tenderlove authored
523 env[routes.env_key] = env['SCRIPT_NAME'].dup
5b3bb61 @drogus Fix handling SCRIPT_NAME from within mounted engine's
drogus authored
524 end
525 app.call(env)
a132229 @drogus Added ability to set asset_path for engines
drogus authored
526 end
527
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
528 # Defines additional Rack env configuration that is added on each call.
a132229 @drogus Added ability to set asset_path for engines
drogus authored
529 def env_config
530 @env_config ||= {
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
531 'action_dispatch.routes' => routes
a132229 @drogus Added ability to set asset_path for engines
drogus authored
532 }
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
533 end
534
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
535 # Defines the routes for this engine. If a block is given to
536 # routes, it is appended to the engine.
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
537 def routes
798a14a @tenderlove pass a config to the route set
tenderlove authored
538 @routes ||= ActionDispatch::Routing::RouteSet.new_with_config(config)
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
539 @routes.append(&Proc.new) if block_given?
ec5d846 @drogus Properly reload routes defined in class definition
drogus authored
540 @routes
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
541 end
542
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
543 # Define the configuration object for the engine.
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
544 def config
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
545 @config ||= Engine::Configuration.new(self.class.find_root(self.class.called_from))
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
546 end
547
d475de7 @drogus Add helper for loading seed data for engine and application
drogus authored
548 # Load data from db/seeds.rb file. It can be used in to load engines'
549 # seeds, e.g.:
550 #
551 # Blog::Engine.load_seed
552 def load_seed
47be2f1 @wycats Fix the build
wycats authored
553 seed_file = paths["db/seeds.rb"].existent.first
e938247 @tenderlove seed file was already tested for existence. ಠ_ಠ
tenderlove authored
554 load(seed_file) if seed_file
d475de7 @drogus Add helper for loading seed data for engine and application
drogus authored
555 end
556
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
557 # Add configured load paths to ruby load paths and remove duplicates.
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
558 initializer :set_load_path, before: :bootstrap_hook do
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
559 _all_load_paths.reverse_each do |path|
4ae7936 @josevalim Got tests working once again.
josevalim authored
560 $LOAD_PATH.unshift(path) if File.directory?(path)
561 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
562 $LOAD_PATH.uniq!
563 end
564
565 # Set the paths from which Rails will automatically load source files,
566 # and the load_once paths.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
567 #
568 # This needs to be an initializer, since it needs to run once
3110c98 @prathamesh-sonpatki Added missing fullstop in engine API docs [ci skip]
prathamesh-sonpatki authored
569 # per engine and get the engine as a block parameter.
74d0906 @vipulnsward remove unused variables
vipulnsward authored
570 initializer :set_autoload_paths, before: :bootstrap_hook do
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
571 ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
572 ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
573
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
574 # 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
575 config.autoload_paths.freeze
9d9b22f @pixeltrix Revert "Deprecate the `eager_load_paths` configuration"
pixeltrix authored
576 config.eager_load_paths.freeze
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
577 config.autoload_once_paths.freeze
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
578 end
579
226d8e7 @josevalim Refactor MetalLoader and RoutesReloader to rely less on class configu…
josevalim authored
580 initializer :add_routing_paths do |app|
1faa00c @vipulnsward - Changed `paths` to `routing_paths` to avoid warning `possible refer…
vipulnsward authored
581 routing_paths = self.paths["config/routes.rb"].existent
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
582
1faa00c @vipulnsward - Changed `paths` to `routing_paths` to avoid warning `possible refer…
vipulnsward authored
583 if routes? || routing_paths.any?
584 app.routes_reloader.paths.unshift(*routing_paths)
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
585 app.routes_reloader.route_sets << routes
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
586 end
587 end
588
1177a40 @josevalim Fix i18n locales order test.
josevalim authored
589 # I18n load paths are a special case since the ones added
590 # later have higher priority.
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
591 initializer :add_locales do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
592 config.i18n.railties_load_path.concat(paths["config/locales"].existent)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
593 end
594
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
595 initializer :add_view_paths do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
596 views = paths["app/views"].existent
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
597 unless views.empty?
fbc9d0f @josevalim Simplify helpers handling. Ensure Metal can run AC hooks.
josevalim authored
598 ActiveSupport.on_load(:action_controller){ prepend_view_path(views) if respond_to?(:prepend_view_path) }
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
599 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
600 end
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
601 end
602
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
603 initializer :load_environment_config, before: :load_environment_hook, group: :all do
8bc1e53 @drogus Load all the env files available in config.paths (closes #6335)
drogus authored
604 paths["config/environments"].existent.each do |environment|
605 require environment
606 end
7cccfed @drogus Allow Engines loading its own environment file from config/environments
drogus authored
607 end
608
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
609 initializer :prepend_helpers_path do |app|
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
610 if !isolated? || (app == self)
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
611 app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
612 end
613 end
614
11aa167 @josevalim Revert "Provide a unique point for running initializers."
josevalim authored
615 initializer :load_config_initializers do
616 config.paths["config/initializers"].existent.sort.each do |initializer|
251923c @pftg Instrument each load config initializer
pftg authored
617 load_config_initializer(initializer)
11aa167 @josevalim Revert "Provide a unique point for running initializers."
josevalim authored
618 end
9af189a @drogus I've changed that test along the way, it should actually stay without…
drogus authored
619 end
620
b43b686 @drogus engines_blank_point should always be the last initializer in Engine
drogus authored
621 initializer :engines_blank_point do
622 # We need this initializer so all extra initializers added in engines are
623 # consistently executed after all the initializers above across all engines.
624 end
625
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
626 rake_tasks do
627 next if self.is_a?(Rails::Application)
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
628 next unless has_migrations?
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
629
630 namespace railtie_name do
631 namespace :install do
afc8288 @josevalim Remove the ability for engines to serve assets from the public direct…
josevalim authored
632 desc "Copy migrations from #{railtie_name} to application"
633 task :migrations do
634 ENV["FROM"] = railtie_name
0e49ef4 @Deradon Provided fix for calling rake tasks within mountable engines
Deradon authored
635 if Rake::Task.task_defined?("railties:install:migrations")
636 Rake::Task["railties:install:migrations"].invoke
637 else
638 Rake::Task["app:railties:install:migrations"].invoke
639 end
959f857 @drogus Added foo:install:assets task that copies assets from plugins public …
drogus authored
640 end
f07cbec @drogus Do not treat information about skipped migrations as WARNINGs but as …
drogus authored
641 end
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
642 end
643 end
644
81b7416 @wangjohn Removing Railtie::Configurable from the base Railtie object and making
wangjohn authored
645 def routes? #:nodoc:
646 @routes
647 end
648
8662722 @tenderlove initialize our instance variables
tenderlove authored
649 protected
1860d87 @josevalim Ensure asset_path defaults to nil for application
josevalim authored
650
251923c @pftg Instrument each load config initializer
pftg authored
651 def load_config_initializer(initializer)
652 ActiveSupport::Notifications.instrument('load_config_initializer.railties', initializer: initializer) do
653 load(initializer)
654 end
655 end
656
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
657 def run_tasks_blocks(*) #:nodoc:
658 super
659 paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
60281af @flippingbits Move load_generators from Rails::Application to Rails::Engine
flippingbits authored
660 end
661
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
662 def has_migrations? #:nodoc:
a7a58af @spohlenz Fix has_migrations? check in Rails::Engine
spohlenz authored
663 paths["db/migrate"].existent.any?
d84b2f3 @josevalim Fix tests.
josevalim authored
664 end
665
9dbcac7 @tenderlove add lib to $LOAD_PATH on application inhertence. fixes #17106
tenderlove authored
666 def self.find_root_with_flag(flag, root_path, default=nil) #:nodoc:
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
667
668 while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
669 parent = File.dirname(root_path)
670 root_path = parent != root_path && parent
671 end
672
673 root = File.exist?("#{root_path}/#{flag}") ? root_path : default
674 raise "Could not find root path for #{self}" unless root
675
66db0b9 @parndt Fixed backward incompatibility for engines.
parndt authored
676 Pathname.new File.realpath root
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
677 end
678
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
679 def default_middleware_stack #:nodoc:
3939d6b @drogus Removed ActionDispatch::Static, but left empty MiddlewareStack to uni…
drogus authored
680 ActionDispatch::MiddlewareStack.new
681 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
682
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
683 def _all_autoload_once_paths #:nodoc:
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
684 config.autoload_once_paths
685 end
686
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
687 def _all_autoload_paths #:nodoc:
9d9b22f @pixeltrix Revert "Deprecate the `eager_load_paths` configuration"
pixeltrix authored
688 @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
689 end
690
50b6110 @josevalim Remove unused responsibilities and add a few load definitions to engines
josevalim authored
691 def _all_load_paths #:nodoc:
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
692 @_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
693 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
694 end
6324eee @jeremy plugin rails/init.rb deprecation message
jeremy authored
695 end
Something went wrong with that request. Please try again.