With a Notification
model, how would you model the Sender/Actor relationship to allow for system notifications, where the application is the sender? i.e “Your export completed”, “Your trial is about to end”, “Welcome to the application”. A good frame of reference is the notification menu in the top left corner of discuss.rubyonrails.org
Here is an explanation for some of the fields for the above model:
recipient_id
: Represents the user in your application which will receive the notification.
actor_id
: Represents the user in your application which triggered the notification.
read_at
: The time when the notification was read. A value of nil is used for unread notifications.
notifiable_id
: The object that represents this notification (a post, comment, etc). This will be a polymorphic association.
notifiable_type
: Type of the notifiable object. This is usually represented by a humanized (and optionally, internationalized) string form of the object’s class.
My first thought was to make the Actor(able) polymorphic, but that wouldn’t allow for a DEFAULT_APPLICATION that references the application, because it’s not in the database and therefore not referable. Extending on the idea of making the Actor polymorphic, the actor could be the @export
and current_user.subscription
, respectively. A third option would be to model the Notification
to allow for a nil
Actor and leave the actor nil
.
# Your export completed. Download your export.
# Your trial is ending soon. Update your payment information.
Notification.create(recipient: current_user, actor: ?, action: "completed", notifiable: @export)
Notification.create(recipient: current_user, actor: ?, action: "ending", notifiable: current_user.subscription)
# Polymorphic Actor
Notification.create(recipient: current_user, actor: @export, action: "completed", notifiable: @export)
Notification.create(recipient: current_user, actor: current_user.subscription, action: "ending", notifiable: current_user.subscription)
# nil
Notification.create(recipient: current_user, actor: nil, action: "completed", notifiable: @export)
Notification.create(recipient: current_user, actor: nil, action: "ending", notifiable: current_user.subscription)