A very simple gem providing the notifications functionality to any model in a Rails application.
Add following line to your gemfile
And then execute:
Or install it yourself as:
$ gem install simple_notifications
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.
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.
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].
Following are the method available
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
Methods for [follower] object
Methods for [notification] object
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')
rails generate simple_notifications:copy_models
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.
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