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.

512 CHAPTER 17 Engines<br />

17.8 Releasing as a gem<br />

By releasing your engine as a gem, you’ll make it publicly available for anybody to<br />

download from http://rubygems.org. This will also allow you to share this engine<br />

across multiple applications. If people wish to use your engine, they would be able to<br />

put this line in their Gemfile of their application:<br />

gem 'forem'<br />

Then they would need to go about configuring the application to have a config/initializers/<br />

forem.rb file with the proper configuration options (only Forem::Engine.user_class for<br />

now, but possibly others). You’d usually put these steps in a README file at the root of<br />

the engine. 22<br />

To release your engine as a gem, you can use Bundler. Bundler provides some<br />

gem-management tasks in the form of Rake tasks that allow you to build your gem<br />

(basically, zip it up into a .gem file), 23 install the gem locally, and release it.<br />

To make these tasks available for your engine, you can put this line at the bottom<br />

of your Rakefile:<br />

Bundler::GemHelper.install_tasks<br />

When you run rake -T, you’ll see that you’ve got a few tasks for your engine, some of<br />

which you’ve already used. This line that you’ve put in the Rakefile will add the build,<br />

install, and release rake tasks.<br />

Before you can release your gem, you must give it a unique name. To do this, you’ll<br />

go into your application and rename forem.gemspec to your-name-forem.gemspec 24 to<br />

make it unique. Then inside this file, you’ll need to change this line<br />

s.name = "forem"<br />

to have the same name as the gemspec:<br />

s.name = "your-name-forem"<br />

Now you’re ready to release your gem. Let’s run the rake build task first and see what<br />

does:<br />

(in /Users/ryanbigg/Sites/book/forem)<br />

your-name-forem 0.0.1 built to pkg/your-name-forem-0.0.1.gem<br />

This command has built your gem and put it in a new directory called pkg. This new<br />

gem can be installed using gem install pkg/your-name-forem-0.0.1 if you wish, but<br />

there’s a problem: this gem doesn’t contain all your files.<br />

At the moment, the gem only contains the files specified by this line in your-nameforem.gemspec:<br />

22 This is so when you put it in GitHub, people will have a clear way of seeing what this engine’s good for and<br />

how they can use it.<br />

23 Yup, .gem files are actually .zip files.<br />

24 A wise decision here would be to use your GitHub username, as that’s bound to be unique, compared to your<br />

first name which is most likely not.

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

Saved successfully!

Ooh no, something went wrong!