Logo: Relish

  1. Sign in

Project: Actionmailer-callbacks

around_create callback

As an actionmailer gem user
I want to be able to use the around_create callback
so that I can wrap the email creation process with my callback.

The around_create callback allows to wrap the email creation
process around another method definition.

The macro accepts the callback name as first arguments and an
optional hash with only and/or except keys that are
functionally equivalent to the ones of ApplicationController
before/after/around create filters: for example in order
to run a callback only for the "test" action you should
specify one of the following:

around_create :test_callback, only: :test
around_create :test_callback, only: [:test]
Scenarios
successful around_create calling
Given
the following mailer class with an around_create callback:
class TestMailer < ::ActionMailer::Base
  around_create :log_args

  def self.logger
    @logger ||= Array.new
  end

  def test(recipient)
    mail(
      to: recipient,
      from: '[email protected]',
      subject: 'Test Email'
    )
  end

  private

  def log_args
    params = args.flatten.to_sentence
    self.class.logger << "Test email now being called with #{params}"
    yield
    self.class.logger << "Test email was successfully created"
  end
end
When
I run the code "TestMailer.test('[email protected]')"
Then
an email should have been created
And
the logger for the class "TestMailer" should contain:
Test email now being called with test and [email protected]
And
the logger for the class "TestMailer" should contain:
Test email was successfully created
around_create callback skipped because not included in "only" directive
Given
the following mailer class with an around_create callback:
class TestMailer < ::ActionMailer::Base
  around_create :log_args, only: :only_method

  def self.logger
    @logger ||= Array.new
  end

  def test(recipient)
    mail(
      to: recipient,
      from: '[email protected]',
      subject: 'Test Email'
    )
  end

  private

  def log_args(*args)
    self.class.logger << "Test email now being called"
    yield
    self.class.logger << "Test email was successfully created"
  end
end
When
I run the code "TestMailer.test('[email protected]')"
Then
an email should have been created
And
the logger for the class "TestMailer" should be empty
around_create callback skipped because included in "except" directive
Given
the following mailer class with an around_create callback:
class TestMailer < ::ActionMailer::Base
  around_create :log_args, except: :test

  def self.logger
    @logger ||= Array.new
  end

  def test(recipient)
    mail(
      to: recipient,
      from: '[email protected]',
      subject: 'Test Email'
    )
  end

  private

  def log_args(*args)
    self.class.logger << "Test email now being called"
    yield
    self.class.logger << "Test email was successfully created"
  end
end
When
I run the code "TestMailer.test('[email protected]')"
Then
an email should have been created
And
the logger for the class "TestMailer" should be empty

Last published over 4 years ago by spaghetticode.