Module: AfterCommitHelper::Concern

Extended by:
ActiveSupport::Concern
Defined in:
lib/after_commit_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_after_commit_callback(&callback) ⇒ Object

Add a callback to be fired once this record commits



21
22
23
24
# File 'lib/after_commit_helper.rb', line 21

def add_after_commit_callback(&callback)
  after_commit_callbacks << callback
  AfterCommitHelper::Logger.logger.debug 'callback added for after_commit hook'
end

#after_commit_callbacksObject

returns the after commit callbacks to be executed



11
12
13
# File 'lib/after_commit_helper.rb', line 11

def after_commit_callbacks
  @after_commit_callbacks ||= []
end

#process_after_commit_callbacksObject

Fires all of the procs one at a time, any exceptions will be rescued and logged, but will not be rethrown



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/after_commit_helper.rb', line 28

def process_after_commit_callbacks
  AfterCommitHelper::Logger.logger.debug "Processing #{after_commit_callbacks.length} callback for the after_commit hook" if after_commit_callbacks.length > 0

  @after_commit_callbacks.each do |callback|
    begin
      callback.call
    rescue Exception => e
      AfterCommitHelper::Logger.logger.debug "Exception while processing AfterCommitHelper proc: #{e.message}"
    end
  end

  # remove all of the procs, we execute one time only
  reset_after_commit_callbacks
end

#reset_after_commit_callbacksObject

resets the after commit callbacks



16
17
18
# File 'lib/after_commit_helper.rb', line 16

def reset_after_commit_callbacks
  @after_commit_callbacks = []
end