A very simple gem providing the notifications functionality to any model in a Rails application.
Installation
Add following line to your gemfile
gem 'simple_notifications'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_notifications
Usage
Run the simple notifications generator
rails generate simple_notifications:install
This will generate two files in your rails project
- simple_notifications.rb - An initializer file.
- Migration files - Required for recording notifications.
Then run
rails db:migrate
Add following line to the model for which notifications functionality is required. Here [Post] is the model which is the base for notification to happen i.e Event is performed on Post.
notify sender: :author,
receivers: :followers,
actions: [:follow, :unfollow, :update, :create, :destroy],
notify_message: :message_method,
before_notify: :before_notify_method,
after_notify: :after_notify_method,
before_delivered: :before_delivered_method,
after_delivered: :after_delivered_method,
before_read: :before_read_method,
after_read: :after_read_method
Here [receivers] will be notified that an event was done by [sender] on [post] entity with a message that is configurable.
You can also provide ActiveRecord::Base object or ActiveRecord::Relation objects as
notify sender: :author, receivers: User.all
notify sender: User.first, receivers: [:followers, User.all]
Here [:sender] is the [belongs_to] association with [:post] while :followers is the [:has_many] associations for the [:post] model through [:sender] model which needs to be notified.
Notification Models
SimpleNotifications::Record
SimpleNotifications::Delivery
Here assumption is that one event performed by [:sender] on entity [:post] will have one type of notification and it needs to be delivered to many [:receivers].
Scopes
SimpleNotifications::Record.read
SimpleNotifications::Record.unread
Methods
Following are the method available
Post.notified?
Methods for the [post] object
post.notify
post.notify(sender: :author, receivers: :followers, message: 'My own message')
post.notifications
post.notifiers
post.notificants
post.read_marked_notificants
post.unread_marked_notificants
post.mark_read
post.mark_read(receivers)
post.mark_unread
post.mark_unread(receivers)
Methods for [author] object
author.sent_notifications
Methods for [follower] object
follower.received_notifications
Methods for [notification] object
SimpleNotifications::Record.last.sender
SimpleNotifications::Record.last.entity
Skipping Notification
Post.create(content: '123', notify_flag: false)
Post.create(content: '123', notify_flag: nil)
Custom Notification message
Post.create(content: '123', message: 'My custom notification message')
Generators
rails generate simple_notifications:copy_models
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/aashishgarg/simple_notifications. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the SimpleNotifications project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
comments powered by Disqus