Class: MailyHerald::Log
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MailyHerald::Log
- Defined in:
- app/models/maily_herald/log.rb
Overview
Stores information about email delivery to entity.
It is associated with entity object and Dispatch. Log can have following statuses:
-
scheduled
- email hasn’t been processed yet, -
delivered
- email was sent to entity, -
skipped
- email deliver was skipped (i.e. due to conditions not met), -
error
- there was an error during email delivery.
Constant Summary collapse
- AVAILABLE_STATUSES =
[:scheduled, :delivered, :skipped, :error]
Instance Attribute Summary collapse
-
#data ⇒ Hash
Custom log data.
-
#entity_email ⇒ String
Delivery email.
-
#entity_id ⇒ Fixnum
Entity association id.
-
#entity_type ⇒ String
Entity association type.
-
#mail ⇒ Object
Contains ‘Mail::Message` object that was delivered.
-
#mailing_id ⇒ Fixnum
Dispatch association id.
-
#processing_at ⇒ DateTime
Timestamp of Dispatch processing.
-
#status ⇒ Sumbol
The current value of status.
Class Method Summary collapse
-
.create_for(mailing, entity, attributes = {}) ⇒ Object
Creates Log object for given Dispatch and entity.
Instance Method Summary collapse
-
#deliver(content) ⇒ Object
Set attributes of a schedule so it has ‘delivered’ status.
- #delivered? ⇒ Boolean
-
#error(msg) ⇒ Object
Set attributes of a schedule so it has ‘error’ status.
- #error? ⇒ Boolean
-
#postpone_delivery ⇒ Object
Set attributes of a schedule so it is postponed.
- #processed? ⇒ Boolean
- #scheduled? ⇒ Boolean
-
#set_attributes_for(mailing, entity, attributes = {}) ⇒ Object
Sets Log instance attributes.
-
#skip(reason) ⇒ Object
Set attributes of a schedule so it has ‘skipped’ status.
- #skipped? ⇒ Boolean
Instance Attribute Details
#data ⇒ Hash
Custom log data.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def data @data end |
#entity_email ⇒ String
Delivery email. Stored in case associated entity gets deleted.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def entity_email @entity_email end |
#entity_id ⇒ Fixnum
Entity association id.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def entity_id @entity_id end |
#entity_type ⇒ String
Entity association type.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def entity_type @entity_type end |
#mail ⇒ Object
Contains ‘Mail::Message` object that was delivered.
Present only in logs of state ‘delivered` and obtained via `Mailing.run` method.
49 50 51 |
# File 'app/models/maily_herald/log.rb', line 49 def mail @mail end |
#mailing_id ⇒ Fixnum
Dispatch association id.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def mailing_id @mailing_id end |
#processing_at ⇒ DateTime
Timestamp of Dispatch processing. Can be either future (when in scheduled
state) or past.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def processing_at @processing_at end |
#status ⇒ Sumbol
Returns the current value of status.
19 20 21 |
# File 'app/models/maily_herald/log.rb', line 19 def status @status end |
Class Method Details
.create_for(mailing, entity, attributes = {}) ⇒ Object
Creates Log object for given Dispatch and entity.
63 64 65 66 67 68 |
# File 'app/models/maily_herald/log.rb', line 63 def self.create_for mailing, entity, attributes = {} log = Log.new log.set_attributes_for mailing, entity, attributes log.save! log end |
Instance Method Details
#deliver(content) ⇒ Object
Set attributes of a schedule so it has ‘delivered’ status.
134 135 136 137 |
# File 'app/models/maily_herald/log.rb', line 134 def deliver content self.status = :delivered self.data[:content] = content end |
#delivered? ⇒ Boolean
92 93 94 |
# File 'app/models/maily_herald/log.rb', line 92 def delivered? self.status == :delivered end |
#error(msg) ⇒ Object
Set attributes of a schedule so it has ‘error’ status.
141 142 143 144 |
# File 'app/models/maily_herald/log.rb', line 141 def error msg self.status = :error self.data[:msg] = msg end |
#error? ⇒ Boolean
100 101 102 |
# File 'app/models/maily_herald/log.rb', line 100 def error? self.status == :error end |
#postpone_delivery ⇒ Object
Set attributes of a schedule so it is postponed.
122 123 124 125 126 127 128 129 130 |
# File 'app/models/maily_herald/log.rb', line 122 def postpone_delivery if !self.data[:delivery_attempts] || self.data[:delivery_attempts].length < 3 self.data[:original_processing_at] ||= self.processing_at self.data[:delivery_attempts] ||= [] self.data[:delivery_attempts].push(date_at: Time.now, action: :postpone, reason: :not_processable) self.processing_at = Time.now + 1.day true end end |
#processed? ⇒ Boolean
108 109 110 |
# File 'app/models/maily_herald/log.rb', line 108 def processed? [:delivered, :skipped, :error].include?(self.status) end |
#scheduled? ⇒ Boolean
104 105 106 |
# File 'app/models/maily_herald/log.rb', line 104 def scheduled? self.status == :scheduled end |
#set_attributes_for(mailing, entity, attributes = {}) ⇒ Object
Sets Log instance attributes.
78 79 80 81 82 83 84 85 86 |
# File 'app/models/maily_herald/log.rb', line 78 def set_attributes_for mailing, entity, attributes = {} self.mailing = mailing self.entity = entity self.entity_email = mailing.destination(entity) self.processing_at = attributes[:processing_at] || DateTime.now self.status = attributes[:status] self.data = attributes[:data] end |
#skip(reason) ⇒ Object
Set attributes of a schedule so it has ‘skipped’ status.
113 114 115 116 117 118 119 |
# File 'app/models/maily_herald/log.rb', line 113 def skip reason if self.status == :scheduled self.status = :skipped self.data[:skip_reason] = reason true end end |
#skipped? ⇒ Boolean
96 97 98 |
# File 'app/models/maily_herald/log.rb', line 96 def skipped? self.status == :skipped end |