2

What i want to do is a little different from what people think when they hear rails notification cause i've been reading up a lot about it but it didn't seem to help.

What i want to do is on creation of a new entry in a particular table i want certain users (admins) to get a notification in their home page or whatever page they're in, as the notification apparatus would be in the navigation bar.

How would i go about doing this ? i'm an intermediate rails developer, and i'm using rails 3.2.13.

Thanks in advance!

Josh Roberts
  • 95
  • 1
  • 8
  • 4
    Have a notification model and when an entry is created in the table create a notification. Then query for them if the user is an admin. You could either create a notification for each admin or have a dismissed notification model that tracks whether the admin has dismissed the notification – j-dexx Aug 13 '14 at 14:56
  • ok yeah that's easily done but what about a persistent server connection? so the user doesn't have to refresh the page to get the notification, any ideas ? – Josh Roberts Aug 13 '14 at 15:17

1 Answers1

3

There are a couple of things you'll want to do (I agree with the approach proposed by japed):

  1. Create the notification model with the required fields, I would suggest make it polymorphic so you can have access to the object that triggered the notification later (for example: having notifiable_type and notifiable_id fields, if an article creation triggered the notification creation you can then do something like notification.notifiable and get the article that was created)

  2. Place the callbacks that will generate the notification in place. for example: if you want to notify an admin every time a user creates an article, you can create the notification in the after_create method on the Article model.

  3. Have a way to know if the admin has read the notification or not (so you can show him a counter of unread notifications for example). For this I would recommend using the unread gem: https://github.com/ledermann/unread

  4. After the admin reads the notification (eg: after clicking on the unread counted) update the status of the notification (using the unread gem of course, not directly on the notification record), maybe with an ajax request.

apeniche
  • 659
  • 3
  • 8