简体   繁体   中英

How to externalize Rails model (api/gem/plugin)

currently I am working on a RoR application (2.3.14 with ActiveRecord) (let's call it A).

Now I started another project B (a remote testing app using capybara, looks something like this: https://github.com/searls/remote-capybara-cucumber-example ).

But now I need to have access to the model of application B for test data setup (and possibly test assertions). I therefore would like to use the existing model classes (and some additional libraries like factory_girl if necessary).

I certainly don't want to wrap my project B in a Rails app and copy the model classes. So is there a way to organize A so that B can access the model and create/update/destroy entities?

Are there any keywords for further research (I tried several google searches containing rails model as a gem, as a plugin, externalize rails model etc... but nothing useful turned up (mostly the documentation of ActiveRecord)

Rails 2.x does make it very hard to share the model layer between two applications.

If you don't care about maintaining migrations twice, you can put the models into a gem and then require it in your apps.

Another way is to symlink the db and app/models directories from both applications to a shared folder. This works quite well though you have to be careful because rake tasks and generators now affect both applications.

Rails 3.1 ships with an improved implementation of rails engines. Engines allow you to isolate parts of an rails application and package them up as a gem.

You could try using an alias (symbolic link) to the A's app/models directory in the B project.

On Mac/Linux:

ln -s /volumes/code/project-a/app/models/ /volumes/code/project-b/models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM