after_commit_queue.rb 648 Bytes
Newer Older
1 2 3 4 5 6 7 8 9
module AfterCommitQueue
  extend ActiveSupport::Concern

  included do
    after_commit :_run_after_commit_queue
    after_rollback :_clear_after_commit_queue
  end

  def run_after_commit(method = nil, &block)
10
    _after_commit_queue << proc { self.send(method) } if method # rubocop:disable GitlabSecurity/PublicSend
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    _after_commit_queue << block if block
    true
  end

  protected

  def _run_after_commit_queue
    while action = _after_commit_queue.pop
      self.instance_eval(&action)
    end
  end

  def _after_commit_queue
    @after_commit_queue ||= []
  end

  def _clear_after_commit_queue
    _after_commit_queue.clear
  end
end