ActsAsNotifiable aims to be a flexibile notification system for your Rails application. On social networks, notifications can be sent from any kind of action. ActsAsNotifiable has 2 goals:
- Centralize the creation of notifications on different objects in a system
- Provide a modular way to deliver these notifications across various methods
Some examples of various things that may generate a notification include:
- A user follows another user
- A user comments on another user's post
- A user likes a post
RDocs for ActsAsNotifiable are available online here:
http://rubydoc.info/github/QFive/acts_as_notifiable/frames
As systems become larger and users have more ways to receive notifications, a flexibile delivery system is needed. ActsAsNotifiable couriers will do just this.
Example couriers:
- Apple Push Notification
- SMS
First, you will need a model to hold notifications.
class Notification < ActiveRecord::Base
include ActsAsNotifiable::Notification
end
A notification has the following properties:
belongs_to :sender, polymorphic: true
belongs_to :receiver, polymorphic: true
belongs_to :notifiable: polymorphic: true
belongs_to :target, polymorphic: true
With a base notification class in place we can start generating notifications for a user.
class Author < ActiveRecord::Base
has_many :posts
# Set up relations to accept notifications
receives_notifications
end
class Post < ActiveRecord::Base
has_many :comments
belongs_to :author
end
class Comment < ActiveRecord::Base
# Comments are notifiable, they will send notifications out when created
notifiable receiver: lambda { |c| c.post.author }, sender: :author
# Specify a courier to send notifications, in this case email
notify_via :email
belongs_to :author
belongs_to :post
end
Whenever a comment is created, the author of the post the comment belongs to will receive a notification notifying them that the comment author commented on their post.
notify_via
specifies any couriers to use to deliver this notification, in this case an email will be generated.
- Document advanced usage
- Abstract APNS courier out
- Example courier creation
- Refactor ActsAsNotifiable::Notification
MIT License. Copyright 2012 QFive, Inc. http://www.qfive.com