Class: MailyHerald::Log

Inherits:
ActiveRecord::Base
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataHash

Custom log data.

Returns:

  • (Hash)

    the current value of data



19
20
21
# File 'app/models/maily_herald/log.rb', line 19

def data
  @data
end

#entity_emailString

Delivery email. Stored in case associated entity gets deleted.

Returns:

  • (String)

    the current value of entity_email



19
20
21
# File 'app/models/maily_herald/log.rb', line 19

def entity_email
  @entity_email
end

#entity_idFixnum

Entity association id.

Returns:

  • (Fixnum)

    the current value of entity_id



19
20
21
# File 'app/models/maily_herald/log.rb', line 19

def entity_id
  @entity_id
end

#entity_typeString

Entity association type.

Returns:

  • (String)

    the current value of entity_type



19
20
21
# File 'app/models/maily_herald/log.rb', line 19

def entity_type
  @entity_type
end

#mailObject

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_idFixnum

Dispatch association id.

Returns:

  • (Fixnum)

    the current value of mailing_id



19
20
21
# File 'app/models/maily_herald/log.rb', line 19

def mailing_id
  @mailing_id
end

#processing_atDateTime

Timestamp of Dispatch processing. Can be either future (when in scheduled state) or past.

Returns:

  • (DateTime)

    the current value of processing_at



19
20
21
# File 'app/models/maily_herald/log.rb', line 19

def processing_at
  @processing_at
end

#statusSumbol

Returns the current value of status.

Returns:

  • (Sumbol)

    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.

Parameters:

  • mailing (Dispatch)
  • entity (ActiveRecord::Base)
  • attributes (Hash) (defaults to: {})

    log attributes

Options Hash (attributes):

  • :processing_at (Time) — default: DateTime.now
  • :status (Symbol)
  • :data (Hash)


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.

Parameters:

  • content

    Body of delivered email.



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

Returns:

  • (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.

Parameters:

  • msg

    Error description.



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

Returns:

  • (Boolean)


100
101
102
# File 'app/models/maily_herald/log.rb', line 100

def error?
  self.status == :error
end

#postpone_deliveryObject

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

Returns:

  • (Boolean)


108
109
110
# File 'app/models/maily_herald/log.rb', line 108

def processed?
  [:delivered, :skipped, :error].include?(self.status)
end

#scheduled?Boolean

Returns:

  • (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.

Parameters:

  • mailing (Dispatch)
  • entity (ActiveRecord::Base)
  • attributes (Hash) (defaults to: {})

    log attributes

Options Hash (attributes):

  • :processing_at (Time) — default: DateTime.now
  • :status (Symbol)
  • :data (Hash)


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

Returns:

  • (Boolean)


96
97
98
# File 'app/models/maily_herald/log.rb', line 96

def skipped?
  self.status == :skipped
end