Eventifier
Event tracking and notifying for active record models
Features
Tracks and logs events on active record objects Send notifications of events
Assumptions
- You’re using warden for auth (or Devise)
Example
class EventTracking
include Eventifier::EventTracking
def initialize
events_for Post do
track_on [:create, :update, :destroy], attributes: { except: %w(updated_at) }
notify group: :members, on: :create
notify group: :commenters, on: :update, unless: ->post, { post.age > 1.week }
end
events_for Announcement do
track_on :create
notify group: :members, on: :create, if: ->announcement,admin { announcement.is_important? }
end
end
end
That’s it!
Installation
Add Eventifier to your Gemfile
# Gemfile
gem "eventifier"
And run bundle install
Add Eventifier to your Gemfile
rails generate eventifier:install
rake eventifier:install:migrations
Add notifications indicator in interface
Eventifier provides a notification tool for your users.
<div class="notifications">
<a href="#" class="notifications-toggle">
<span class="icon-notifications"></span>
<span class="notifications-alert"></span>
</a>
</div>
# app/views/layouts/application.erb
<div class="notifications"></div>
Add stylesheets
# app/assets/stylesheets/application.scss
/*= require eventifier/notifications*/
Add javascript
# app/assets/javascripts/application.coffee
#= require eventifier/notifications
new NotificationDropdown el: $(".notifications")
Add the required routes
# config/routes.rb
mount Eventifier::Engine => '/'
Customise views
# app/views/eventifier/dropdown/_comment.haml
%a{ href: "#{root_url}#{url_for([object.commentable.user, object.commentable])}" }
= image_tag object.user.photo.thumb.url, class: 'avatar'
#{object.user.username} commented on your post
You can create a custom view for each context
Dropdown notification: app/views/eventifier/dropdown/comment.haml Email notification: app/views/eventifier/email/comment.haml
Helpers made available to these views are:
notification: The notification object Eventifier::Notification(event: event, user: user_being_notified, parent: parent_notification, sent: email_sent_flag)
event: The event object Eventifier::Event(user: event_owner, eventable: object_for_event, verb: [:create, :update, :destroy], change_data: => [“Bill”, “Bob”], groupable: grouped_object)
object: The object the event was created for
Sending of emails
Firstly, you’ll need to set the FROM address for the Eventifier mailer
# config/initializers/eventifier.rb
Eventifier.mailer_sender = 'Funways <[email protected]>'
You want to add a scheduled task to run the following task every x minutes
rake eventifier:email:deliver
Customise email settings descriptions
# config/locales/events.en.yml
en:
events:
labels:
preferences:
default: "All notifications"
create_relationships_notify_followed: "When you get a new follower"
Requirements
- ActiveRecord
Turbolinks
Turbolinks removes the body of the page, which is where our dropdown notifications box sits. We’ll need to re-render it and point it at the holding element when the page changes.
``` $ → window.notifications = new NotificationDropdown el: $(‘.notifications’), pollTime: 60
$(document).on “page:change”, → notifications.el = $(‘.notifications’) notifications.render() ```
Testing
Creating the database:
- createdb eventifier
Spec
rspec spec
Contributors
- PJ Murray