Module: Alerter::Models::Notifiable

Extended by:
ActiveSupport::Concern
Defined in:
lib/alerter/models/notifiable.rb

Defined Under Namespace

Modules: ActiveRecordExtension

Instance Method Summary collapse

Instance Method Details

#configure_notification_methods(notification_type, methods) ⇒ Object

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



160
161
162
163
164
165
166
167
168
169
170
# 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

#mailboxObject

Gets the mailbox of the notifiable



50
51
52
# File 'lib/alerter/models/notifiable.rb', line 50

def mailbox
  @mailbox ||= Alerter::Mailbox.new(self)
end

#mark_as_deleted(obj, details = nil) ⇒ Object

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



118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object

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



138
139
140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

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



100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object

Get the notification preferences for a given notification_type



152
153
154
155
156
# 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) ⇒ Object

Sends a notification as originator



62
63
64
65
66
67
68
69
70
71
# 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_countObject

Get number of unread messages



55
56
57
# File 'lib/alerter/models/notifiable.rb', line 55

def unread_inbox_count
  mailbox.inbox(unread: true).count
end