Class: Dallal::Events::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/dallal/events/observer.rb

Constant Summary collapse

NOTIFIABLES =
[]

Class Method Summary collapse

Class Method Details

.callbacksObject



14
15
16
# File 'lib/dallal/events/observer.rb', line 14

def self.callbacks
  @__notification_callbacks ||= []
end

.create_notification(id:, event:) ⇒ Object

TODO Rethink this. Currently one worker executes all notifications rethink moving to multiple concrete notifications so we can have one worker per notification.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dallal/events/observer.rb', line 26

def self.create_notification(id:, event:)
  # TODO This loops on all callbacks. Rethink this
  # method and implement it differently. Add listeners?
  callbacks.each do |callback|
    next unless callback[:on].include?(event)
    obj = model_class.constantize.find(id)
    notification = Dallal::Notification.new(event: event, model_class: model_class, opts: callback[:opts], _object: obj)
    notification.define_singleton_method(model_class.underscore) do
      obj
    end
    notification.instance_eval(&callback[:block])
    notification.dispatch!
  end
end

.inherited(base) ⇒ Object



7
8
9
10
11
12
# File 'lib/dallal/events/observer.rb', line 7

def self.inherited(base)
  model_name = base.name.gsub('Notifier', '')
  validate_model_exists!(model_name)

  NOTIFIABLES << model_name
end

.on(*args, &block) ⇒ Object



18
19
20
21
# File 'lib/dallal/events/observer.rb', line 18

def self.on *args, &block
  opts = args.extract_options!
  callbacks << { on: args, opts: opts, block: block }
end