问题描述
我有两个运行在同一个数据库上的 rails 应用程序,一个运行客户端,一个提供管理界面.
I have two rails apps running off the same database, one which runs the client and one which provides an admin interface.
除了少量差异外,这两个应用程序都定义了完全相同的模型.将模型中的绝大多数更改复制到这两个应用程序上对我来说很烦人.
Both the apps have the exact same models defined save for small number of differences. Its tiresome for me to duplicate the vast majority of changes in the models on to both apps.
两个应用程序使用相同模型信息的一种方法是将模型文件夹从一个应用程序符号链接到另一个应用程序,但由于代码中的一些差异(例如对客户端的额外验证),我无法这样做.
One way for two apps to use the same model info is to symlink the model folder from one app to the other, but I cant do that due to the few differences in code (e.g an extra validation on the client).
是否有一种简单的方法可以消除差异,以便将通用代码保存在一个地方?
Is there a simple way I can move out the differences so I can keep the common code in one place?
推荐答案
svn:externals 或 git submodules 绝对是解决这种情况的方法.
svn:externals or git submodules are definitely the way to go for this sort of situation.
当然,您希望能够在这两个应用中测试它们,因此您也应该分享您的模型测试或规格.但请记住,模型通常依赖于插件,因此您也需要共享您的插件文件夹.而且您可能想要相同的 gems 和相同版本的 Rails,所以最好的办法是共享所有供应商.哦,有时你在 lib 中的代码会修改你的模型,所以你会想要分享它.哦,一定要在环境文件中共享任何自定义配置.
Of course, you'll want to be able to test them in both apps, so you should share your model tests or specs as well. But remember that models often depend on plugins, so you'll want to share your plugins folder, too. And you probably want the same gems and the same version of Rails, so your best bet is to share all of vendor. Oh, and sometimes your code in lib modifies your models, so you'll want to share that. Oh, and be sure to share any custom configuration in the environment files.
并且您需要设置一个持续集成服务器来在两个应用程序上运行您的测试套件,以防主应用程序中模型层的更改破坏了您的另一个应用程序.
And you'll want to have a continuous integration server set up to run your test suite on both applications in case changes to the model tier in your master application break your other application.
但我的意思是,一旦所有这些都解决了,svn:externals 或 git submodules 绝对是解决这种情况的方法.
But I mean, once all that is worked out, svn:externals or git submodules are definitely the way to go for this sort of situation.
更新(几年后)
Rails 引擎可能是目前在多个应用程序之间可靠地共享模型代码的最佳选择.它们可以被宝石化并包含在每个应用程序的 Gemfile
中.
Rails Engines are probably the best bet currently for reliably sharing model code between multiple applications. They can be gemified and included in the Gemfile
of each application.
这篇关于共享一个模型文件夹的两个 Rails 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!