<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - How to Gemify your Rails Plugins Changes</title>
  <id>tag:opensoul.org,2009:/2009/10/5/how-to-gemify-your-rails-plugins/changes</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://opensoul.org/2009/10/5/how-to-gemify-your-rails-plugins/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2009/10/5/how-to-gemify-your-rails-plugins" rel="alternate" type="text/html"/>
  <updated>2009-10-05T17:33:03Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2009-10-05:549:1</id>
    <updated>2009-10-05T17:33:03Z</updated>
    <link href="http://opensoul.org/2009/11/9/how-to-gemify-your-rails-plugins" rel="alternate" type="text/html"/>
    <title>How to Gemify your Rails Plugins</title>
<content type="html">&lt;p&gt;Ever since &lt;a href=&quot;http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies&quot;&gt;Rails added support for declaring gem dependencies&lt;/a&gt;, there is really no (good) reason to use plain ol&#8217; plugins. We&#8217;ve been slowly gemifying &lt;a href=&quot;http://github.com/collectiveidea&quot;&gt;all of our plugins&lt;/a&gt;. There&#8217;s a few hoops you have to jump through to get Rake tasks and Capistrano recipes working.&lt;/p&gt;


	&lt;p&gt;First, you need something that will help you generate the gemspec and build the gem. You can do this by hand, but there&#8217;s several great plugins out there that make it easy. We recommend &lt;a href=&quot;http://github.com/technicalpickles/jeweler&quot;&gt;Jeweler&lt;/a&gt;. Follow the directions in the Jeweler &lt;span class=&quot;caps&quot;&gt;README&lt;/span&gt; for &#8220;Using in an existing project&#8221;.&lt;/p&gt;


	&lt;h3&gt;Initializing&lt;/h3&gt;


	&lt;p&gt;Rails plugins have the magical &lt;code&gt;init.rb&lt;/code&gt; that gets loaded when the plugin is initialized. To make this work in a gem, all you have to do is move it to &lt;code&gt;rails/init.rb&lt;/code&gt;. Recent versions of Rails will look there whether you install it as a plugin or a gem, so you can just move it to the new location if you don&#8217;t care about ancient versions of Rails.&lt;/p&gt;


	&lt;h3&gt;Rake Tasks&lt;/h3&gt;


	&lt;p&gt;Rails will load &lt;code&gt;tasks/*.rake&lt;/code&gt; defined in any plugins. Unfortunately, these don&#8217;t get loaded from Gems.  To make your rake tasks work from a plugin, you will need to move them into the lib directory, and explicitly require them from your app&#8217;s &lt;code&gt;Rakefile&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require 'mygem/tasks'&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you want your tasks to still be available when your code is installed as a plugin, you can just explicitly require the task from &lt;code&gt;task/mygem.rake&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'mygem', 'tasks'))&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Capistrano Tasks&lt;/h3&gt;


	&lt;p&gt;Capistrano recipes defined at &lt;code&gt;recipes/*.rb&lt;/code&gt; in your plugin are also automatically loaded by Rails. Unfortunately, they suffer the same fate as Rake tasks and have to be move to the lib directory and be explicitly required from &lt;code&gt;config/deploy.rb&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;When moving the recipes to the lib directory, we have to jump through a hoop to get Capistrano to load it properly.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Capistrano::Configuration.instance.load do
  # put cap recipes here
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And in your &lt;code&gt;config/deploy.rb&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require 'mygem/recipes'&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;As with Rake tasks, if you want your recipes to still work when installed as a plugin, add the following to a file in the &lt;code&gt;recipes/&lt;/code&gt; directory of your plugin:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'mygem', 'recipes'))&lt;/code&gt;&lt;/pre&gt;</content>  </entry>
</feed>
