Actionmailer Callbacks

This gem adds before_create and around_create to ActionMailer::Base to make it work similarly to ActionController before/around filters and ActiveRecord::Base callbacks:
before_create :log_params, except: :test_email
around_create :benchmark, only: :test_emailexcept and only options are optional and has same functionality as in ActionController.
Requirements
The master branch now works only with Actionmailer 3.x, if you need to add callbacks to older versions please refer to the 0.x release.
Installation
Add the gem to the Gemfile:
gem 'actionmailer-callbacks'And then run bundle
Documentation
You can find some more documentation on the workings of the gem on relish: https://www.relishapp.com/spaghetticode/actionmailer-callbacks/docs
Notes
If you need something like before/after deliver callbacks ActionMailer 3.x comes ready for that: you can use an observer or an instrumentation for that.
around_create wraps the mail method execution (and all before_create callbacks). You can use them for rescuing from errors or for benchmarking, for example. There can be only one around_create method for each email method, if you register more than one only the first will be executed.
Example
class UserMailer < ActionMailer::Base
before_create :log_params
around_create :rescue_from_errors
def user_registration(user)
# this is a regular ActionMailer email method
end
private
def log_params(args)
MailerLogger.info "[CREATE] #{args.inspect}"
end
def rescue_from_errors
begin
yield
rescue
puts 'An error occured!'
end
end
endContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Add your feature tests to the rspec/cucumber test suite
- Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
In this section
Source: features/README.md at commit c11dd02d, Actionmailer Callbacks (MIT license).