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.

472 CHAPTER 17 Engines<br />

With that safety net in place, let’s go through the major parts of what this has<br />

generated.<br />

17.3.2 The layout of an engine<br />

You’ve now got the basic scaffold of your engine in the same directory of your application,<br />

and you can go back to that directory by using cd ../forem from within the<br />

application. Let’s go through the important parts of this engine.<br />

FOREM.GEMSPEC<br />

Each plugin that is generated using the new Rails plugin generator now comes with a<br />

gemspec, which allows it to be used as a gem. 12 This file allows you to specify information<br />

for your gem such as its name, a helpful description, and most important the<br />

other gems it depends on as either runtime dependencies using add_dependency, or<br />

as development dependencies using add_development_dependency. You can specify<br />

this information by lines like this in the Gem::Specification definition in this file:<br />

s.add_dependency 'rails'<br />

s.add_development_dependency 'rspec-rails'<br />

GEMFILE<br />

This file contains the rails and sqlite3 gems, which provide the basis for your application.<br />

When, however, people install your engine using gem install forem, they’ll<br />

not receive these dependencies. To fix this, you need to place them.<br />

You need to tell your Gemfile to reference the forem.gemspec file too, as you specify<br />

gem dependencies in this rather than the Gemfile for engines. The Gemfile is not<br />

referenced at all when you install a gem, but the forem.gemspec file is. Therefore, you<br />

must put all dependencies in the forem.gemspec file and tell your Gemfile to reference<br />

it for all its dependencies. To do this, change the Gemfile to be this:<br />

source :rubygems<br />

gemspec<br />

And add these lines to your forem.gemspec inside the Gem::Specification block:<br />

s.add_dependency "rails", "3.1.0"<br />

s.add_development_dependency "sqlite3"<br />

When you run bundle install, it will install the Rails version specified in your Gemfile<br />

and any gem dependencies declared in forem.gemspec.<br />

APP<br />

This folder serves the same purpose as an application: to house the assets, controllers,<br />

helpers, models, views, mailers, observers, and whatever else is particular to your<br />

application.<br />

Rails is automatically told about the app/assets directory contained within an<br />

engine, based on the class definition within a file you’ll see a little later on, lib/forem/<br />

12 A great guide to developing a gem can be found here: http://github.com/radar/guides/blob/master/gem-<br />

development.md.

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

Saved successfully!

Ooh no, something went wrong!