Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Render views to Pusher from anywhere in your application

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 action_pusher.gemspec
README.markdown

ActionPusher

Render views to Pusher from anywhere in your application

Pusher users often want to transmit JSON or HTML to the browser upon model events, but models have no business generating either. ActionPusher allows you to render data to Pusher using your existing view templates from an observer or model.

Requirements

Installation

gem 'action_pusher', :require => false

Create an initializer at config/initializers/action_pusher.rb such as:

require 'action_pusher'
ActionPusher::Base.view_paths = File.join( Rails.root, 'app/api' )

Ensure that view_paths points to the location where your templates are actually stored. The default is app/views.

Create a directory at app/pushers to store your ActionPusher "controllers".

Example

app/pushers/notification_pusher.rb:

class NotificationPusher < ActionPusher::Base
  def initialize(notification)
    @notification = notification
    event = @notification.event
    channel = @notification.notification_channel.name
    Pushable.push(channel, event, render(template: 'notifications/show'))
  end

  def controller_path; 'notifications'; end
end

Now when I call NotificationPusher.new( notification ) from my model callbacks, observers, what-have-you... my Notification model object is transmitted to Pusher as pretty JSON output from jbuilder!

Credit

This gem was inspired by stephan.com's solution to a question on StackOverflow, I just wrapped it up in a gem once I got it working well in production.

Something went wrong with that request. Please try again.