Extending a model

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: 1
type: post_karma
condition: above
value: 100

And here’s a weather 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.

Thanks.

The best advice I can give you here, is one I have read on Sandy Metz
book(http://www.amazon.co.uk/Practical-Object-Oriented-Design-Ruby-Primer/dp/B00D81W074%3FSubscriptionId%3DAKIAILSHYYTFIVPWUY6Q%26tag%3Dduc08-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00D81W074):

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?

On 19 May 2014 05:18, Christophe N. [email protected] wrote:

  • 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.

Have you considered STI?

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs