Sha256: 0b0a5a1381480e3c5f8f156f62b0a501adddce34462bf43844adac46df4c1a58

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'pusherable/version'

module Pusherable
  def pusherable?
    false
  end

  def pusherable(pusherable_channel='test_channel')
    raise "Please `gem install pusher` and configure it to run in your app!" if Pusher.app_id.blank? || Pusher.key.blank? || Pusher.secret.blank?

    class_attribute :pusherable_channel
    self.pusherable_channel = pusherable_channel

    class_eval do
      after_create :pusherable_trigger_create
      after_update :pusherable_trigger_update
      before_destroy :pusherable_trigger_destroy

      def self.pusherable?
        true
      end

      private

      def pusherable_class_name
        self.class.name.underscore
      end

      def pusherable_trigger_create
        Pusher.trigger(pusherable_channel, "#{pusherable_class_name}.create", self.to_json)
      end

      def pusherable_trigger_update
        Pusher.trigger(pusherable_channel, "#{pusherable_class_name}.update", self.to_json)
      end

      def pusherable_trigger_destroy
        Pusher.trigger(pusherable_channel, "#{pusherable_class_name}.destroy", self.to_json)
      end
    end
  end
end

if defined?(ActiveRecord::Base)
  ActiveRecord::Base.extend Pusherable
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pusherable-1.0.4 lib/pusherable.rb