SimpleNotifications
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 file - Required for recording notifications.
Then run
rails db:migrate
Add following line to the model for which notifications functionality is required
notify sender: :product_class,
receivers: :variants,
actions: [:follow, :unfollow, :update, :create],
notify_message: :dd_message,
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
Or you can 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 and :followers should be associations for the model which needs to be notified.
Notification Models
SimpleNotifications::Record
SimpleNotifications::Delivery
Scopes
SimpleNotifications::Record.read
SimpleNotifications::Record.unread
Methods
Suppose Post is the notified model and author is the sender association and followers is the receiver association. Then following methods are available
Post.notified?
Methods for the [post] object
post.notify(sender: :author, receivers: :followers, message: 'My own message')
post.notifications
post.notifiers
post.notificants
post.#{receiver_class}_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
.sent_notifications
Methods for [follower] object
follower.received_notifications
Methods for [notification] object
SimpleNotifications::Record.last.sender
SimpleNotifications::Record.last.entity
SimpleNotifications::Record.last.#{receiver_class.name.downcase}_receivers
Skipping Notification
Post.create(content: '123', notify: false)
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.