Class: ActiveMailer::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/active_mailer/base.rb

Overview

:nodoc:

Defined Under Namespace

Classes: DefaultActionMailer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

leave this as an accessor for now since it’s complicated



13
14
15
# File 'app/models/active_mailer/base.rb', line 13

def body
  @body
end

#rendered_contentsObject

contains the actual sent email after send! is called



14
15
16
# File 'app/models/active_mailer/base.rb', line 14

def rendered_contents
  @rendered_contents
end

Class Method Details

.after_send(action) ⇒ Object



78
79
80
81
# File 'app/models/active_mailer/base.rb', line 78

def self.after_send(action)
  self.after_send_actions ||= []
  self.after_send_actions << action
end

.before_send(action) ⇒ Object



73
74
75
76
# File 'app/models/active_mailer/base.rb', line 73

def self.before_send(action)
  self.before_send_actions ||= []
  self.before_send_actions << action
end

.default_email_method_nameObject



191
192
193
# File 'app/models/active_mailer/base.rb', line 191

def default_email_method_name
  "#{self.name.underscore}"
end

.define_action_mailer_methodObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/active_mailer/base.rb', line 159

def define_action_mailer_method
  method_name = default_email_method_name
  return true if DefaultActionMailer.respond_to?(method_name)

  DefaultActionMailer.instance_eval do
    define_method(method_name) do |*args|
      options = args[0]
      options = options.with_indifferent_access
      attachments_to_set = (options[:attachments] || [])
      options.keys.each do |k|
        self.instance_eval("@#{k.to_s} = options[k]") if options[k]
#              instance_variable_set(k.to_s, options[k])
      end

      attachments_to_set.each do |att|
        attachment(
                   :content_type => (att[:content_type]  || att.content_type),
                   :body         => File.read(att[:path] || att.path),
                   :filename     => (att[:file_name]     || att.file_name)
                   )
      end

      mail_options = {
        :to      => options[:recipients],
        :subject => options[:subject],
        :from    => options[:sender].email_address }
      headers(options[:headers]) if options[:headers].present?
      mail(mail_options)
    end
  end
end

.layout(*args) ⇒ Object



141
142
143
# File 'app/models/active_mailer/base.rb', line 141

def self.layout(*args)
  DefaultActionMailer.send("layout", *args)
end

.mailer_variable(*variable_name) ⇒ Object



145
146
147
148
# File 'app/models/active_mailer/base.rb', line 145

def self.mailer_variable(*variable_name)
  self.mailer_variables =  Set.new(variable_name.map(&:to_s)) + (mailer_variables || [])
  attr_accessor *variable_name
end

Instance Method Details

#ar_recipients=Object



41
# File 'app/models/active_mailer/base.rb', line 41

alias :ar_recipients= :recipients=

#ar_sender=Object



32
# File 'app/models/active_mailer/base.rb', line 32

alias :ar_sender= :sender=

#do_after_sendObject



67
68
69
70
71
# File 'app/models/active_mailer/base.rb', line 67

def do_after_send
  self.after_send_actions.each do |m|
    self.send(m)
  end unless self.after_send_actions.blank?
end

#do_before_sendObject



61
62
63
64
65
# File 'app/models/active_mailer/base.rb', line 61

def do_before_send
  self.before_send_actions.each do |m|
    self.send(m)
  end unless self.before_send_actions.blank?
end

#mailer(reload = false) ⇒ Object



126
127
128
# File 'app/models/active_mailer/base.rb', line 126

def mailer(reload = false)
  @mailer ||= DefaultActionMailer.send("#{self.class.default_email_method_name}".to_sym, self.mailer_variables)
end

#mailer_variablesObject

cattr_accessor :template_variables

def self.template_variable(variable_name)
  self.template_variables ||= []
  self.template_variables << variable_name
  self.template_variables.flatten!
  attr_accessor variable_name
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/active_mailer/base.rb', line 100

def mailer_variables
  mvars = {}

  vars_to_include = self.class.mailer_variables + self.class.content_columns.map(&:name) + self.class.reflect_on_all_associations.map(&:name)

  vars_to_include.each do |var|
    mvars[var] = self.send(var.to_sym)
  end

  # TODO: this should be less ghetto
  mvars[:from] = self.sender.email_address unless mvars[:from]
  mvars[:recipients] = self.recipients.map(&:email_address) unless mvars[:recipients].all? {|r| r.is_a? String }

  mvars
end

#must_have_at_least_one_recipient_of_some_kindObject



21
22
23
24
25
# File 'app/models/active_mailer/base.rb', line 21

def must_have_at_least_one_recipient_of_some_kind
  if self.recipients.blank? and self.cc.blank? and self.bcc.blank?
    self.errors[:base] << "You have to have at least one recipient in the to, cc, or bcc fields"
  end
end

#recipients=(emails) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/active_mailer/base.rb', line 42

def recipients=(emails)
  emails = [emails] unless emails.is_a?(Array)
  emails.compact!
  self.ar_recipients = emails.map! do |email|
    if email.is_a?(String)
      EmailUser.find_or_create_by_email_address(email)
    else
      email
    end
  end
end

#render(*args) ⇒ Object



54
55
56
# File 'app/models/active_mailer/base.rb', line 54

def render(*args)
  ActionMailer::Base.send(:new).send(:render, *args)
end

#send!Object

should take false to avoid validations i.e. sending it again



130
131
132
133
134
135
136
137
138
139
# File 'app/models/active_mailer/base.rb', line 130

def send! # should take false to avoid validations i.e. sending it again
  if self.save!
    logger.info "sending email to #{self.recipients.join(", ")}"
    self.class.define_action_mailer_method
    sent_mail = mailer.deliver
    self.rendered_contents = sent_mail.body.to_s # in case someone wants to save it
    logger.info "email #{self.class.default_email_method_name} sent to #{self.recipients.map(&:email_address).join(", ")} from #{self.sender.email_address}"
    self.update_attribute("sent_at", Time.now)
  end
end

#sender=(email) ⇒ Object



33
34
35
36
37
38
39
# File 'app/models/active_mailer/base.rb', line 33

def sender=(email)
  if email.is_a?(String)
    self.ar_sender = EmailUser.find_or_create_by_email_address(email)
  else
    self.ar_sender = email
  end
end