Giter Club home page Giter Club logo

after_commit's Introduction

after_commit

An ActiveRecord/Rails library to add before_commit, after_commit, before_rollback and after_rollback callbacks. These callbacks are focused on the transactions, instead of specific model actions. This is beneficial in situations where you are doing asynchronous processing and need committed objects.

Please note: Rails 3 (and thus, ActiveRecord 3) provides commit callbacks natively, so this library will remain just for versions 1.2.x to 2.x of ActiveRecord.

Installation

gem install after_commit

Usage

The following callbacks are provided:

  • before_commit
  • before_commit_on_create
  • before_commit_on_update
  • before_commit_on_destroy
  • after_commit
  • after_commit_on_create
  • after_commit_on_update
  • after_commit_on_destroy
  • before_rollback
  • after_rollback

You can use these just like you would any other callback:

class Article < ActiveRecord::Base
  after_commit :method_to_call_after_commit
  
  # ...
  
  private
  
  def method_to_call_after_commit
    # Do something knowing that the transaction is committed.
  end
end

In Tests

Keep in mind that transactions are finicky at best in tests, and so there’s a helper module to make after_commit play nicely in your testing context. You’ll need to add these two lines to your spec/test helper:

ActiveRecord::Base.send(:include, AfterCommit::AfterSavepoint)
ActiveRecord::Base.include_after_savepoint_extensions

Credits

This code first appeared in a blog post by Eli Miller, and was then included in Thinking Sphinx by Pat Allan, with modifications from Joost Hietbrink. The code was then put on GitHub as a plugin by Nick Muerdter, and many people forked and added their own contributions.

This version (maintained by Pat Allan) includes the following patches:

  • Callbacks for specific types of actions (create, update, destroy) (DeLynn Berry)
  • Fixes to extra callbacks (Xavier Shay)
  • Thread-safety (Dmitry Galinsky)
  • after_rollback callback (Justin Balthrop)
  • Test environment fix (Pivotal Labs)
  • Scoping callbacks to specific connections (Mat Brown)
  • before_* callbacks (Trotter Cashion)
  • Gemspec and extended tests, works as a plugin, doesn’t load ActiveRecord hooks twice, doesn’t add Test environment hook automatically. (David Yip)
  • Exception propagation (George Ogata)
  • Keeping callbacks focused on the correct transactions (Benjamin Stein)
  • Using savepoints for test helper (Lars Klevan and Joel Chippindale)
  • Only tracking objects from classes that have appropriate callbacks; Adding after/before_commit_on_save to cover creates and updates but not destroys (Jason Weathered)
  • Fixing up exception handling and savepoints in the test helper, and cleaning up successful transactions correctly (Identified).
  • Improved JRuby support (Kevin Menard)
  • Stop transaction failures in tests (Ochko)

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.