Class: AlchemyCrm::Mailing

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

Constant Summary collapse

MAILING_PAGE_LAYOUT_PREFIX =
"newsletter_layout_"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.copy(id) ⇒ Object

Makes a copy of another mailing.



56
57
58
59
60
61
# File 'app/models/alchemy_crm/mailing.rb', line 56

def self.copy(id)
  source = self.find(id)
  new(source.attributes.merge(
    "name" => "#{source.name} (#{::I18n.t(:copy, :scope => :alchemy_crm, :default => 'Copy')})"
  ).except("id", "updated_at", "created_at", "sha1", "salt", "page_id"))
end

Instance Method Details

#additional_emailsObject

Return a list of email addresses from additional_email_addresses field.



45
46
47
48
# File 'app/models/alchemy_crm/mailing.rb', line 45

def additional_emails
  return [] if additional_email_addresses.blank?
  additional_email_addresses.gsub(/\s/,'').split(',').uniq
end

#contactsObject

Returns all contacts found via newsletter contacts and additional email addresses.



25
26
27
# File 'app/models/alchemy_crm/mailing.rb', line 25

def contacts
  (newsletter_contacts + contacts_from_additional_email_addresses).uniq
end

#contacts_countObject



29
30
31
32
# File 'app/models/alchemy_crm/mailing.rb', line 29

def contacts_count
  return 0 if contacts.blank?
  contacts.count
end

#contacts_from_additional_email_addressesObject

Returns a list of contacts found or initialized by email address from additional_email_addresses field.



51
52
53
# File 'app/models/alchemy_crm/mailing.rb', line 51

def contacts_from_additional_email_addresses
  additional_emails.collect{ |email| Contact.find_or_initialize_by_email(:email => email) }
end

#contacts_not_having_email_yetObject



34
35
36
37
# File 'app/models/alchemy_crm/mailing.rb', line 34

def contacts_not_having_email_yet
  return contacts if recipients.empty?
  contacts.select { |c| !recipients.collect(&:email).include?(c.email) }
end

#emailsObject

Returns a list of all email addresses for contacts that have not got any email yet.



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

def emails
  contacts_not_having_email_yet.collect(&:email)
end

#newsletter_contactsObject

Returns all contacts found via newsletter.



19
20
21
22
# File 'app/models/alchemy_crm/mailing.rb', line 19

def newsletter_contacts
  return [] if newsletter.blank?
  newsletter.contacts
end

#next_pending_deliveryObject



63
64
65
# File 'app/models/alchemy_crm/mailing.rb', line 63

def next_pending_delivery
  deliveries.pending.first
end