Make all model classes replaceable #4577
Replies: 3 comments
-
I think this would be very interesting and not break existing extensions or implementation that work with eval or prepend. I am wondering how you envision this for (new) extensions? They will still need to rely on monkey patching. |
Beta Was this translation helpful? Give feedback.
-
That's a great idea! I just wrote something related but for the service objects: #4138 (comment) We could do the same for models. We can auto-register all of them and allow users to inject the ones they need. |
Beta Was this translation helpful? Give feedback.
-
I like the idea but I have the same concerns that @victorth raised:
|
Beta Was this translation helpful? Give feedback.
-
The problem
Most of the service objects used in Solidus are configurable, so that users can easily swap them with their own implementation which either inherits from or completely replaces the original. This allows users to customize the business logic in a very clean and easily testable way.
When it comes to models, things are a bit more complicated. Our official recommendation is to customize them through monkey patches, e.g.
This works, but it has a few drawbacks:
prepend
).The solution
I would like to suggest a different solution: right now, it's already possible to override the user model class. So instead of the above decorator, I could do the following:
With this configuration, Solidus will use my custom
User
model instead of the defaultSpree::User
. Because my model inherits from the built-in model, I still get all the out-of-the-box functionality, but I can add new features or override existing ones however I want.This creates a clearer separation between the Solidus domain and the host app domain, it plays well with static typing and it uses class inheritance, which is a well understood pattern in all OOP languages.
Nothing prevents us from simply extending this pattern to all models. See the following example:
This would be a very straightforward and convenient way of replacing/extending any models with your own. It has all of the advantages of
prepend
with none of the drawbacks.I'd like to gather feedback from the group and see how everyone feels about this, and whether it's worth a spike.
Beta Was this translation helpful? Give feedback.
All reactions