Reputation: 1104
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:
Create a rake task to copy the models from the e-commerce app to the admin app
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.
Upvotes: 5
Views: 4509
Reputation: 8044
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.
Upvotes: 9
Reputation: 9937
The easiest way to do this is by creating a 'Mountable' gem using the command line:
rails plugin new e_commerce_app_core --mountable
This will set up your rails engine so all you need to do is move your model(s) from your app to e_commerce_app_core/app/models/
.
I include the gem like this:
gem 'e_commerce_app_core_core', git: 'https://github.com/my_repo/e_commerce_app_core_core.git'
Then I run:
bundle config local.e_commerce_app_core_core ../e_commerce_app_core_core
This command attached your local directory to your build instead of using the external repo. That way I can work with the gem locally and push it up to github when I'm ready.
I also moved my user controller that contained log in logic but to get my user routes to work I changed my config/routes.rb
from:
ECommerceAppCore::Engine.routes.draw do
...
end
to:
Rails.application.routes.draw do
...
end
I moved my migrations to my gem by adding this to my application.rb
:
config.paths['db/migrate'] = ECommerceAppCore::Engine.paths['db/migrate'].existent
Upvotes: 4
Reputation: 3075
You can use Git-subtree.
Git Subtree is used for sharing code between rails applications
Please have look at- http://igor-alexandrov.github.io/blog/2013/03/28/using-git-subtree-to-share-code-between-rails-applications/
I think this will help you.
Upvotes: 2