Skip to content
This repository
Browse code

[engines guide] WIP for explaining how to hook engine into application

  • Loading branch information...
commit 6df79bf580cf9a0464fce948c212bd117c378cc9 1 parent aae77d2
Ryan Bigg authored October 14, 2011

Showing 1 changed file with 46 additions and 0 deletions. Show diff stats Hide diff stats

  1. 46  railties/guides/source/engines.textile
46  railties/guides/source/engines.textile
Source Rendered
@@ -204,6 +204,14 @@ If you'd rather play around in the console, +rails console+ will also work just
204 204
   => #<Blorgh::Post id: 1 ...>
205 205
 </ruby>
206 206
 
  207
+One final thing is that the +posts+ resource for this engine should be the root of the engine. Whenever someone goes to the root path where the engine is mounted, they should be shown a list of posts. This can be made to happen if this line is inserted into the +config/routes.rb+ file inside the engine:
  208
+
  209
+<ruby>
  210
+root :to => "posts#index"
  211
+</ruby>
  212
+
  213
+Now people will only need to go to the root of the engine to see all the posts, rather than visiting +/posts+.
  214
+
207 215
 h4. Generating a comments resource
208 216
 
209 217
 Now that the engine has the ability to create new blog posts, it only makes sense to add commenting functionality as well. To do get this, you'll need to generate a comment model, a comment controller and then modify the posts scaffold to display comments and allow people to create new ones.
@@ -337,11 +345,49 @@ That completes the comment function of the blogging engine. Now it's time to use
337 345
 
338 346
 h3. Hooking into application
339 347
 
  348
+Using an engine within an application is very easy. First, the engine needs to be specified inside the application's +Gemfile+. If there isn't an application handy to test this out in, generate one using the +rails new+ command outside of the engine directory like this:
  349
+
  350
+<shell>
  351
+$ rails new unicorn
  352
+</shell>
  353
+
  354
+Usually, specifying the engine inside the Gemfile would be done by specifying it as a normal, everyday gem.
  355
+
  356
+<ruby>
  357
+gem 'devise'
  358
+</ruby>
  359
+
  360
+Because the +blorgh+ engine is still under development, it will need to have a +:path+ option for its +Gemfile+ specification:
  361
+
  362
+<ruby>
  363
+gem 'blorgh', :path => "/path/to/blorgh"
  364
+</ruby>
  365
+
  366
+If the whole +blorgh+ engine directory is copied to +vendor/engines/blorgh+ then it could be specified in the +Gemfile+ like this:
  367
+
  368
+<ruby>
  369
+gem 'blorgh', :path => "vendor/engines/blorgh"
  370
+</ruby>
  371
+
  372
+As described earlier, by placing the gem in the +Gemfile+ it will be loaded when Rails is loaded, as it will first require +lib/blorgh.rb+ in the engine and then +lib/blorgh/engine.rb+, which is the file that defines the major pieces of functionality for the engine.
  373
+
  374
+To make the engine's functionality accessible from within an application, it needs to be mounted in that application's +config/routes.rb+ file:
  375
+
  376
+<ruby>
  377
+  mount Blorgh::Engine, :at => "blog"
  378
+</ruby>
  379
+
  380
+NOTE: Other engines, such as Devise, handle this a little differently by making you specify custom helpers such as +devise_for+ in the routes. These helpers do exactly the same thing, mounting pieces of the engines's functionality at a pre-defined path which may be customizable.
  381
+
  382
+
  383
+
  384
+This line will mount the engine
340 385
 TODO: Application will provide a User foundation class which the engine hooks into through a configuration setting, configurable in the application's initializers. The engine will be mounted at the +/blog+ path in the application.
341 386
 
342 387
 h3. Overriding engine functionality
343 388
 
344 389
 TODO: Cover how to override engine functionality in the engine, such as controllers and views.
  390
+
345 391
 IDEA: I like Devise's +devise :controllers => { "sessions" => "sessions" }+ idea. Perhaps we could incorporate that into the guide?
346 392
 TODO: Mention how to use assets within an engine?
347 393
 TODO: Mention how to depend on external gems, like RedCarpet.

0 notes on commit 6df79bf

Please sign in to comment.
Something went wrong with that request. Please try again.