27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

470 CHAPTER 17 Engines<br />

copy over assets from an engine into a Rails application any more either; they are<br />

served through the functionality of the Sprockets gem.<br />

Finally, this ancient engine implementation didn’t enforce namespacing of the<br />

controllers or models. This could potentially lead to conflicts between engines and<br />

the application code, where the application code would override an engine’s code. If<br />

the application has a model called Forum at app/models/forum.rb, and the engine has<br />

the same model at the same location (relative to its root), the application’s model will<br />

take precedence. Namespacing is something that you’ll see has almost a zealot level of<br />

impact in the work that is done with engines today. It’s absolutely important to keep<br />

the application and engine’s code separate so that they do not conflict.<br />

So today, we’ve now got engines as a core part of Rails 3.1, and they’re better, and<br />

they’re here to stay. Let’s see why they’re useful.<br />

17.2 Why engines are useful<br />

Engines allow Rails programmers to share common code between applications in an<br />

extremely easy fashion. It’s entirely possible to use more than one engine in an application,<br />

and many people do. Engines are generally provided as gems, and so they are<br />

managed like every other gem your application uses: by using Bundler.<br />

In previous (before 3.0) versions of Rails, we have seen that people had to use the<br />

engines plugin. 9 This was sometimes problematic, because whenever a new Rails version<br />

was released it could potentially break the compatibility of the plugin. By having<br />

this feature within Rails itself, this issue is fixed.<br />

Alternatively, people could use generators. These would often generate controllers,<br />

models, and views in the application itself, which would allow people to change<br />

the code exceptionally easily. When new versions of these generators were released<br />

with changes to the previously generated code, however, there was no clean way to<br />

keep the changes.<br />

One final, very hacky way, would be to copy over the controllers, models, or views<br />

into the application manually from a directory, which runs into the same problems as<br />

described, as well as making it difficult to know if you got it right or not.<br />

With an engine, all the code is kept separate from the application and must be<br />

explicitly overridden in the application if that’s what is needed. When a new version of<br />

an engine is released, it will only alter the code of the engine and not the application,<br />

making the upgrade process as easy as changing a version number in a Gemfile.<br />

Even the routes for an engine are kept separate, being placed in the engine’s<br />

config/routes.rb file rather than the application’s. This allows you to namespace the<br />

engine’s routes so that they don’t conflict with the application.<br />

The whole point of engines is to separate out chunks of functionality, and to be<br />

able to share it without it crashing into the code that already exists.<br />

Let’s generate your own engine and have a look at its parts.<br />

9 http://github.com/lazyatom/engines.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!