Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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