bukowskis_after_transaction 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d464bb8e14322d2c5f7e4219db2f2eebc3df8fbfc6fd5948d17a192c123a7952
4
+ data.tar.gz: b6d662b24ab76081feb6a53eca5a831e8ca5ec1642bd90692a0f94c0acddd37c
5
+ SHA512:
6
+ metadata.gz: 8a4cd1f5ab8adcc25ca6bba365341c03bac43d139692bdd01025db990a62d3eb0a9c9855fb52d53a2a60303d65f8c8444a63fabb331464480b6ac63baf50ed44
7
+ data.tar.gz: 3823aa8e647d78df53afe54163ff857247f8c1509b24fb50819df10e7c5f089e7bc3da29d43a21d3a249377e426030038864d0d30ed019699b9e17d5dceae5f6
@@ -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 @@
1
+ require 'after_transaction'
@@ -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,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bukowskis_after_transaction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
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
+ - lib/bukowskis_after_transaction.rb
78
+ - spec/after_transaction_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/bukowskis/bukowskis_after_transaction
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.6
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Run blocks of code after transaction is commited
104
+ test_files:
105
+ - spec/spec_helper.rb
106
+ - spec/after_transaction_spec.rb