27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Brand-new engine<br />

engine.rb. This folder works in the same way that it does in an application, providing a<br />

home for the images, JavaScript files, and stylesheets that are served by the sprockets<br />

gem. Providing that either the host application or the engine 13 specifies a dependency<br />

on CoffeeScript or Sass, you can use these as well.<br />

Inside the app directory lies the app/controllers directory, which serves the same<br />

purpose as the app/controllers directory in a Rails application. This directory has a<br />

key difference though: the controllers should be placed into a forem namespace so<br />

that they do not clash with identically named controllers in the application. If you<br />

moved these controllers out of the namespace, they’d clash with controllers of the<br />

same name in the application, or in other engines that aren’t using namespacing. By<br />

namespacing them, you prevent this error from happening. This also explains why the<br />

helpers in app/helpers are also separate.<br />

Your Forem::ApplicationController currently inherits from ActionController<br />

::Base, but in the case of your engine you’ll want it to inherit from Application-<br />

Controller instead. Therefore, you’ll change app/controllers/forem/application<br />

_controller.rb from this<br />

module Forem<br />

class ApplicationController<br />

into this:<br />

module Forem<br />

class ApplicationController<br />

You must use the :: prefix on the super ApplicationController so that it goes to the<br />

top-level ApplicationController, not the one inside the Forem model that you’re<br />

defining! By inheriting from the ApplicationController, your engine will use the<br />

same layout as the application it’s hosted within.<br />

Within the app directory, you can define models which also go under a<br />

namespace. If you had a Forum model, it would live at app/models/forem/forum.rb<br />

and would be called Forem::Forum. By doing this, you separate the model class from<br />

any identically named class in the application or other engines. The default table<br />

name for this model would be forums if it weren’t for some additional configuration<br />

in lib/forem/engine.rb that you’ll see later.<br />

With models also come migrations. When you create migrations in an engine,<br />

these are stored (again, like an application) in db/migrate. When you (or others)<br />

install the engine into an application, there’s a rake forem:install:migrations task<br />

that will copy across these migrations, adding the migrations to the current list of<br />

migrations in the application’s db/migrate folder. If a new version of the engine is<br />

released, the user can re-run rake forem:install:migrations and it will only copy<br />

across the newer migrations.<br />

13 It’s best to specify this dependency in the engine if you wish to use either of these.<br />

473

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

Saved successfully!

Ooh no, something went wrong!