Eventifier

Event tracking and notifying for active record models

Features

Tracks and logs events on active record objects Send notifications of events

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,author { 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.


  # 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

Testing

Creating the database:

  • createdb eventifier

Spec


  rspec spec

Contributors

  • PJ Murray