after_transaction 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d798fe126a19e0cdafaf8d1aafff328bf2d07ecc4050b0164d0f2d623c02a2f1
4
+ data.tar.gz: '0809a4e56f92d88bec520e950c43b1ec017770ee208ceda2041818848f709c81'
5
+ SHA512:
6
+ metadata.gz: f111404f248af1ea07db61c4dbd0814db8cf186b73c4e9acfde3e66cb8067dc6053bed603a3b7b2792ddf6d72a242ddd5c95af284d53a0bc1a41dfc648d03aeb
7
+ data.tar.gz: 631272adf2444c5b17ec5f7348357bcf22b99c24949d523cbc63b335221901cb6e066e52347cdee53be1d603109cc2226abee9ed25bd62b651c39abd45a14853
@@ -0,0 +1,13 @@
1
+ # AfterTransaction
2
+
3
+ A helper to run a block of code after a transaction has been committed.
4
+ If used outside of a transaction the block will be called immediately.
5
+ If the transaction is rolled back, the block will not get called.
6
+
7
+ ## Usage
8
+
9
+ ````ruby
10
+ AfterTransaction.call do
11
+ puts 'this will execute after commit'
12
+ end
13
+ ````
@@ -0,0 +1,52 @@
1
+ module AfterTransaction
2
+ def self.call(&block)
3
+ return block.call unless defined? ActiveRecord
4
+ return block.call unless in_transaction?
5
+ return use_after_commit_gem(block) if legacy_rails_with_after_commit_gem?
6
+ register_as_callback(block)
7
+ end
8
+
9
+ def self.in_transaction?
10
+ open_transactions = ActiveRecord::Base.connection.open_transactions
11
+ open_transactions -= 1 if ENV['RAILS_ENV'] == 'test'
12
+ open_transactions.positive?
13
+ end
14
+
15
+ def self.current_transaction
16
+ ActiveRecord::Base.connection.current_transaction
17
+ end
18
+
19
+ def self.use_after_commit_gem(block)
20
+ ActiveRecord::Base.after_transaction { block.call }
21
+ end
22
+
23
+ def self.legacy_rails_with_after_commit_gem?
24
+ ActiveRecord::Base.respond_to? :after_transaction
25
+ end
26
+
27
+ def self.register_as_callback(block)
28
+ current_transaction.add_record(Wrapper.new(block))
29
+ end
30
+
31
+ class Wrapper
32
+ def initialize(callable)
33
+ @callable = callable
34
+ end
35
+
36
+ def has_transactional_callbacks?
37
+ true
38
+ end
39
+
40
+ def before_committed!(*_); end
41
+
42
+ def committed!(*_)
43
+ @callable.call
44
+ end
45
+
46
+ def rolledback!(*_); end
47
+
48
+ def add_to_transaction
49
+ AfterTransaction.call &@callable
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require 'sqlite3'
3
+ require 'active_record'
4
+
5
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
6
+
7
+ RSpec.describe AfterTransaction do
8
+ describe '.call' do
9
+ context 'when not in a transaction' do
10
+ it 'executes immediately' do
11
+ result = AfterTransaction.call { 'me Al' }
12
+ expect(result).to eq 'me Al'
13
+ end
14
+ end
15
+
16
+ context 'when in a transaction' do
17
+ it 'executes after the transaction is commited' do
18
+ result = []
19
+ ActiveRecord::Base.transaction do
20
+ described_class.call { result << :after_transaction }
21
+ result << :in_transaction
22
+ end
23
+ expect(result).to eq [:in_transaction, :after_transaction]
24
+ end
25
+
26
+ it 'never execute if the transaction rolls back' do
27
+ result = []
28
+ ActiveRecord::Base.transaction do
29
+ described_class.call { result << :after_transaction }
30
+ result << :in_transaction
31
+ raise ActiveRecord::Rollback, 'spec cleanup'
32
+ end
33
+ expect(result).to eq [:in_transaction]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ require 'bundler/setup'
2
+ require 'after_transaction'
3
+ ENV['RAILS_ENV'] = 'test'
4
+
5
+ module TransactionHelper
6
+ def wrap_spec_in_transaction
7
+ ActiveRecord::Base.transaction(joinable: false) do
8
+ yield
9
+ raise ActiveRecord::Rollback, 'spec cleanup'
10
+ end
11
+ end
12
+ end
13
+
14
+ RSpec.configure do |config|
15
+ config.include TransactionHelper
16
+ # Enable flags like --only-failures and --next-failure
17
+ config.example_status_persistence_file_path = '.rspec_status'
18
+
19
+ # Disable RSpec exposing methods globally on `Module` and `main`
20
+ config.disable_monkey_patching!
21
+
22
+ config.expect_with :rspec do |c|
23
+ c.syntax = :expect
24
+ end
25
+
26
+ config.around do |example|
27
+ wrap_spec_in_transaction { example.run }
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: after_transaction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Bukowskis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-expectations
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "<"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.4'
69
+ description: Run blocks of code after transaction is commited
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - README.md
76
+ - lib/after_transaction.rb
77
+ - spec/after_transaction_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/bukowskis/after_transaction
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.0.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Run blocks of code after transaction is commited
102
+ test_files:
103
+ - spec/spec_helper.rb
104
+ - spec/after_transaction_spec.rb