Class: MandrillMailer::CoreMailer

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_mailer/core_mailer.rb

Direct Known Subclasses

MessageMailer, TemplateMailer

Defined Under Namespace

Classes: InvalidEmail, InvalidInterceptorParams, InvalidMailerMethod

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/mandrill_mailer/core_mailer.rb', line 287

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
    options = args.extract_options!.merge({host: MandrillMailer.config.default_url_options[:host], protocol: MandrillMailer.config.default_url_options[:protocol]})
    args << options
    Rails.application.routes.url_helpers.method(method).call(*args)
  end
end

Class Attribute Details

.defaultsObject

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

Examples

default from: '[email protected]'

Returns options



134
135
136
# File 'lib/mandrill_mailer/core_mailer.rb', line 134

def self.defaults
  @defaults || super_defaults
end

Instance Attribute Details

#asyncObject

Public: Enable background sending mode



116
117
118
# File 'lib/mandrill_mailer/core_mailer.rb', line 116

def async
  @async
end

#ip_poolObject

Public: Name of the dedicated IP pool that should be used to send the message



119
120
121
# File 'lib/mandrill_mailer/core_mailer.rb', line 119

def ip_pool
  @ip_pool
end

#messageObject

Public: Other information on the message to send



113
114
115
# File 'lib/mandrill_mailer/core_mailer.rb', line 113

def message
  @message
end

#send_atObject

Public: When message should be sent



122
123
124
# File 'lib/mandrill_mailer/core_mailer.rb', line 122

def send_at
  @send_at
end

Class Method Details

.default(args) ⇒ Object



142
143
144
145
146
# File 'lib/mandrill_mailer/core_mailer.rb', line 142

def self.default(args)
  @defaults ||= {}
  @defaults[:from] ||= '[email protected]'
  @defaults.merge!(args)
end

.super_defaultsObject



138
139
140
# File 'lib/mandrill_mailer/core_mailer.rb', line 138

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.



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/mandrill_mailer/core_mailer.rb', line 189

def self.test(mailer_method, options={})
  unless options[: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, options)
  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, options|
  invitation = OpenStruct.new({
    email: options[:email],
    owner_name: 'foobar',
    secret: rand(9000000..1000000).to_s
  })
  mailer.invite(invitation).deliver
end

Returns the duplicated String.



172
173
174
175
# File 'lib/mandrill_mailer/core_mailer.rb', line 172

def self.test_setup_for(mailer_method, &block)
  @mailer_methods ||= {}
  @mailer_methods[mailer_method] = block
end

Instance Method Details

#bccObject



251
252
253
# File 'lib/mandrill_mailer/core_mailer.rb', line 251

def bcc
  self.message && self.message['bcc_address']
end

#check_required_optionsObject

Raises:

  • (NotImplementedError)


234
235
236
237
# File 'lib/mandrill_mailer/core_mailer.rb', line 234

def check_required_options
  mesg = "#{self.class.name}#check_required_options() is not implemented."
  raise NotImplementedError.new(mesg)
end

#dataObject

Public: Data hash (deprecated)

Raises:

  • (NotImplementedError)


229
230
231
232
# File 'lib/mandrill_mailer/core_mailer.rb', line 229

def data
  mesg = "#{self.class.name}#data() is not implemented."
  raise NotImplementedError.new(mesg)
end

#deliverObject

Public: Triggers the stored Mandrill params to be sent to the Mandrill api

Raises:

  • (NotImplementedError)


203
204
205
206
# File 'lib/mandrill_mailer/core_mailer.rb', line 203

def deliver
  mesg = "#{self.class.name}#deliver() is not implemented."
  raise NotImplementedError.new(mesg)
end

#fromObject



239
240
241
# File 'lib/mandrill_mailer/core_mailer.rb', line 239

def from
  self.message && self.message['from_email']
end

#mandrill_mail(args) ⇒ Object

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)

Raises:

  • (NotImplementedError)


223
224
225
226
# File 'lib/mandrill_mailer/core_mailer.rb', line 223

def mandrill_mail(args)
  mesg = "#{self.class.name}#mandrill_mail() is not implemented."
  raise NotImplementedError.new(mesg)
end

#toObject



243
244
245
# File 'lib/mandrill_mailer/core_mailer.rb', line 243

def to
  self.message && self.message['to']
end

#to=(values) ⇒ Object



247
248
249
# File 'lib/mandrill_mailer/core_mailer.rb', line 247

def to=(values)
  self.message && self.message['to'] = format_to_params(values)
end