Module: Alondra::ChangesCallbacks

Extended by:
ChangesCallbacks
Included in:
ChangesCallbacks
Defined in:
lib/alondra/changes_callbacks.rb

Instance Method Summary collapse

Instance Method Details

#channel_names_from(type, record, options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alondra/changes_callbacks.rb', line 40

def channel_names_from(type, record, options)
  case options[:to]
  when String then
    [options[:to]]
  when Symbol then
    records = record.send options[:to]
    Channel.names_for(records)
  else
    [Channel.default_name_for(record)]
  end
end

#push_creations(klass, options) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/alondra/changes_callbacks.rb', line 13

def push_creations(klass, options)
  klass.class_eval do
    after_create do |record|
      ChangesCallbacks.push_event :created, record, options
    end
  end
end

#push_destroys(klass, options) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/alondra/changes_callbacks.rb', line 21

def push_destroys(klass, options)
  klass.class_eval do
    after_destroy do |record|
      ChangesCallbacks.push_event :destroyed, record, options
    end
  end
end

#push_event(type, record, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/alondra/changes_callbacks.rb', line 29

def push_event(type, record, options)
  channels = channel_names_from(type, record, options)

  channels.each do |channel|
    event = Event.new(:event         => type,              :resource => record,
                      :resource_type => record.class.name, :channel  => channel)

    MessageQueueClient.push event
  end
end

#push_updates(klass, options) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/alondra/changes_callbacks.rb', line 5

def push_updates(klass, options)
  klass.class_eval do
    after_update do |record|
      ChangesCallbacks.push_event :updated, record, options
    end
  end
end