File: after_commit_queue.rb

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 (36 lines) | stat: -rw-r--r-- 874 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
module AfterCommitQueue
  extend ActiveSupport::Concern

  included do
    after_commit :_run_after_commit_queue
    after_rollback :_clear_after_commit_queue
  end

  # Public: Add method to after commit queue
  def run_after_commit(method = nil, &block)
    _after_commit_queue << Proc.new { self.send(method) } if method
    _after_commit_queue << block if block
    true
  end

  protected

  # Protected: Is called as after_commit callback
  # runs methods from the queue and clears the queue afterwards
  def _run_after_commit_queue
    _after_commit_queue.each do |action|
      self.instance_eval &action
    end
    @after_commit_queue.clear
  end

  # Protected: Return after commit queue
  # Returns: Array with methods to run
  def _after_commit_queue
    @after_commit_queue ||= []
  end

  def _clear_after_commit_queue
    _after_commit_queue.clear
  end
end