Class: AfterCommitEverywhere::Wrap

Inherits:
Object
  • Object
show all
Defined in:
lib/after_commit_everywhere/wrap.rb

Overview

ActiveRecord model-like class to fake ActiveRecord to make it believe that it calls transactional callbacks on real model objects.

Instance Method Summary collapse

Constructor Details

#initialize(connection: ActiveRecord::Base.connection, **handlers) ⇒ Wrap

Returns a new instance of Wrap.



7
8
9
10
11
# File 'lib/after_commit_everywhere/wrap.rb', line 7

def initialize(connection: ActiveRecord::Base.connection, **handlers)
  @connection = connection
  @handlers = handlers
  @locale = I18n.locale
end

Instance Method Details

#add_to_transactionObject

Required for transaction(requires_new: true)



38
39
40
# File 'lib/after_commit_everywhere/wrap.rb', line 38

def add_to_transaction(*)
  @connection.add_transaction_record(self)
end

#before_committed!Object

rubocop: enable Naming/PredicateName



19
20
21
# File 'lib/after_commit_everywhere/wrap.rb', line 19

def before_committed!(*)
  I18n.with_locale(@locale) { @handlers[:before_commit]&.call }
end

#committed!(should_run_callbacks: true) ⇒ Object



27
28
29
30
31
# File 'lib/after_commit_everywhere/wrap.rb', line 27

def committed!(should_run_callbacks: true)
  return unless should_run_callbacks

  I18n.with_locale(@locale) { @handlers[:after_commit]&.call }
end

#has_transactional_callbacks?Boolean

rubocop: disable Naming/PredicateName

Returns:

  • (Boolean)


14
15
16
# File 'lib/after_commit_everywhere/wrap.rb', line 14

def has_transactional_callbacks?
  true
end

#rolledback!Object



33
34
35
# File 'lib/after_commit_everywhere/wrap.rb', line 33

def rolledback!(*)
  I18n.with_locale(@locale) { @handlers[:after_rollback]&.call }
end

#trigger_transactional_callbacks?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/after_commit_everywhere/wrap.rb', line 23

def trigger_transactional_callbacks?
  true
end