Render your notifications on multiple platforms by specifying notification types.
NotificationRenderer works with Rails 5 onwards. You can add it to your Gemfile
with:
gem 'notification-renderer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install notification-renderer
Now run the generator:
$ rails g notification_renderer:install
To wrap things up, migrate the changes to your database:
$ rails db:migrate
NotificationRenderer uses templates to render your notifications.
The type
of a notification determines which template gets utilized for rendering. Each notification type has multiple templates each of which responsible for rendering a notification in another scenario. The default template for a given type is index
.
This gem comes with a generator to make adding new types a whole lot easier. Run in your terminal:
$ rails g notification_renderer:type -t notification
This will create the following structure in your application:
views
notifications
notification
_index.html.erb
You can also customize the generated templates (renderers):
$ rails g notification_renderer:type -t notification -r index feed
This command will also create a custom renderer called feed
for the notification type notification
:
views
notifications
notification
_feed.html.erb
_index.html.erb
You are able to specify the type
of a Notification
record:
notification = Notification.create(target: User.first, object: Recipe.first, type: 'notification')
Note: The type
attribute of any new Notification
record will default to the default_type
configuration.
You can also scope records by their type:
# Return records with `'notification'` as type
Notification.notification_type
# Return records with `'follow'` as type
Notification.follow_type
In your renderers you can access the Notification
record through the notification
variable. This is how a renderer could look like:
<%= notification.target.name %> commented on <%= notification.object.article.title %>.
NotificationRenderer introduces some view helpers to assist you in embedding notifications.
render_notification
renders a single Notification
record:
<%= render_notification Notification.first %>
Rendering a notification will set its read
attribute to true
. This behavior can be configured.
You can also specify a renderer. It defaults to 'index'
.
<%= render_notification Notification.first, 'feed' %>
render_notifications
takes an ActiveRecord array of Notification
records and renders each of them in order:
<%= render_notifications Notification.all %>
You can also specify a renderer. It defaults to 'index'
.
<%= render_notifications Notification.all, 'feed' %>
It wraps the rendered notifications in a div
:
<div class="notification-renderer notifications">
<!-- ... -->
</div>
You can group any ActiveRecord array of Notification
records by an attribute value:
Notification.all.grouping(['object.article'])
Notification.all.grouping(['object.article', 'metadata[:title]'])
Note: Notifications will be grouped in order.
When rendering notifications you often want to group them by the object they belong to. This is how to group notifications by the associated object:
<%= render_notifications_grouped Notification.all, ['object'], renderer: 'feed' %>
You can also group notifications by nested attributes:
<%= render_notifications_grouped Notification.all, ['object.article'] %>
<%= render_notifications_grouped Notification.all, ['metadata[:title]'] %>
It is also possible to group notifications for just one object:
<%= render_notifications_grouped Notification.where(object_id: 1, object_type: 'Comment'), ['object'] %>
This will render the last notification for every group and pass the attributes value grouped by to your renderer:
<!-- View -->
<%= render_notifications_grouped Notification.notification_type, ['object.article'] %>
<!-- Renderer -->
<% if notification_grouped? %>
<%= notification.target.name %> and <%= (notifications.size - 1).to_s %> others commented on <%= attributes['object.article'].title %>.
<% else %>
<%= notification.target.name %> commented on <%= notification.object.article.title %>.
<% end %>
Grouping makes the following two methods available in your renderer:
attributes
Hash of attributes grouped by and their values.
notifications
The ActiveRecord array of notifications including the currently rendered notification.
You may check whether a template is being used for grouping by using the notification_grouped?
helper method.
It is common, if rendering multiple notification types at once, to group the notifications by their type:
<%= render_notifications_grouped Notification.all, ['object.article'], group_by_type: true %>
This is identical to the following:
<%= render_notifications_grouped Notification.all, [:type, 'object.article'] %>
It is also often required to group notifications by their date of creation:
<%= render_notifications_grouped Notification.all, ['object.article'], group_by_date: :month %>
This is identical to the following:
<%= render_notifications_grouped Notification.all, ['created_at.beginning_of_month', 'object.article'] %>
Accepted values are:
:minute
:hour
:day
:week
:month
:year
Note: If used together with group_by_type
, notifications will be grouped first by creation date and then by :type
.
You can configure NotificationRenderer by passing a block to configure
. This can be done in config/initializers/notification-renderer.rb
:
NotificationRenderer.configure do |config|
config.default_type = 'notification'
end
default_type
Choose your default notification type. Takes a string. Defaults to 'notification'
.
default_renderer
Choose your default renderer. Takes a string. Defaults to 'index'
.
auto_read
Automatically mark rendered notifications as read. Takes a boolean. Defaults to true
.
We use GitHub projects to coordinate the work on this project.
To propose your ideas, initiate the discussion by adding a new issue.
We hope that you will consider contributing to NotificationRenderer. Please read this short overview for some information about how to get started:
Learn more about contributing to this repository, Code of Conduct
NotificationRenderer follows Semantic Versioning 2.0 as defined at http://semver.org.