Module: Protocolist::ModelAdditions::ClassMethods

Defined in:
lib/protocolist/model_additions.rb

Instance Method Summary collapse

Instance Method Details

#fires(activity_type, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/protocolist/model_additions.rb', line 24

def fires(activity_type, options = {})
  fires_on  = [*options[:on] || activity_type]
  data_proc = extract_data_proc(options[:data])

  options_for_callback = options.slice(:if, :unless)
  options_for_fire     = options.except(:if, :unless, :on)

  callback_proc = ->(record; options) do
    options = options_for_fire.merge(data: data_proc.call(record))

    record.fire(activity_type, options)
  end

  fires_on.each do |on|
    send("after_#{on}", callback_proc, options_for_callback)
  end
end