Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 587 lines (539 sloc) 20.495 kB
b17e358 @josevalim Move configuration to subfolders.
josevalim authored
1 require 'rails/railtie'
812136a @jeremy Fix unstated usage of Pathname
jeremy authored
2 require 'active_support/core_ext/module/delegation'
3 require 'pathname'
53c13f1 @anildigital Use Config::CONFIG['host_os'] instead of RUBY_PLATFORM [#4477 state:r…
anildigital authored
4 require 'rbconfig'
c787bfd @drogus Engine can now load its own plugins
drogus authored
5 require 'rails/engine/railties'
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
6
7 module Rails
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 an…
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 an…
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 case…
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 in…
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 in…
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 o…
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 in…
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 in…
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 in…
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 in…
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 in…
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 in…
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 in…
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 o…
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 in…
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 in…
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 in…
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 in…
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 in…
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 di…
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 …
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 b…
josevalim authored
336 unless base.abstract_railtie?
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
337 base.called_from = begin
a5684df @josevalim Ensure config.after_initializer is executed before building the middl…
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 di…
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 di…
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 uni…
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 incl…
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 …
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…
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 meth…
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…
drogus authored
370 end
371 end
372 end
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
373 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
374 end
375
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
376 delegate :middleware, :root, :paths, :to => :config
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
377 delegate :engine_name, :isolated?, :to => "self.class"
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
378
379 def load_tasks
7317d9e @josh Remove implicit controller namespacing from new dsl
josh authored
380 super
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
381 paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
2b75b94 @josevalim Plugin is now an Engine.
josevalim authored
382 end
383
351816f @josevalim Ensure that eager_load actually takes place just after the middleware…
josevalim authored
384 def eager_load!
385 config.eager_load_paths.each do |load_path|
386 matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
387 Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
388 require_dependency file.sub(matcher, '\1')
389 end
390 end
391 end
392
c787bfd @drogus Engine can now load its own plugins
drogus authored
393 def railties
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
394 @railties ||= self.class::Railties.new(config)
c787bfd @drogus Engine can now load its own plugins
drogus authored
395 end
396
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
397 def app
092b148 @drogus Engine can now serve files with ActionDispatch::Static
drogus authored
398 @app ||= begin
399 config.middleware = config.middleware.merge_into(default_middleware_stack)
400 config.middleware.build(endpoint)
401 end
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
402 end
403
404 def endpoint
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
405 self.class.endpoint || routes
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
406 end
407
408 def call(env)
a132229 @drogus Added ability to set asset_path for engines
drogus authored
409 app.call(env.merge!(env_config))
410 end
411
412 def env_config
413 @env_config ||= {
414 'action_dispatch.routes' => routes,
415 'action_dispatch.asset_path' => config.asset_path
416 }
ad6be08 @drogus Made Engine valid rack app with its own middleware stack
drogus authored
417 end
418
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
419 def routes
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
420 @routes ||= ActionDispatch::Routing::RouteSet.new
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
421 @routes.append(&Proc.new) if block_given?
ec5d846 @drogus Properly reload routes defined in class definition
drogus authored
422 @routes
c989d1a @drogus Engine sets routes as default rack endpoint if no endpoint was given
drogus authored
423 end
424
675f3ea @drogus Gather initializers from railties in engines to get rid of additional…
drogus authored
425 def initializers
426 initializers = []
427 railties.all { |r| initializers += r.initializers }
428 initializers += super
429 initializers
430 end
431
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
432 def config
937f419 @drogus Engine's assets are now served with ActionDispatch::Static
drogus authored
433 @config ||= Engine::Configuration.new(find_root_with_flag("lib"))
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
434 end
435
d475de7 @drogus Add helper for loading seed data for engine and application
drogus authored
436 # Load data from db/seeds.rb file. It can be used in to load engines'
437 # seeds, e.g.:
438 #
439 # Blog::Engine.load_seed
440 def load_seed
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
441 seed_file = paths["db/seeds"].existent.first
d475de7 @drogus Add helper for loading seed data for engine and application
drogus authored
442 load(seed_file) if File.exist?(seed_file)
443 end
444
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
445 # Add configured load paths to ruby load paths and remove duplicates.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
446 initializer :set_load_path, :before => :bootstrap_hook do
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
447 _all_load_paths.reverse_each do |path|
4ae7936 @josevalim Got tests working once again.
josevalim authored
448 $LOAD_PATH.unshift(path) if File.directory?(path)
449 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
450 $LOAD_PATH.uniq!
451 end
452
453 # Set the paths from which Rails will automatically load source files,
454 # and the load_once paths.
9cfeefb @wycats Reorganized initializers a bit to enable better hooks for common case…
wycats authored
455 #
456 # This needs to be an initializer, since it needs to run once
457 # per engine and get the engine as a block parameter
458 initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
459 ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
460 ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
461
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
462 # 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
463 config.autoload_paths.freeze
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
464 config.eager_load_paths.freeze
6f83a50 @fxn renames load_(once_)paths to autoload_(once_)paths in dependencies an…
fxn authored
465 config.autoload_once_paths.freeze
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
466 end
467
226d8e7 @josevalim Refactor MetalLoader and RoutesReloader to rely less on class configu…
josevalim authored
468 initializer :add_routing_paths do |app|
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
469 paths = self.paths["config/routes"].existent
470
471 if routes? || paths.any?
472 app.routes_reloader.paths.unshift(*paths)
08f4713 @drogus Refactored routes reloading to use RouteSet#append instead keeping bl…
drogus authored
473 app.routes_reloader.route_sets << routes
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
474 end
475 end
476
1177a40 @josevalim Fix i18n locales order test.
josevalim authored
477 # I18n load paths are a special case since the ones added
478 # later have higher priority.
98240c4 @josevalim Get rid of initializers global and create i18n railtie.
josevalim authored
479 initializer :add_locales do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
480 config.i18n.railties_load_path.concat(paths["config/locales"].existent)
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
481 end
482
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
483 initializer :add_view_paths do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
484 views = paths["app/views"].existent
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
485 unless views.empty?
486 ActiveSupport.on_load(:action_controller){ prepend_view_path(views) }
487 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
488 end
02c5137 @josevalim Add view paths to Engine setup.
josevalim authored
489 end
490
7cccfed @drogus Allow Engines loading its own environment file from config/environments
drogus authored
491 initializer :load_environment_config, :before => :load_environment_hook do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
492 environment = paths["config/environments"].existent.first
7cccfed @drogus Allow Engines loading its own environment file from config/environments
drogus authored
493 require environment if environment
494 end
495
a5e509d @drogus We should avoid creating additional initializers when we can, adding …
drogus authored
496 initializer :append_asset_paths do
11558a1 @drogus Use railtie_name to correctly get name from plugins
drogus authored
497 config.asset_path ||= "/#{railtie_name}%s"
8284fd3 @drogus Get rid of static_paths method and instead configure paths for Action…
drogus authored
498
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
499 public_path = paths["public"].first
8284fd3 @drogus Get rid of static_paths method and instead configure paths for Action…
drogus authored
500 if config.compiled_asset_path && File.exist?(public_path)
501 config.static_asset_paths[config.compiled_asset_path] = public_path
502 end
503 end
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
504
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
505 initializer :prepend_helpers_path do |app|
5d5eb2b @drogus Rename namespace method to isolate_namespace.
drogus authored
506 if !isolated? || (app == self)
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
507 app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
e35c204 @drogus Include all helpers from non-namespaced engines
drogus authored
508 end
509 end
510
9af189a @drogus I've changed that test along the way, it should actually stay without…
drogus authored
511 initializer :load_config_initializers do
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
512 config.paths["config/initializers"].existent.sort.each do |initializer|
9af189a @drogus I've changed that test along the way, it should actually stay without…
drogus authored
513 load(initializer)
514 end
515 end
516
b43b686 @drogus engines_blank_point should always be the last initializer in Engine
drogus authored
517 initializer :engines_blank_point do
518 # We need this initializer so all extra initializers added in engines are
519 # consistently executed after all the initializers above across all engines.
520 end
521
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
522 rake_tasks do
523 next if self.is_a?(Rails::Application)
524
525 namespace railtie_name do
43215de @drogus Add task foo:install (where foo is plugin) as a shortcutinstall:migra…
drogus authored
526 desc "Shortcut for running both rake #{railtie_name}:install:migrations and #{railtie_name}:install:assets"
527 task :install do
528 Rake::Task["#{railtie_name}:install:migrations"].invoke
529 Rake::Task["#{railtie_name}:install:assets"].invoke
530 end
531
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
532 namespace :install do
a8b1780 @josevalim Updated DOCS for engines and added a couple TODOs. Also, commented in…
josevalim authored
533 # TODO Add assets copying to this list
534 # 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
535 desc "Copy migrations from #{railtie_name} to application"
536 task :migrations do
537 ENV["FROM"] = railtie_name
538 Rake::Task["railties:install:migrations"].invoke
539 end
f07cbec @drogus Do not treat information about skipped migrations as WARNINGs but as …
drogus authored
540
959f857 @drogus Added foo:install:assets task that copies assets from plugins public …
drogus authored
541 desc "Copy assets from #{railtie_name} to application"
542 task :assets do
543 ENV["FROM"] = railtie_name
544 Rake::Task["railties:install:assets"].invoke
545 end
f07cbec @drogus Do not treat information about skipped migrations as WARNINGs but as …
drogus authored
546 end
a9bf91e @drogus Add 'foo:install:migrations' task to copy migrations from foo engine
drogus authored
547 end
548 end
549
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
550 protected
d649bf1 @josevalim Provide a cleaner syntax for paths configuration that does not rely o…
josevalim authored
551 def routes?
552 defined?(@routes)
553 end
554
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
555 def find_root_with_flag(flag, default=nil)
556 root_path = self.class.called_from
557
558 while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
559 parent = File.dirname(root_path)
560 root_path = parent != root_path && parent
561 end
562
563 root = File.exist?("#{root_path}/#{flag}") ? root_path : default
564 raise "Could not find root path for #{self}" unless root
565
4a4ff14 @josevalim Use RbConfig instead of Config for 1.9.3 compatibility.
josevalim authored
566 RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ?
32a5b49 @drogus Move singleton pattern to Railtie and remove Engine::Configurable and…
drogus authored
567 Pathname.new(root).expand_path : Pathname.new(root).realpath
568 end
569
3939d6b @drogus Removed ActionDispatch::Static, but left empty MiddlewareStack to uni…
drogus authored
570 def default_middleware_stack
571 ActionDispatch::MiddlewareStack.new
572 end
788fce2 @josevalim Create configurable modules and ensure that they are added only on di…
josevalim authored
573
6f194bb @josevalim Small tidy up on Rails::Engine.
josevalim authored
574 def _all_autoload_once_paths
575 config.autoload_once_paths
576 end
577
9b19a6f @josevalim A few changes were done in this commit:
josevalim authored
578 def _all_autoload_paths
579 @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
580 end
581
582 def _all_load_paths
583 @_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
584 end
7fcf859 @josevalim Massive cleanup in Railties and load stack.
josevalim authored
585 end
6324eee @jeremy plugin rails/init.rb deprecation message
jeremy authored
586 end
Something went wrong with that request. Please try again.