Class: MailyHerald::AdHocMailing

Inherits:
Mailing show all
Defined in:
app/models/maily_herald/ad_hoc_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

#runObject

Sends mailing to all subscribed entities who have delivery scheduled.

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 47

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_delivery_to(entity, time = Time.now) ⇒ Object

Schedules mailing delivery to `entity` at given `time`.

This always creates new Log object of type `schedule`.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 23

def schedule_delivery_to entity, time = Time.now
  subscribed = self.list.subscribed?(entity)

  if !enabled? || !(self.override_subscription? || subscribed)
    return
  end

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

#schedule_delivery_to_all(time = Time.now) ⇒ Object

Schedules mailing delivery to all entities in the scope at given `time`.

This always creates new Log objects of type `schedule`.



10
11
12
13
14
15
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 10

def schedule_delivery_to_all time = Time.now
  self.list.context.scope_with_subscription(self.list, :outer).each do |entity|
    MailyHerald.logger.debug "Adding schedule of #{self} ad-hoc for entity ##{entity.id} #{entity}"
    schedule_delivery_to entity, time
  end
end

#schedule_for(entity) ⇒ Object

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



78
79
80
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 78

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

#schedulesObject

Returns collection of all delivery schedules (Log collection).



83
84
85
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 83

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”.



67
68
69
70
71
72
73
74
75
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 67

def set_schedule_for entity
  subscribed = self.list.subscribed?(entity)

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

#to_sObject



87
88
89
# File 'app/models/maily_herald/ad_hoc_mailing.rb', line 87

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