views:

454

answers:

3

As a Rails developer I feel a bit stupid asking this question but hopefully I will learn something new and someone can put me out of my misery! In my rails applications I use (other peoples) gems all the time, I also use plugins from the community or my own.

I understand the benefits of use gems over plugins as they are version-able, segmented, system wide, easier to manage and share etc etc but I don't really know how to go about making a gem for my rails apps!?

Do you always start with a plugin and convert it to a gem, I've seen the words 'package it as Gem'. Also the gem I'm thinking of building would be no good in a normal ruby program, it's only useful to rails apps. I'm not even sure if the semantics of that make sense, 'RubyGem' that will only work in a rails application!?

I would like to create a gem (if that's what I should use?) for a discreet piece of functionality for my rails apps. It will need to add a database migration, new routes and provide controllers and views or useful view helpers. I'm know I can achieve this via a plug-in but would just like to know how/why to do it as a 'Ruby Gem'?

+1  A: 

To avoid the risk of Over-engineering, I usually start with the feature I need directly into the application. Then, as soon as I need to use the same feature into an other project, I check whether it is worth to extract it into a plugin or even a separate application providing a set of API.

Plugins and Gems are often interchangeable. Gems provides several significant advantages in terms of reusability and maintainability. On the other side, there are some specific known issue. For instance, a Rails app actually can't load rake tasks defined into a plugin packaged as a Gem.

Almost every Rails plugin can be packaged as a Gem. For instance, take my tabs_on_rails plugin.

You can install it as a Gem specifying the dependency on environment.rb. Or you can use script/plugin install command as you would expect.

If you want to achieve the same result, make sure to follow the standard Gem layout and provide the init.rb initialization script required by Rails. Also, you might want to create an install.rb and uninstall.rb file to include the post-install and post-uninstall hooks when the plugin is installed as a standard Rails plugin.

Last but not least, if you package a plugin as Gem you can reuse it in non-Rails projects and provide Rails-specific initializations using the init.rb file. Non-Rails applications will simply ignore it.

Simone Carletti
Can you fix the url to your tabs_on_rails plugin?
Jeff Paquette
Fixed! Thank you.
Simone Carletti
+1  A: 

If you want to make a plugin for Rails, https://peepcode.com/products/rails-2-plugin-patterns gives you a good start. After that, make the plugin into a gem.

To make a gem, this resource http://railscasts.com/episodes/183-gemcutter-jeweler will be helpful.

TK
Thanks for your comment, I've read that plugin book when I first wrote a plugin which was very helpful. "Make a plugin into a gem" seems to be the answer.
tsdbrown
A: 

Take a look at Jeweler. Jeweler gives you a set of rake tasks to make gem versioning and building very easy.

Ryan McGeary