User notifications is a common feature of many web applications. They are a useful mean of informing users about actions that happened without their direct input (e.g.: background tasks or actions taken by other users).
After looking at a few implementations of this, I wanted to make an attempt of my own at designing a generic solution for notifications as a Ruby gem.
The objective was to have a gem that enabled notifications at a configuration level in Rails models. Ideally, only two steps should be needed to configure notifications:
- Hooking the publisher and the subscriber using a specific trigger
- Defining the notification's content mapping using the publisher
More details can be found in the repo's README.
At this point, correspondent contains the model configuration and a JSON API for retrieving and dismissing notifications created. However, it is still under development and might not yet be suited for a production environment.
I am posting this because I would really appreciate feedback and suggestions on the idea for the gem. And of course, contributors are very welcome as well!
Dead simple configurable user notifications using the Correspondent engine!
Configure subscribers and publishers and let Correspondent deal with all notification work with very little overhead.
Add this line to your application's Gemfile:
And then execute:
Create the necessary migrations:
$ rails g correspondent:install
Notifications can easily be setup using Correspondent. The following example goes through the basic usage. There are only two steps for the basic configuration:
- Invoke notifies and configure the subscriber (user in this case), the triggers (method purchase in this case) and desired options
- Define the to_notification method to configure information that needs to be used to create notifications
# Example model using Correspondent # app/models/purchase.rb class Purchase < ApplicationRecord belongs_to :user belongs_to :store # Notifies configuration # First argument is the subscriber (the one that receives a notification). Can be an NxN association as well…