File: README.md

package info (click to toggle)
ruby-after-commit-queue 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 296 kB
  • sloc: ruby: 217; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 2,003 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# after_commit_queue
---

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

### What problem does it solve ?

When using [state_machine](https://github.com/pluginaweek/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```

```ruby
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.

```ruby
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.