module Alerter::Models::Notifiable

Public Instance Methods

configure_notification_methods(notification_type, methods) click to toggle source

configure methods for a given notification type methods can be an array of methods, a single method, or nil

# File lib/alerter/models/notifiable.rb, line 160
def configure_notification_methods(notification_type, methods)
  preference = preferences.find_or_create_by(notification_type: notification_type)
  if methods.is_a?(Array)
    preference.alert_methods = methods
  elsif methods.is_a?(String)
    preference.alert_methods = [ methods ]
  elsif methods.nil?
    preference.alert_methods = [ ]
  end
  preference.save
end
mailbox() click to toggle source

Gets the mailbox of the notifiable

# File lib/alerter/models/notifiable.rb, line 50
def mailbox
  @mailbox ||= Alerter::Mailbox.new(self)
end
mark_as_deleted(obj, details = nil) click to toggle source

Mark the object as deleted for notifiable.

Object can be:

  • A Receipt

  • A Message

  • An Array of these

Optionally pass in details of the deletion as String

# File lib/alerter/models/notifiable.rb, line 118
def mark_as_deleted(obj, details = nil)
  case obj
    when Receipt
      return obj.mark_as_deleted if obj.receiver == self
    when Message
      obj.mark_as_deleted(self)
    when Array
      obj.map{ |sub_obj| mark_as_deleted(sub_obj) }
    else
      return nil
  end
end
mark_as_not_deleted(obj, details = nil) click to toggle source

Mark the object as not deleted for notifiable.

Object can be:

  • A Receipt

  • A Message

  • An Array of these

Optionally pass in details of the deletion as String

# File lib/alerter/models/notifiable.rb, line 138
def mark_as_not_deleted(obj, details = nil)
  case obj
    when Receipt
      return obj.mark_as_not_deleted if obj.receiver == self
    when Message
      obj.mark_as_not_deleted(self)
    when Array
      obj.map{ |sub_obj| mark_as_not_deleted(sub_obj) }
    else
      return nil
  end
end
mark_as_read(obj, details = nil) click to toggle source

Mark the object as read for notifiable. Object can be:

  • A Receipt

  • A Message

  • An Array of these

Optionally pass in details of the read receipt as String

# File lib/alerter/models/notifiable.rb, line 81
def mark_as_read(obj, details = nil)
  case obj
    when Alerter::Receipt
      obj.mark_as_read if obj.receiver == self
    when Alerter::Message
      obj.mark_as_read(self)
    when Array
      obj.map{ |sub_obj| mark_as_read(sub_obj) }
  end

end
mark_as_unread(obj, details = nil) click to toggle source

Mark the object as unread for notifiable.

Object can be:

  • A Receipt

  • A Message

  • An Array of these

Optionally pass in details of the un-read receipt as String

# File lib/alerter/models/notifiable.rb, line 100
def mark_as_unread(obj, details = nil)
  case obj
    when Alerter::Receipt
      obj.mark_as_unread if obj.receiver == self
    when Alerter::Message
      obj.mark_as_unread(self)
    when Array
      obj.map{ |sub_obj| mark_as_unread(sub_obj) }
  end
end
notification_methods(notification_type) click to toggle source

Get the notification preferences for a given notification_type

# File lib/alerter/models/notifiable.rb, line 152
def notification_methods(notification_type)
  return [] unless notification_type.is_a?(Alerter::NotificationType)
  prefs = preferences.find_by(notification_type: notification_type).try(:alert_methods)
  prefs ||= []
end
send_message(short_msg, long_msg, notification_type_name, sanitize_text = true) click to toggle source

Sends a notification as originator

# File lib/alerter/models/notifiable.rb, line 62
def send_message(short_msg, long_msg, notification_type_name, sanitize_text = true)
  message = Alerter::MessageBuilder.new({
                                              :recipients        => self,
                                              :short_msg         => short_msg,
                                              :long_msg          => long_msg,
                                              :notification_type => NotificationType.find_or_create_by(name: notification_type_name),
                                          }).build
  message.save!
  message.deliver sanitize_text
end
unread_inbox_count() click to toggle source

Get number of unread messages

# File lib/alerter/models/notifiable.rb, line 55
def unread_inbox_count
  mailbox.inbox(unread: true).count
end