class Dallal::Events::Observer

Constants

NOTIFIABLES

Public Class Methods

callbacks() click to toggle source
# File lib/dallal/events/observer.rb, line 14
def self.callbacks
  @__notification_callbacks ||= []
end
create_notification(id:, event:) click to toggle source

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

# 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?
  # Change callbacks data structure
  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) click to toggle source
# 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) click to toggle source
# 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

Private Class Methods

model_class() click to toggle source
# File lib/dallal/events/observer.rb, line 45
def self.model_class
  self.name.gsub('Notifier', '')
end
validate_model_exists!(model) click to toggle source
# File lib/dallal/events/observer.rb, line 49
def self.validate_model_exists!(model)
  unless Object.const_defined?(model)
    # TODO add more descriptive error
    # also fix this
    # raise "Model #{model} is not defined."
  end
end