Class: AlchemyCrm::Delivery

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/alchemy_crm/delivery.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chunk_delayObject

Returns the value of attribute chunk_delay.



5
6
7
# File 'app/models/alchemy_crm/delivery.rb', line 5

def chunk_delay
  @chunk_delay
end

Class Method Details

.settings(name) ⇒ Object



40
41
42
# File 'app/models/alchemy_crm/delivery.rb', line 40

def self.settings(name)
  AlchemyCrm::Config.get(name)
end

Instance Method Details

#delivered?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/models/alchemy_crm/delivery.rb', line 15

def delivered?
  !self.delivered_at.nil?
end

#send_chunks(options = {}) ⇒ Object

Sending mails in chunks.



20
21
22
23
24
25
26
27
28
# File 'app/models/alchemy_crm/delivery.rb', line 20

def send_chunks(options={})
  raise "No Mailing Given" if mailing.blank?
  @chunk_delay = 0
  recipients.each_slice(self.class.settings(:send_mails_in_chunks_of)) do |recipients_chunk|
    send_mail_chunk(recipients_chunk, options)
    @chunk_delay += 1
  end
  update_attribute(:delivered_at, Time.now)
end

#send_mail_chunk(recipients_chunk, options) ⇒ Object

Send the mail chunk via delayed_job and waiting some time before next is enqueued



32
33
34
35
36
37
# File 'app/models/alchemy_crm/delivery.rb', line 32

def send_mail_chunk(recipients_chunk, options)
  recipients_chunk.each do |recipient|
    mail = MailingsMailer.build(mailing, recipient, options).deliver
    recipient.update_attribute(:message_id, mail.message_id)
  end
end