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.

17.3 Brand-new engine<br />

Brand-new engine<br />

The layout of an engine is nearly identical to that of<br />

a Rails application, with the notable exception that<br />

all code that usually goes straight into the app directory<br />

now goes into a namespace. Let’s take a look at<br />

the layout of the forem engine 10 at an early stage of<br />

its development, shown in figure 17.1.<br />

You’re going duplicate a little chunk of this code<br />

using the same practices that were used to develop<br />

forem. It’s a good an example as any. 11<br />

17.3.1 Creating an engine<br />

Here you’ll create your engine using a generator<br />

built-in to Rails.<br />

WARNING You’re going to need to use<br />

at least the Rails 3.1 version for this,<br />

which is what you should have installed<br />

from earlier chapters in this book.<br />

You can run this executable file to generate the layout<br />

of your engine using this command at the root<br />

of the Ticketee application you created earlier:<br />

cd ..<br />

rails plugin new forem --mountable<br />

The --mountable option here is the magic incantation:<br />

it tells the plugin generator that you want to<br />

generate a mountable plugin, more commonly known as an engine. The output of this<br />

command is very similar to that of an application, containing an app directory and a<br />

config/routes.rb file. But that’s what an engine is essentially: a miniature application!<br />

This command even runs bundle install automatically for you on this new engine,<br />

like the rails new command does when you generate a new application.<br />

Before you go any further, run bundle --binstubs in this new directory so that<br />

you can use bin/rspec to run your tests, rather than bin/rspec.<br />

Before you get too involved here, you’re going to set this project up as a Git repository<br />

and create a base commit that you can revert back to if anything goes wrong.<br />

You’ll first need to change back into the forem directory, then set up the git repository:<br />

git init<br />

git add .<br />

git commit -m "Initial base for the forem engine"<br />

10 http://github.com/radar/forem.<br />

11 It also helps that one of the authors of this book has done extensive work on it.<br />

Figure 17.1 The forem engine,<br />

directory structure<br />

471

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

Saved successfully!

Ooh no, something went wrong!