Class: MandrillMailer::CoreMailer
- Inherits:
-
Object
- Object
- MandrillMailer::CoreMailer
- Defined in:
- lib/mandrill_mailer/core_mailer.rb
Direct Known Subclasses
Defined Under Namespace
Classes: InvalidEmail, InvalidInterceptorParams, InvalidMailerMethod, InvalidMergeLanguageError
Class Attribute Summary collapse
-
.defaults ⇒ Object
Public: Defaults for the mailer.
Instance Attribute Summary collapse
-
#async ⇒ Object
Public: Enable background sending mode.
-
#ip_pool ⇒ Object
Public: Name of the dedicated IP pool that should be used to send the message.
-
#message ⇒ Object
Public: Other information on the message to send.
-
#send_at ⇒ Object
Public: When message should be sent.
Class Method Summary collapse
- .default(args) ⇒ Object
- .super_defaults ⇒ Object
-
.test(mailer_method, options = {}) ⇒ Object
Public: Executes a test email.
-
.test_setup_for(mailer_method, &block) ⇒ Object
Public: setup a way to test mailer methods.
Instance Method Summary collapse
- #bcc ⇒ Object
-
#deliver ⇒ Object
Public: Triggers the stored Mandrill params to be sent to the Mandrill api.
- #from ⇒ Object
-
#mandrill_mail(args) ⇒ Object
Public: Build the hash needed to send to the mandrill api.
- #mandrill_mail_handler(args) ⇒ Object
- #to ⇒ Object
- #to=(values) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
Proxy route helpers to rails if Rails exists. Doing routes this way makes it so this gem doesn’t need to be a rails engine
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 297 def method_missing(method, *args) return super unless defined?(Rails) && Rails.application.routes.url_helpers.respond_to?(method) # Check to see if one of the args is an open struct. If it is, we'll assume it's the # test stub and try to call a path or url attribute. if args.any? {|arg| arg.kind_of?(MandrillMailer::Mock)} # take the first OpenStruct found in args and look for .url or.path args.each do |arg| if arg.kind_of?(MandrillMailer::Mock) break arg.url || arg.path end end else = args..merge({host: MandrillMailer.config.[:host], protocol: MandrillMailer.config.[:protocol]}) args << Rails.application.routes.url_helpers.method(method).call(*args) end end |
Class Attribute Details
.defaults ⇒ Object
Public: Defaults for the mailer. Currently the only option is from:
options - The Hash options used to refine the selection (default: {}):
:from - Default from email address
:from_name - Default from name
:merge_vars - Default merge vars
Examples
default from: '[email protected]',
from_name: 'Foo Bar',
merge_vars: {'FOO' => 'Bar'}
Returns options
144 145 146 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 144 def self.defaults @defaults || super_defaults || {} end |
Instance Attribute Details
#async ⇒ Object
Public: Enable background sending mode
121 122 123 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 121 def async @async end |
#ip_pool ⇒ Object
Public: Name of the dedicated IP pool that should be used to send the message
124 125 126 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 124 def ip_pool @ip_pool end |
#message ⇒ Object
Public: Other information on the message to send
118 119 120 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 118 def end |
#send_at ⇒ Object
Public: When message should be sent
127 128 129 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 127 def send_at @send_at end |
Class Method Details
.default(args) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 152 def self.default(args) @defaults ||= {} @defaults[:from] ||= '[email protected]' @defaults[:merge_vars] ||= {} @defaults.merge!(args) end |
.super_defaults ⇒ Object
148 149 150 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 148 def self.super_defaults superclass.defaults if superclass.respond_to?(:defaults) end |
.test(mailer_method, options = {}) ⇒ Object
Public: Executes a test email
mailer_method - Method to execute
options - The Hash options used to refine the selection (default: {}):
:email - The email to send the test to.
Examples
InvitationMailer.test(:invite, email: '[email protected]')
Returns the duplicated String.
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 200 def self.test(mailer_method, ={}) unless [:email] raise InvalidEmail.new 'Please specify a :email option(email to send the test to)' end if @mailer_methods[mailer_method] @mailer_methods[mailer_method].call(self.new, ) else raise InvalidMailerMethod.new "The mailer method: #{mailer_method} does not have test setup" end end |
.test_setup_for(mailer_method, &block) ⇒ Object
Public: setup a way to test mailer methods
mailer_method - Name of the mailer method the test setup is for
block - Block of code to execute to perform the test. The mailer and options are passed to the block. The options have to contain at least the :email to send the test to.
Examples
test_setup_for :invite do |mailer, |
invitation = OpenStruct.new({
email: [:email],
owner_name: 'foobar',
secret: rand(9000000..1000000).to_s
})
mailer.invite(invitation).deliver
end
Returns the duplicated String.
183 184 185 186 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 183 def self.test_setup_for(mailer_method, &block) @mailer_methods ||= {} @mailer_methods[mailer_method] = block end |
Instance Method Details
#bcc ⇒ Object
266 267 268 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 266 def bcc self. && self.['bcc_address'] end |
#deliver ⇒ Object
Public: Triggers the stored Mandrill params to be sent to the Mandrill api
214 215 216 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 214 def deliver raise NotImplementedError.new("#{self.class.name}#deliver is not implemented.") end |
#from ⇒ Object
254 255 256 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 254 def from self. && self.['from_email'] end |
#mandrill_mail(args) ⇒ Object
Public: Build the hash needed to send to the mandrill api
args - The Hash options used to refine the selection:
Examples
mandrill_mail template: 'Group Invite',
subject: I18n.t('invitation_mailer.invite.subject'),
to: invitation.email,
vars: {
'OWNER_NAME' => invitation.owner_name,
'INVITATION_URL' => new_invitation_url(email: invitation.email, secret: invitation.secret)
}
Returns the the mandrill mailer class (this is so you can chain #deliver like a normal mailer)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 238 def mandrill_mail(args) (args) # Call the mandrill_mail_handler so mailers can handle the args in custom ways mandrill_mail_handler(args) # Construct message hash self. = MandrillMailer::ArgFormatter.(args, self.class.defaults) # Apply any interceptors that may be present apply_interceptors!(self.) # return self so we can chain deliver after the method call, like a normal mailer. self end |
#mandrill_mail_handler(args) ⇒ Object
219 220 221 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 219 def mandrill_mail_handler(args) args end |
#to ⇒ Object
258 259 260 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 258 def to self. && self.['to'] end |
#to=(values) ⇒ Object
262 263 264 |
# File 'lib/mandrill_mailer/core_mailer.rb', line 262 def to=(values) self. && self.['to'] = MandrillMailer::ArgFormatter.params(values) end |