Giter Club home page Giter Club logo

actionmailer-callbacks's Introduction

ActionMailer Callbacks

Build Status

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_email

except and only options are optional and has same functionality as in ActionController.

Callbacks functionality is implemented using the ActiveSupport::Callbacks module.

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.

The around_create callback wraps the entire mail method execution. You can use them for rescuing from errors or for benchmarking, for example.

The gem adds the args attribute accessor to the mail object in order to be able to access the arguments used for its initialization.

Example

  class UserMailer < ActionMailer::Base
    before_create :log_args
    around_create :rescue_from_errors

    def user_registration(user)
      # this is a regular ActionMailer email method
    end

    private

    def log_args
      MailerLogger.info "[CREATE] #{args.inspect}"
    end

    def rescue_from_errors
      begin
        yield
      rescue
        puts 'An error occured!'
      end
    end
  end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Add your feature tests to the rspec/cucumber test suite
  4. Commit your changes (git commit -am 'Added some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

actionmailer-callbacks's People

Contributors

spaghetticode avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.