Created
August 27, 2013 17:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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