Skip to content

Instantly share code, notes, and snippets.

@cwoodcox
Created August 27, 2013 17:36
Show Gist options
  • Save cwoodcox/6356582 to your computer and use it in GitHub Desktop.
Save cwoodcox/6356582 to your computer and use it in GitHub Desktop.
I just wrote this. Now I understand why it's a thing. BECAUSE IT'S COOL.
module Shortwave
module Observable
def self.included(base)
base.send :include, InstanceMethods
end
module InstanceMethods
class Notification
attr_accessor :event, :id, :klass
def initialize(record)
klass = record.class.underscore
id = record.send(record.class.primary_key)
event = record.new_record? ? :created : :updated
end
def routing_key
"#{klass}.#{id}"
end
end
def notify(record)
notification = Notification.new(record)
Shortwave.broadcast(notification.routing_key, notification.event)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment