Building a ruby gem for Rails applications

不想你离开。 提交于 2019-11-29 23:09:04
Simone Carletti

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 another project, I check whether it is worth to extract it into a plugin or even a separate application providing an 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.

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.

As of 2013 you'll want to use Bundler and the following tutorials:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!