before_create callback
As an actionmailer gem user I want to be able to use the before_create callback so that I can to execute code before creating an email
The before_create callback allows to execute a callback before the email creation process. 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:
before_create :test_callback, only: :test
before_create :test_callback, only: [:test]Scenario: successful before_create callback calling
- Given the following mailer class with a before_create callback:
ruby
class TestMailer < ::ActionMailer::Base before_create :log_args def self.logger @logger ||= Array.new end def test(recipient) mail( to: recipient, from: 'sender@test.com', subject: 'Test Email' ) end private def log_args self.class.logger << "Test was called with #{args.to_sentence}" end end - When I run the code "
TestMailer.test('recipient@test.com').deliver" - Then an email should have been sent
- And the logger for the class "
TestMailer" should contain:textTest was called with test and recipient@test.com
Scenario: before_create callback skipped because not included in "only" directive
- Given the following mailer class with a before_create callback:
ruby
class TestMailer < ::ActionMailer::Base before_create :log_args, only: :only_method def self.logger @logger ||= Array.new end def test(recipient) mail( to: recipient, from: 'sender@test.com', subject: 'Test Email' ) end private def log_args self.class.logger << "Test was called with #{args.to_sentence}" end end - When I run the code "
TestMailer.test('recipient@test.com').deliver" - Then an email should have been sent
- And the logger for the class "
TestMailer" should be empty
Scenario: before_create callback skipped because included in "except" directive
- Given the following mailer class with a before_create callback:
ruby
class TestMailer < ::ActionMailer::Base before_create :log_args, except: :test def self.logger @logger ||= Array.new end def test(recipient) mail( to: recipient, from: 'sender@test.com', subject: 'Test Email' ) end private def log_args self.class.logger << "Test was called with #{args.to_sentence}" end end - When I run the code "
TestMailer.test('recipient@test.com').deliver" - Then an email should have been sent
- And the logger for the class "
TestMailer" should be empty
Source: features/before_create_callback.feature at commit c11dd02d, Actionmailer Callbacks (MIT license).