7 steps to publishing code as plugins and gems

08 Mar 2008

There’s something I really like about Ryan Davis: he publishes code. Lots of women and men write code but he actually makes it available to the world. Publishing changes the way you look at code you write. The temptation to cut corners and ignore cleanliness disappears when you know somebody else is going to rifle through it.

Publishing code is like planning to throw a party in your bedroom: it’s the fastest way to get rid of dirty laundry.

Note: I work for an incredible company called adPickles that has encouraged its developers to release code for public benefit. We hope our pioneering pays off for the Ruby community.

So how do you publish code? Specifically, how do you publish Rails code? Well, if you are asking yourself this question and you like bulleted lists THIS IS YOUR LUCKY DAY.

Let someone else write it

Look around for existing plugins and gems. You don’t want to spend a weekend re-writing the gchart gem when you could have learned hang gliding or at least gone outside. # Write the code
Whether it’s rhtml (or haml) view code, funky helper methods, controller filters, model stuff - whatever. Just write it and get it working inside your app. It doesn’t have to be perfect but don’t let it get too ugly. If it’s poorly written your app is going to suck and you’ll spend all your time fixing bugs rather than moving on to the next step. # Extract the pieces that make sense together
Hopefully you started with putting the relevant code in their own methods. This is the time to grab all those methods together and put them into a module. Maybe even a module with different submodules that get inserted into different classes. Put this extracted code under a lib/ directory somewhere and require it with some low-level application file (in Rails I recommend config/initializers/application.rb) # Try to abandon your code for a simpler alternative
Seriously, if this code isn’t absolutely necessary then now is a great time to delete it and make your app that much simpler. Writing code is good, deleting code is better. # Make a plugin!
This part is very fun. Start with: script/generate plugin my_awesome_code and move your code into the vendor/plugins/my_awesome_code/lib/ directory. Now that this code lives outside your app you’re going to need to give it test cases of it’s own. This can be hard or it can be dead simple. # Convert it to a gem
I just realized last week how easy this is. Install hoe and type ‘sow my_awesome_code’. Follow the instructions that get printed out and then just copy your plugin code to the new gem folder. Your code can function as both a gem and a plugin at the same time. A gem with an init.rb file _is a plugin_. # Tell the world
Make sure your code is in svn or git somewhere. Post on your blog. Send out an email. Mention it on Twitter. Whatever. As long as you’ve got test coverage and the tests pass there’s no reason to be ashamed. Even if your code just spits out obscure Russian idioms it’s worth being very proud of.

This is the part where everybody sends you tons of money, people of your preferred gender battle for your attention, and your community thanks you. But the real payoff? The real payoff is that your app just got smaller. And that’s worth all the effort.

  • Eric said: Good article! One more possibility for the 7th point: 7)...or you could even shamelessly squat some other blog for your own promotion! More seriously though, how would you publish a stand-alone rails app? I suppose that a plugin would not make much sense, so that I could try to build a gem out of it. I'll give it a try!
  • Jack Danger said: Blog-squatting is an excellent plan - I highly encourage it. And for publishing a whole app: http://svn.6brand.com/admiteer is one example. The next time you throw some code out into the cybertubes please throw a comment up here :-)

Please if you found this post helpful or have questions.