Sharing AR Models Between Rails Applications

I have a problem that I've been trying to solve for a while. I have 4 different Rails applications using the same database, meaning they must use the same models and have the same migrations. I first solved the problem by creating a gem-packed Rails engine that then transports all the models and migrations with it. Now I understand that there are pieces of functionality that only one application needs and others not - for example, an admin application needs to have methods to provide sorted tables for all models - other applications don't need this feature at all.

So my idea was to find a way that I can provide "base" gemstone models, while augmenting those base models in my specific applications to add additional functionality when needed. I first tried to inherit:

class User < Base::User
end

      

This doesn't work because now you have 2 user models in your load path (User and Base :: User), and when asking for associations, it always picks the "closest" class for the corresponding write class - this means that when you have there is a Model :: Account that owns_to: user, it will select Model :: User as the association class, not User. I tried to change the AR type calculation method, but this led to more problems.

I can technically provide all my models from the underlying engine (gem), but the problem here is, how do I extend those models in my application? .Class_eval feels really messy, inheritance doesn't work providing basic functionality as mixins means " basic "models do not feel and do not look like models at all. My goal would be to make as little friction as possible for other developers, I want them to be able to define their models in stone as they do, and then also have an easy way to extend that functionality to other applications.

Has anyone solved this problem before or had any suggestions? Or how do you solve this problem in your larger applications? Any help would be appreciated.

+3


source to share


1 answer


This is mentioned in the Rails guide . It describes modifying a class using the Decorator pattern.



0


source







All Articles