Class: FayePluginObserver

Inherits:
ActiveRecord::Observer
  • Object
show all
Defined in:
lib/faye-plugin/observers/faye_plugin_observer.rb

Instance Method Summary collapse

Instance Method Details

#after_update(model) ⇒ Object



22
23
24
# File 'lib/faye-plugin/observers/faye_plugin_observer.rb', line 22

def after_update(model)
  @client_changes.call
end

#before_update(model) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/faye-plugin/observers/faye_plugin_observer.rb', line 10

def before_update(model)
  if model.changed?
    @client_changes = lambda do
      model.changes.each do |key, value|
        broadcast_message("/listener", "$(\"##{model.class.name.downcase}_#{model.id}_#{key}\").html(\"#{value[1]}\")")
      end
    end
  else
    @client_changes = lambda {}
  end
end

#broadcast_message(channel, data) ⇒ Object



4
5
6
7
8
# File 'lib/faye-plugin/observers/faye_plugin_observer.rb', line 4

def broadcast_message(channel, data)
  message = {:channel => channel, :data => data, :ext => {:auth_token => FAYE_TOKEN}}
  uri = URI.parse("http://#{Rails.configuration.faye_host}:#{Rails.configuration.faye_port}/faye")
  Net::HTTP.post_form(uri, :message => message.to_json)
end