Method: Notifun::Notification.notify

Defined in:
lib/notifun/notification.rb

.notify(models, key, merge_hash = {}, options = {}) ⇒ Object

model passed in must have following methods: notifun_uuid or uuid: unique identifier notifun_email or email: email to send to notifun_notify_via or notify_via(method) returns if it should send message using one of the message types: [“email”, “push”, “text”]



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/notifun/notification.rb', line 6

def self.notify(models, key, merge_hash={}, options={})
  message_template = Notifun::MessageTemplate.find_by_key(key)
  raise "Unable to find message_template with key #{key}" if message_template.nil?
  models = [models].flatten
  status = true
  models.each do |model|
    primary_sent = false
    backup_sent = false
    preference = Notifun::Preference.where(preferable: model).where(message_template_key: key).first
    if message_template.category.present?
      preference ||= Notifun::Preference.where(preferable: model).where(category: message_template.category).first
    end
    if preference
      sent = false
      preference.notification_methods.each do |notification_method|
        next if options[:notification_methods] && !options[:notification_methods].include?(notification_method)
        sent = self.send("send_via_#{notification_method}", message_template, model, merge_hash, options)
      end

      return sent
    else
      message_template.default_notification_methods.each do |notification_method|
        next if options[:notification_methods] && !options[:notification_methods].include?(notification_method)
        notify_via = model.try(:notifun_notify_via, notification_method)
        notify_via = model.notify_via(notification_method) if notify_via.nil?
        if notify_via
          if self.send("send_via_#{notification_method}", message_template, model, merge_hash, options)
            primary_sent = true
          end
        end
      end
      if !primary_sent
        message_template.backup_notification_methods.each do |notification_method|
          next if options[:notification_methods] && !options[:notification_methods].include?(notification_method)
          notify_via = model.try(:notifun_notify_via, notification_method)
          notify_via = model.notify_via(notification_method) if notify_via.nil?
          if notify_via
            if self.send("send_via_#{notification_method}", message_template, model, merge_hash, options)
              backup_sent = true
              break
            end
          end
        end
      end

      if status == true
        status = primary_sent || backup_sent
      end
    end
  end

  return status
end