No commit activity in last 3 years
No release in over 3 years
A very simple gem providing the notifications functionality to any model in a Rails application.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
 Project Readme

Simple Notifications

A very simple gem providing the notifications functionality to any model in a Rails application. It does not provide the push notifications.

It creates all the models/migrations/associations/callbacks for you.

Installation

Add following line to your gemfile

gem 'simple_notifications'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_notifications

Usage

  • First run the simple notifications generator to generate two files in your rails project

    • simple_notifications.rb - An initializer file.
    • Migration files - Required for recording notifications.
rails generate simple_notifications:install
  • Migrate Database
rails db:migrate

Add the following line to the model(after all the associations defined) 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.

acts_as_notifier   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

acts_as_notifier sender: :author, receivers: User.all, actions: [:create]
acts_as_notifier sender: User.first, receivers: [:followers, User.all], actions: [:create]

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.