I'm not sure what is the best approach for my problem. I need a model
for notification (or alerts) in my application. Notifications are based
on
services, have multiple fields to define conditions and are associated
with a user. Here's an example for a reddit notification:
user_id: 2
type: temperature
city: Seattle
condition: above
value: 70
So both entries are notifications but they have different fields. Should
I:
- Create different models for each type of notification?
- Create a base notification model and other models for each type that
extend the base one?
- Only have 1 model (notification) and then some sort of condition
table. A notification has multiple conditions.
You won’t know what kind of fields all notifications will have in common until you implement them all.
First you should implement all the concrete classes for every notification and then if there is many fields/methods that all of them have in common, you can create a concern or base notification class.
It’s much easier to refactor from all the concrete to an abstract class, then start from a guess abstract class and then refactor to concrete ones. For example, if you defined a base class notification with 3 fields: user_id, type and value. And after you defined 5 notifications, you would realise a new one does not need value. And imagine value is used in 3 different methods. Removing this could be a pain to all the other 4 “sub-classes”.
So def start by implementing concrete with a good test base and then it will be much easier to refactor to an abstract class.
I'll take a look at that book and read on single table inheritance
(STI). Thanks for your suggestions.
I was also thinking about having a notification model and has N
attributes. An attribute is a model that basically only have a type and
value. Think that's a good idea?