Two rails apps sharing a model folder

前端 未结 7 403
半阙折子戏
半阙折子戏 2020-12-23 22:14

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 de

相关标签:
7条回答
  • 2020-12-23 22:23

    Just curious - why not have one app with two modes?

    I have one application that looks like one of a dozen different apps, branded differently and with different functionality, depending on which URL you're coming in on and who you log in as. My users have roles and I added a method to ActiveRecord::Base that gives you the current user. Thus I can do stuff like:

    MAX_VOLUME = current_user.admin? ? 11 : 10
    validates_inclusion_of :volume, :in => 0..MAX_VOLUME # Admins can go to 11!
    

    And in the views, stuff like this:

    <%= render :partial => common_tabs %>
    <%= render :partial => admin_tabs if @current_user.admin? %>
    
    0 讨论(0)
  • 2020-12-23 22:24

    Why not using composition instead of inheritance?

    I have a similar issue : an admin app and a public app, with the same database, and only some specific methods on some models for one app or the other.

    I'm currently thinking I could create a common place with many modules where I would put my methods. I would then include the modules I need in each app (in models, controllers, helpers, …) when I need them. This place could be in the lib directory (updated with a git submodule or svm external) or in a gem (updated with Bundler or a similar tool).

    What do you think about this ?

    0 讨论(0)
  • 2020-12-23 22:27

    You can treat one rails project as a gem, then you can use it in other rails project by add the model path to its load path.

    Here's an example, i think you can get it, though it's written in chinese. http://mvj3.github.com/2011/09/13/multiple_rails_apps_sharing_models_folder/

    0 讨论(0)
  • 2020-12-23 22:30

    Why not doing it OOP way, e.g. creating a separate base class (in a separate directory) and then inheriting from it in both projects, so the differences are in the inherited class.

    So, you have

    class BaseModel < ActiveRecord::Base
    

    in "common" subdirectory, and then you have

    class AdminBaseModel < Common::BaseModel
    

    in admin project and

    class UserBaseModel < Common::BaseModel
    

    you might need

    set_table_name "basemodel"
    

    so Rails knows which table to open.

    0 讨论(0)
  • 2020-12-23 22:30

    svn:externals or git submodules are definitely the way to go for this sort of situation.

    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.

    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.

    Update (some years later)

    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.

    0 讨论(0)
  • 2020-12-23 22:32

    I work on a project that has a similar issue and we used svn:externals to share the model code between two apps.

    Basically you have the models diretory in one svn project and use an external in the other project to share the code. You can edit code in either project and it will be updated when you run svn up in the other project.

    As far as Rails and your build scripts are concerned the two models directories are completely separate.

    0 讨论(0)
提交回复
热议问题