为Rails应用程序构建红宝石 [英] Building a ruby gem for Rails applications

查看:87
本文介绍了为Rails应用程序构建红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一名Rails开发人员,我觉得这个问题有点愚蠢,但希望我能学到新的东西,并且有人能够让我摆脱不幸。在我的Rails应用程序中,我始终使用(其他人)宝石,我也使用社区或我自己的插件。



我了解使用gems而不是插件的好处因为它们是可版本化的,分割的,系统范围的,更容易管理和共享等等,但我真的不知道如何去为我的rails应用程序制作一个gem!?

你是否总是从一个插件开始,将它转换为一个宝石,我已经看到将它打包成宝石这个词。此外,我想构建的宝石在普通的ruby程序中不会有用,它只对rails应用程序有用。我甚至不确定是否有意义的语义,'RubyGem'只能在rails应用程序中工作!?



我想创建一个gem(如果这是我应该使用的?)为我的rails应用程序的离散片功能。它将需要添加数据库迁移,新路线并提供控制器和视图或有用的视图助手。我知道我可以通过一个插件来实现这个目的,但是只是想知道怎么做/为什么要做成'Ruby Gem'? 解决方案为避免过度工程的风险,我通常从我需要直接进入应用程序的功能开始。然后,只要我需要在另一个项目中使用相同的功能,我会检查是否值得将它提取到插件或提供API的单独应用程序中。



<插件和宝石经常是可以互换的。宝石在可重用性和可维护性方面提供了几个显着的优势。
另一方面,有一些具体的已知问题。例如,一个Rails应用程序实际上不能加载定义在一个插件中的rake任务,打包成一个Gem。



几乎每个Rails插件都可以打包成一个Gem。
例如,带上我的 tabs_on_rails 插件。



您可以将它安装为指定依赖于environment.rb的Gem。或者您可以像预期的那样使用 script / plugin install 命令。



如果您想获得相同的结果,请确保遵循标准的Gem布局,并提供Rails所需的 init.rb 初始化脚本。
此外,您可能需要创建 install.rb uninstall.rb 文件以包含该文章当插件作为标准Rails插件安装时,安装和后卸载钩子。



最后但并非最不重要的一点是,如果您将插件打包为Gem,您可以重新使用它非Rails项目,并使用 init.rb 文件提供Rails特定的初始化。非Rails应用程序将简单地忽略它。


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 discrete 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'?

解决方案

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.

这篇关于为Rails应用程序构建红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆