Pusherable

Build Status

Adds callback hooks for your ActiveModel models for sending messages to a Pusher channel.

Installation

Install and configure Pusher to work on your application by following the pusher gem’s instructions.

Then, add this line to your application’s Gemfile:

gem "pusherable"

And then execute:

$ bundle

Or install it yourself as:

$ gem install pusherable

Usage

Add in the following lines to any ActiveModel model class:

pusherable("some_channel")

Pusherable has a default channel of test_channel, just like the Pusher example docs, that it will publish to.

On your subscribed client(s), events will be triggered by Pusher reflecting your ActiveModel_ create/update/destroy actions.

Here is a list of the ActiveModel callbacks that trigger Pusher events…

ActiveModel Callback => Triggered Event
----------------------------------------
after_create => "model.create"
after_update => "model.update"
before_destroy => "model.destroy"

Example

If you have an ActiveModel model called, Post, and you create a new record, Pusher will receive an event called, “post.create”. It will also carry a payload of data containing a JSON representation of the record (literally calling to_json on the record).

The following callbacks that trigger Pusher events in this Post example will then be…

ActiveModel Callback => Triggered Event
----------------------------------------
after_create => "post.create"
after_update => "post.update"
before_destroy => "post.destroy"

Currently this gem extends ActiveRecord::Base and Mongoid::Document if defined.

ActiveRecord::Base.extend Pusherable

For any other ActiveModel compliant “Data store”, simply mirror this statement.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request