Class: ActiveMailer::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActiveMailer::Base
show all
- Defined in:
- app/models/active_mailer/base.rb
Defined Under Namespace
Classes: DefaultActionMailer
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#body ⇒ Object
leave this as an accessor for now since it’s complicated
8
9
10
|
# File 'app/models/active_mailer/base.rb', line 8
def body
@body
end
|
#rendered_contents ⇒ Object
contains the actual sent email after ‘send!` is called
9
10
11
|
# File 'app/models/active_mailer/base.rb', line 9
def rendered_contents
@rendered_contents
end
|
Class Method Details
.after_send(action) ⇒ Object
73
74
75
76
|
# File 'app/models/active_mailer/base.rb', line 73
def self.after_send(action)
self.after_send_actions ||= []
self.after_send_actions << action
end
|
.before_send(action) ⇒ Object
68
69
70
71
|
# File 'app/models/active_mailer/base.rb', line 68
def self.before_send(action)
self.before_send_actions ||= []
self.before_send_actions << action
end
|
.default_email_method_name ⇒ Object
185
186
187
|
# File 'app/models/active_mailer/base.rb', line 185
def default_email_method_name
name.underscore
end
|
.define_action_mailer_method ⇒ Object
154
155
156
157
158
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
|
# File 'app/models/active_mailer/base.rb', line 154
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]
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 }
(options[:headers]) if options[:headers].present?
mail(mail_options)
end
end
end
|
.layout(*args) ⇒ Object
136
137
138
|
# File 'app/models/active_mailer/base.rb', line 136
def self.layout(*args)
DefaultActionMailer.send("layout", *args)
end
|
.mailer_variable(*variable_name) ⇒ Object
140
141
142
143
|
# File 'app/models/active_mailer/base.rb', line 140
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
36
|
# File 'app/models/active_mailer/base.rb', line 36
alias :ar_recipients= :recipients=
|
#ar_sender= ⇒ Object
27
|
# File 'app/models/active_mailer/base.rb', line 27
alias :ar_sender= :sender=
|
#do_after_send ⇒ Object
62
63
64
65
66
|
# File 'app/models/active_mailer/base.rb', line 62
def do_after_send
self.after_send_actions.each do |m|
self.send(m)
end unless self.after_send_actions.blank?
end
|
#do_before_send ⇒ Object
56
57
58
59
60
|
# File 'app/models/active_mailer/base.rb', line 56
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
121
122
123
|
# File 'app/models/active_mailer/base.rb', line 121
def mailer(reload = false)
@mailer ||= DefaultActionMailer.send("#{self.class.default_email_method_name}".to_sym, self.mailer_variables)
end
|
#mailer_variables ⇒ Object
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'app/models/active_mailer/base.rb', line 95
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
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_kind ⇒ Object
16
17
18
19
20
|
# File 'app/models/active_mailer/base.rb', line 16
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
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/models/active_mailer/base.rb', line 37
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.where(:email_address => email).first_or_create
else
email
end
end
end
|
#render(*args) ⇒ Object
49
50
51
|
# File 'app/models/active_mailer/base.rb', line 49
def render(*args)
ActionMailer::Base.send(:new).send(:render, *args)
end
|
#send! ⇒ Object
should take false to avoid validations i.e. sending it again
125
126
127
128
129
130
131
132
133
134
|
# File 'app/models/active_mailer/base.rb', line 125
def send! if self.save!
logger.info "sending email to #{self.recipients.join(", ")}"
self.class.define_action_mailer_method
sent_mail = mailer.respond_to?(:deliver_now) ? mailer.deliver_now : mailer.deliver
self.rendered_contents = sent_mail.body.to_s 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
28
29
30
31
32
33
34
|
# File 'app/models/active_mailer/base.rb', line 28
def sender=(email)
if email.is_a?(String)
self.ar_sender = EmailUser.where(:email_address => email).first_or_create
else
self.ar_sender = email
end
end
|