Class: MailyHerald::OneTimeMailing

Inherits:
Mailing show all
Defined in:
app/models/maily_herald/one_time_mailing.rb

Instance Attribute Summary

Attributes inherited from Dispatch

#absolute_delay, #conditions, #from, #list_id, #mailer_name, #name, #override_subscription, #period, #sequence_id, #state, #subject, #template, #title, #type

Instance Method Summary collapse

Methods inherited from Mailing

#ad_hoc?, #build_mail, #conditions, #conditions=, #conditions_changed?, #conditions_met?, #destination, #general_scheduling?, #generic_mailer?, #has_conditions?, #has_conditions_proc?, #individual_scheduling?, #mailer, #mailer_name, #one_time?, #periodical?, #render_subject, #render_template, #sequence?, #test_conditions

Methods included from Autonaming

included

Methods included from TemplateRenderer

included

Methods inherited from Dispatch

#archive, #archive!, #archived?, #disable, #disable!, #disabled?, #enable, #enable!, #enabled?, #has_start_at_proc?, #in_scope?, #list=, #locked?, #processable?, #start_at, #start_at=, #start_at_changed?, #subscription_valid?

Instance Method Details

#processed_logs(entity) ⇒ Object

Returns collection of processed Logs for given entity.

[View source]

31
32
33
# File 'app/models/maily_herald/one_time_mailing.rb', line 31

def processed_logs entity
  Log.ordered.for_entity(entity).for_mailing(self).processed
end

#runObject

Sends mailing to all subscribed entities.

Performs actual sending of emails; should be called in background.

Returns array of Log with actual ‘Mail::Message` objects stored in Log.mail attributes.

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/maily_herald/one_time_mailing.rb', line 15

def run
  # TODO better scope here to exclude schedules for users outside context scope
  schedules.where("processing_at <= (?)", Time.now).collect do |schedule|
    if schedule.entity
      mail = deliver schedule
      schedule.reload
      schedule.mail = mail
      schedule
    else
      MailyHerald.logger.log_processing(schedule.mailing, {class: schedule.entity_type, id: schedule.entity_id}, prefix: "Removing schedule for non-existing entity") 
      schedule.destroy
    end
  end
end

#schedule_for(entity) ⇒ Object

Returns Log object which is the delivery schedule for given entity.

[View source]

80
81
82
# File 'app/models/maily_herald/one_time_mailing.rb', line 80

def schedule_for entity
  schedules.for_entity(entity).first
end

#schedulesObject

Returns collection of all delivery schedules (Log collection).

[View source]

85
86
87
# File 'app/models/maily_herald/one_time_mailing.rb', line 85

def schedules
  Log.ordered.scheduled.for_mailing(self)
end

#set_schedule_for(entity) ⇒ Object

Sets the delivery schedule for given entity

New schedule will be created or existing one updated. Schedule is Log object of type “schedule”.

[View source]

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/maily_herald/one_time_mailing.rb', line 39

def set_schedule_for entity
  if processed_logs(entity).last
    # this mailing is sent only once
    log = schedule_for(entity)
    log.try(:destroy)
    return
  end

  subscribed = self.list.subscribed?(entity)
  start_time = start_processing_time(entity)

  if !self.start_at || !enabled? || !start_time || !(self.override_subscription? || subscribed)
    log = schedule_for(entity)
    log.try(:destroy)
    return
  end

  log = schedule_for(entity)

  log ||= Log.new
  log.with_lock do
    log.set_attributes_for(self, entity, {
      status: :scheduled,
      processing_at: start_time,
    })
    log.save!
  end
  log
end

#set_schedulesObject

Sets delivery schedules of all entities in mailing scope.

New schedules will be created or existing ones updated.

[View source]

72
73
74
75
76
77
# File 'app/models/maily_herald/one_time_mailing.rb', line 72

def set_schedules
  self.list.context.scope_with_subscription(self.list, :outer).each do |entity|
    MailyHerald.logger.debug "Updating schedule of #{self} one-time for entity ##{entity.id} #{entity}"
    set_schedule_for entity
  end
end

#start_processing_time(entity) ⇒ Object

Returns processing time for given entity.

This is the time when next mailing should be sent based on Dispatch#start_at mailing attribute.

[View source]

93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/maily_herald/one_time_mailing.rb', line 93

def start_processing_time entity
  subscription = self.list.subscription_for(entity)

  if has_start_at_proc?
    start_at.call(entity, subscription)
  else
    evaluator = Utils::MarkupEvaluator.new(self.list.context.drop_for(entity, subscription))

    evaluator.evaluate_start_at(self.start_at)
  end
end

#to_sObject

[View source]

105
106
107
# File 'app/models/maily_herald/one_time_mailing.rb', line 105

def to_s
  "<OneTimeMailing: #{self.title || self.name}>"
end