Module: AfterTransaction
- Defined in:
- lib/after_transaction.rb
Defined Under Namespace
Classes: Wrapper
Class Method Summary collapse
- .call(&block) ⇒ Object
- .current_transaction ⇒ Object
- .in_transaction? ⇒ Boolean
- .legacy_rails_with_after_commit_gem? ⇒ Boolean
- .register_as_callback(block) ⇒ Object
- .use_after_commit_gem(block) ⇒ Object
Class Method Details
.call(&block) ⇒ Object
2 3 4 5 6 7 |
# 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 ⇒ Object
15 16 17 |
# File 'lib/after_transaction.rb', line 15 def self.current_transaction ActiveRecord::Base.connection.current_transaction end |
.in_transaction? ⇒ Boolean
9 10 11 12 13 |
# 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? ⇒ Boolean
23 24 25 |
# 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) ⇒ Object
27 28 29 |
# 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) ⇒ Object
19 20 21 |
# File 'lib/after_transaction.rb', line 19 def self.use_after_commit_gem(block) ActiveRecord::Base.after_transaction { block.call } end |