SimpleNotification
SimpleNotification is a user notification system using tracked events. Event has a owner, recipient, trackable source, and a message.
Assumptions
SimpleNotification assumes a user systems is in place which uses the User model and has the current_user helper.
SimpleNotification also assumes that the Notification model is not taken.
Installation
Add this line to your application's Gemfile:
gem 'simple_notification'
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_notification
Usage
SimpleNotification generates the files in the /app folder, feel free to tweak it to fit specific needs.
First generate the necessary files:
$ rails g simple_notification
Add the routes for the view in /config/routes.rb:
resources :notifications
Add the following to /app/models/user.rb:
has_notifications
Add the following to any models that needs to be tracked:
can_be_tracked
And finally, add the following to /app/controllers/application_controller.rb:
include SimpleNotification::Methods
Now the setup is complete, the following are available:
Notifications generated by a user's activities:
@user.own_notifications
Notifications meant for a user:
@user.received_notifications
one can add .where(read: false) to obtain unread notifications
Method to notify a user of a tracked event:
notify(recipient, trackable, message)
which will generate a notification from current_user (guest if undefined) to the recipient with the message and trackable source.
Notifications are now working and can be displayed however desired, sample usages are:
render "notifications/show", notification_id: notification.id, read: false
render "notifications/show", notification_id: notification.id, read: true
Will will generate the following by default:
**Owner** message **Trackable** | This notification was sent to **Recipient** | Destroy
Contributing
- Fork it ( http://github.com//simple_notification/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
MIT License