module AfterTransaction
Public Class Methods
call(&block)
click to toggle source
# File lib/after_transaction.rb, line 2 def self.call(&block) return block.call unless defined? ActiveRecord return block.call unless in_transaction? return use_after_commit_gem(block) if legacy_rails_with_after_commit_gem? register_as_callback(block) end
current_transaction()
click to toggle source
# File lib/after_transaction.rb, line 15 def self.current_transaction ActiveRecord::Base.connection.current_transaction end
in_transaction?()
click to toggle source
# File lib/after_transaction.rb, line 9 def self.in_transaction? open_transactions = ActiveRecord::Base.connection.open_transactions open_transactions -= 1 if ENV['RAILS_ENV'] == 'test' open_transactions.positive? end
legacy_rails_with_after_commit_gem?()
click to toggle source
# File lib/after_transaction.rb, line 23 def self.legacy_rails_with_after_commit_gem? ActiveRecord::Base.respond_to? :after_transaction end
register_as_callback(block)
click to toggle source
# File lib/after_transaction.rb, line 27 def self.register_as_callback(block) current_transaction.add_record(Wrapper.new(block)) end
use_after_commit_gem(block)
click to toggle source
# File lib/after_transaction.rb, line 19 def self.use_after_commit_gem(block) ActiveRecord::Base.after_transaction { block.call } end