Class: Email

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SpreeMail::HasToken
Defined in:
app/model/email.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SpreeMail::HasToken

included

Class Method Details

.new(parameters = {}) ⇒ Object



47
48
49
50
# File 'app/model/email.rb', line 47

def new(parameters={})
  parameters ||= {}
  super(parameters.reverse_merge!(:body => template))
end

.templateObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/model/email.rb', line 52

def template
  txt=<<TXT
Hello {{name}},
  

  
Regards,

#{Spree::Config[:site_name]}

TXT
end

Instance Method Details

#deliver!Object



33
34
35
36
37
38
39
40
41
42
43
# File 'app/model/email.rb', line 33

def deliver!
  count = 0
  recipients.each do |email|
    subscriber = Subscriber.find_by_email(email) rescue nil
    if subscriber
      mail = EmailMailer.with_layout(self, subscriber)
      count += 1 if mail && mail.deliver!
    end
  end   
  return 0 < count, count
end

#fromObject



16
17
18
# File 'app/model/email.rb', line 16

def from
  MailMethod.current.preferred_mails_from rescue "[email protected]"
end

#recipient_listObject



25
26
27
# File 'app/model/email.rb', line 25

def recipient_list
  recipients.join(", ")
end

#recipientsObject



20
21
22
23
# File 'app/model/email.rb', line 20

def recipients
  hash = eval(read_attribute(:to)) rescue {}  
  hash.values  
end

#render(attribute, subscriber) ⇒ Object



29
30
31
# File 'app/model/email.rb', line 29

def render(attribute, subscriber)
  Mustache.render(self.send(attribute), subscriber.attributes)
end

#to=(value) ⇒ Object



9
10
11
12
13
14
# File 'app/model/email.rb', line 9

def to=(value)
  value = {} unless value.is_a? Hash
  value.delete("0")
  return false if value.empty?    
  write_attribute :to, value.inspect
end