Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share common models between two Rails Application

I am developing an E-commerce application and also working on its admin website. These two applications have the same business domain and there are a few models from the e-commerce application that I need in the admin app.

I have found a few solutions online for sharing models although I am not clear which is better and how should I implement it.

Solutions I found:

  1. Create a rake task to copy the models from the e-commerce app to the admin app

  2. Create a third ruby module and put the models in there and pull the models from the app directory into this newly created lib and require it inside the app

  3. Auto loading the models from the e-commerce app to admin ruby search

    config.autoload_paths += %W(#{config.root}/../e-commerce-app/app/models/)
    

I think the second solution is the right way to do it though I am unsure of how to implement it.

like image 542
Saurabh Avatar asked Sep 23 '14 10:09

Saurabh


1 Answers

None of these.

How about creating a shared gem and put that on your private git repo? With Rails, you can provide the same mechanism for autoloading the models within the application itself by packaging the gem as a rails_engine

See example

At the end require it within Gemfile

gem 'my_gem_name', git: 'http:my_git_repo.com/my_gem_name'

A more advance solution would be creating a REST service for the desired data in the e-commerce app and consume it in the admin app. Especially for Users (and authentication), it would even make sense to extract it to a separate app and provide a UserAuthentication Service.

like image 120
dre-hh Avatar answered Oct 03 '22 02:10

dre-hh