如何抽象出多个 Rails 应用程序的核心功能?
我想开发一些重要的 Rails 应用程序,它们都实现一组核心功能,但每个应用程序都有某些特定的自定义、扩展和审美差异。如何提取所有这些系统共有的核心功能(模型、控制器、帮助器、支持类、测试),以便更新核心将使基于它的每个应用程序受益?
我见过 Rails Engines,但它们似乎太独立,几乎太抽象而无法构建。我认为它们对于向现有应用程序添加一个组件很有用,例如将博客引擎连接到现有的电子商务网站。由于引擎似乎大多是独立的,因此在保持 DRY 的同时覆盖它们的功能和视图似乎很困难且不方便。
我也考虑过将代码抽象成 gem,但这似乎有点奇怪。我是否使 gem 依赖于 Rails gem,以及定义模型和模型?里面的控制器,然后在我的各种应用程序中对它们进行子类化?或者我是否在 gem 中定义了许多模块,并将其包含在各种应用程序的不同位置?如何测试 gem,然后测试其上的自定义设置和覆盖功能集?我还关心如何协同开发 gem 和 Rails 应用程序,我可以将 gem 的 git 存储库供应到应用程序中并从中推送,这样我就不必每次迭代都构建新的 gem 吗?另外,是否有私人宝石托管/我可以设置自己的宝石源吗?
另外,对于此类事业有什么一般性建议吗?要遵守的抽象范式?必读?以前做过这件事的智者的评论?谢谢!
I'd like to develop a number of non-trivial Rails applications which all implement a core set of functionality but each have certain particular customizations, extensions, and aesthetic differences. How can I pull the core functionality (models, controllers, helpers, support classes, tests) common to all these systems out in such a way that updating the core will benefit every application based upon it?
I've seen Rails Engines but they seem to be too detached, almost too abstracted to be built upon. I can seem them being useful for adding one component to an existing app, for example bolting on a blog engine to your existing e-commerce site. Since engines seem to be mostly self contained, it seems difficult and inconvenient to override their functionality and views while keeping DRY.
I've also considered abstracting the code into a gem, but this seems a little odd. Do I make the gem depend on the Rails gems, and the define models & controllers inside it, and then subclass them in my various applications? Or do I define many modules inside the gem that I include in the different spots inside my various applications? How do I test the gem and then test the set of customizations and overridden functionality on top of it? I'm also concerned with how I'll develop the gem and the Rails apps in tandem, can I vendor a git repository of the gem into the app and push from that so I don't have to build a new gem every iteration? Also, are there private gem hosts/can I set my own gem source up?
Also, any general suggestions for this kind of undertaking? Abstraction paradigms to adhere to? Required reading? Comments from the wise who have done this before? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从来没有做过这样的事情,但我想 Rails 插件是最合适的地方:
http ://guides.rubyonrails.org/plugins.html#models
http:// guides.rubyonrails.org/plugins.html#controllers
http://guides.rubyonrails .org/plugins.html#helpers
然后你可以用它创建一个 gem:
http://guides.rubyonrails.org/plugins.html#plugingems
Never did such a thing, but I guess that a Rails plugin would be the right place for it:
http://guides.rubyonrails.org/plugins.html#models
http://guides.rubyonrails.org/plugins.html#controllers
http://guides.rubyonrails.org/plugins.html#helpers
And you can create a gem out of it afterwards:
http://guides.rubyonrails.org/plugins.html#plugingems