Git Product home page Git Product logo

after_commit_queue's Introduction

after_commit_queue


Rails plugin which allows to run callbacks after database transaction is committed.

What problem does it solve ?

When using state_machine it's hard to run a callback on event after the transaction is committed.

after_commit_queue plugin addresses this problem.

Installation

Add this to your Gemfile and run bundle install

gem 'after_commit_queue'

Usage

Include AfterCommitQueue module in your ActiveRecord model and you're ready to go. When registering a hook with run_after_commit you can supply either a method symbol or a block. No parameter is supplied when using the block form.

class Server < ActiveRecord::Base
  attr_accessor :started, :stopped

  # include plugin
  include AfterCommitQueue

  state_machine :state, :initial => :pending do
    after_transition :pending => :running, :do => :schedule_start
    after_transition :running => :turned_off, :do => :schedule_stop
    event(:start) { transition :pending => :running }
    event(:stop) { transition :running => :turned_off }
  end

  def schedule_start
    # Adds method to be run after transaction is committed
    run_after_commit(:start_server)
  end

  def schedule_stop
    run_after_commit do
      stop_server
    end
  end

  def start_server; @started = true end
  def stop_server; @stopped = true end
end

Contributions

To fetch & test the library for development, do:

$ git clone https://github.com/Ragnarson/after_commit_queue
$ cd after_commit_queue
$ bundle

Running tests

# Before each test run, the database will be created and migrated automatically
$ bundle exec rake test

If you want to contribute, please:

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Send me a pull request on Github.

This project rocks and uses MIT-LICENSE.

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.