ar_after_transaction 0.4.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0cc72b7f26b842b9adfedb004133d2cc0748e0783b0fdfeb0aebe9e4c52f8b3a
4
+ data.tar.gz: c61ca0e181cc2a4eaacdb45b63aab20dc77bd7af72dcb066735adce2f989944b
5
+ SHA512:
6
+ metadata.gz: a5402cb5c03bbf9d67a8a2da4e30dde24c191e8ec403a8f4a0084d95a5d6d46f9d658cf3903f21f2947e51113df2a13eae51393bf6e8a9730e3bd8126a93cabc
7
+ data.tar.gz: 4e6556949152666f978bf152ac5c4c830dfbbaa4cb3a36da6004a4ec8a2273a989476116377ec81c3f58e4710265da911eaf2debf5565ca21718c6443c3904ca
data/Readme.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem Version](https://badge.fury.io/rb/ar_after_transaction.png)](http://badge.fury.io/rb/ar_after_transaction)
2
+ ![CI](https://github.com/grosser/ar_after_transaction/workflows/CI/badge.svg)
3
+
1
4
  Do something only after the currently open transactions have finished.
2
5
 
3
6
  Normally everything gets rolled back when a transaction fails, but you cannot roll back sending an email or adding a job to Resque.
@@ -5,36 +8,44 @@ Normally everything gets rolled back when a transaction fails, but you cannot ro
5
8
  Install
6
9
  =======
7
10
 
8
- gem install ar_after_transaction
11
+ ```bash
12
+ gem install ar_after_transaction
13
+ ```
9
14
 
10
15
 
11
16
  Usage
12
17
  =====
18
+
13
19
  ### just-in-time callbacks
14
- class User
15
- after_create :do_stuff, :oops
16
-
17
- def do_stuff
18
- after_transaction do
19
- send_an_email # cannot be rolled back
20
- end
21
- comments.create(...) # will be rolled back
22
- end
23
-
24
- def oops
25
- raise "do the rolback!"
26
- end
20
+
21
+ ```ruby
22
+ class User
23
+ after_create :do_stuff, :oops
24
+
25
+ def do_stuff
26
+ after_transaction do
27
+ send_an_email # cannot be rolled back
27
28
  end
29
+ comments.create(...) # will be rolled back
30
+ end
31
+
32
+ def oops
33
+ raise "do the rolback!"
34
+ end
35
+ end
36
+ ```
28
37
 
29
38
  ### General 'this should be rolled back when in a transaction' code like jobs
30
39
 
31
- class Resque
32
- def revertable_enqueue(*args)
33
- ActiveRecord::Base.after_transaction do
34
- enqueue(*args)
35
- end
36
- end
40
+ ```ruby
41
+ class Resque
42
+ def revertable_enqueue(*args)
43
+ ActiveRecord::Base.after_transaction do
44
+ enqueue(*args)
37
45
  end
46
+ end
47
+ end
48
+ ```
38
49
 
39
50
  ### When not in a transaction
40
51
  after_transaction will perform the given block immediately
@@ -43,25 +54,33 @@ after_transaction will perform the given block immediately
43
54
  after_transaction assumes zero open transactions.<br/>
44
55
  If you use transactional fixtures you should change it in test mode.
45
56
 
46
- # config/environments/test.rb
47
- config.after_initialize do
48
- ActiveRecord::Base.normally_open_transactions = 1
49
- end
57
+ `Rspec:`
58
+ ```ruby
59
+ # spec/rails_helper.rb
60
+ config.before(:suite) do
61
+ ActiveRecord::Base.normally_open_transactions = 1
62
+ end
63
+ ```
50
64
 
51
65
  ### Rails 3: after_commit hook can replace the first usage example:
52
66
 
53
- class User
54
- after_commit :send_an_email :on=>:create
55
- after_create :do_stuff, :oops
56
- ...
57
- end
67
+ ```ruby
68
+ class User
69
+ after_commit :send_an_email on: :create
70
+ after_create :do_stuff, :oops
71
+ ...
72
+ end
73
+ ```
58
74
 
59
75
  Alternative
60
76
  ===========
77
+
61
78
  Rails 3+
62
79
  - basic support is built in, use it if you can!
63
80
  - `after_commit :foo`
64
- - `after_commit :bar, :on => :create / :update`
81
+ - `after_commit :bar, on: :create / :update`
82
+ - [after_commit everywhere](https://dev.to/evilmartians/rails-aftercommit-everywhere--4j9g)
83
+
65
84
 
66
85
  [after_commit](https://github.com/pat/after_commit)<br/>
67
86
  - pro: threadsafe<br/>
@@ -78,9 +97,11 @@ Authors
78
97
  - [Benedikt Deicke](http://blog.synatic.net)
79
98
  - [Tyler Rick](https://github.com/TylerRick)
80
99
  - [Michael Wu](https://github.com/michaelmwu)
100
+ - [C.W.](https://github.com/compwron)
101
+ - [Ben Weintraub](https://github.com/benweint)
102
+ - [Vladimir Temnikov](https://github.com/vladimirtemnikov)
81
103
 
82
104
  [Michael Grosser](http://grosser.it)<br/>
83
105
  michael@grosser.it<br/>
84
106
  License: MIT<br/>
85
- [![Build Status](https://travis-ci.org/grosser/ar_after_transaction.png)](https://travis-ci.org/grosser/ar_after_transaction)
86
107
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ARAfterTransaction
2
- VERSION = Version = '0.4.0'
3
- end
4
+ VERSION = '0.8.0'
5
+ end
@@ -1,70 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record'
2
4
  require 'ar_after_transaction/version'
3
5
 
4
6
  module ARAfterTransaction
5
7
  module ClassMethods
6
- def self.extended( base )
7
- base.class_eval do
8
- class << self
9
- alias_method :transaction_without_after, :transaction
10
- alias_method :transaction, :transaction_with_after
11
- end
12
- end
13
- end
14
-
15
- def transaction_with_after(*args)
16
- clean = true
17
- transaction_without_after(*args) do
18
- begin
19
- yield
20
- rescue ActiveRecord::Rollback
21
- clean = false
22
- raise
23
- end
24
- end
25
- rescue Exception
26
- clean = false
27
- raise
28
- ensure
29
- unless transactions_open?
30
- callbacks = delete_after_transaction_callbacks
31
- callbacks.each(&:call) if clean
32
- end
33
- end
34
-
35
8
  def after_transaction(&block)
36
- if transactions_open?
37
- connection.after_transaction_callbacks ||= []
38
- connection.after_transaction_callbacks << block
39
- else
40
- yield
41
- end
9
+ connection.after_transaction(&block)
42
10
  end
43
11
 
44
12
  def normally_open_transactions
45
- @@normally_open_transactions ||= 0
13
+ connection.normally_open_transactions ||= 0
46
14
  end
47
15
 
48
- def normally_open_transactions=(value)
49
- @@normally_open_transactions = value
50
- end
16
+ delegate :normally_open_transactions=, to: :connection
51
17
 
52
18
  private
53
19
 
54
20
  def transactions_open?
55
- connection.open_transactions > normally_open_transactions
56
- end
57
-
58
- def delete_after_transaction_callbacks
59
- result = connection.after_transaction_callbacks || []
60
- connection.after_transaction_callbacks = []
61
- result
21
+ connection.send :transactions_open?
62
22
  end
63
23
  end
64
24
 
65
25
  module InstanceMethods
66
26
  def after_transaction(&block)
67
- self.class.after_transaction(&block)
27
+ self.class.connection.after_transaction(&block)
68
28
  end
69
29
  end
70
30
  end
@@ -74,10 +34,55 @@ module ARAfterTransactionConnection
74
34
  base.class_eval do
75
35
  attr_accessor :normally_open_transactions
76
36
  attr_accessor :after_transaction_callbacks
37
+
38
+ alias_method :transaction_without_after, :transaction
39
+ alias_method :transaction, :transaction_with_after
40
+ end
41
+ end
42
+
43
+ def transaction_with_after(**args)
44
+ clean = true
45
+ transaction_without_after(**args) do
46
+ yield
47
+ rescue ActiveRecord::Rollback
48
+ clean = false
49
+ raise
50
+ end
51
+ rescue StandardError
52
+ clean = false
53
+ raise
54
+ ensure
55
+ unless transactions_open?
56
+ callbacks = delete_after_transaction_callbacks
57
+ callbacks.each(&:call) if clean
58
+ end
59
+ end
60
+
61
+ def after_transaction(&block)
62
+ if transactions_open?
63
+ self.after_transaction_callbacks ||= []
64
+ self.after_transaction_callbacks << block
65
+ else
66
+ yield
77
67
  end
78
68
  end
69
+
70
+ private
71
+
72
+ def transactions_open?
73
+ return false unless active?
74
+
75
+ self.normally_open_transactions ||= 0
76
+ open_transactions > normally_open_transactions
77
+ end
78
+
79
+ def delete_after_transaction_callbacks
80
+ result = after_transaction_callbacks || []
81
+ self.after_transaction_callbacks = []
82
+ result
83
+ end
79
84
  end
80
85
 
81
- ActiveRecord::Base.send(:extend, ARAfterTransaction::ClassMethods)
82
- ActiveRecord::Base.send(:include, ARAfterTransaction::InstanceMethods)
83
- ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, ARAfterTransactionConnection)
86
+ ActiveRecord::Base.extend ARAfterTransaction::ClassMethods
87
+ ActiveRecord::Base.include ARAfterTransaction::InstanceMethods
88
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.include ARAfterTransactionConnection
metadata CHANGED
@@ -1,30 +1,117 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_after_transaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
5
- prerelease:
4
+ version: 0.8.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Michael Grosser
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-15 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: 5.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.1'
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 5.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bump
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: wwtd
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
28
115
  - !ruby/object:Gem::Version
29
116
  version: '0'
30
117
  description:
@@ -33,52 +120,30 @@ executables: []
33
120
  extensions: []
34
121
  extra_rdoc_files: []
35
122
  files:
36
- - .travis.yml
37
- - Appraisals
38
- - Gemfile
39
- - Gemfile.lock
40
- - Rakefile
41
123
  - Readme.md
42
- - ar_after_transaction.gemspec
43
- - gemfiles/rails2.gemfile
44
- - gemfiles/rails2.gemfile.lock
45
- - gemfiles/rails3.gemfile
46
- - gemfiles/rails3.gemfile.lock
47
124
  - lib/ar_after_transaction.rb
48
125
  - lib/ar_after_transaction/version.rb
49
- - spec/after_initialize_spec.rb
50
- - spec/ar_after_transaction_spec.rb
51
- - spec/setup_database.rb
52
- - spec/spec_helper.rb
53
126
  homepage: http://github.com/grosser/ar_after_transaction
54
127
  licenses:
55
128
  - MIT
129
+ metadata: {}
56
130
  post_install_message:
57
131
  rdoc_options: []
58
132
  require_paths:
59
133
  - lib
60
134
  required_ruby_version: !ruby/object:Gem::Requirement
61
- none: false
62
135
  requirements:
63
- - - ! '>='
136
+ - - ">="
64
137
  - !ruby/object:Gem::Version
65
- version: '0'
66
- segments:
67
- - 0
68
- hash: 2099536990730070045
138
+ version: 2.6.0
69
139
  required_rubygems_version: !ruby/object:Gem::Requirement
70
- none: false
71
140
  requirements:
72
- - - ! '>='
141
+ - - ">="
73
142
  - !ruby/object:Gem::Version
74
143
  version: '0'
75
- segments:
76
- - 0
77
- hash: 2099536990730070045
78
144
  requirements: []
79
- rubyforge_project:
80
- rubygems_version: 1.8.25
145
+ rubygems_version: 3.1.6
81
146
  signing_key:
82
- specification_version: 3
147
+ specification_version: 4
83
148
  summary: Execute irreversible actions only when transactions are not rolled back
84
149
  test_files: []
data/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- rvm:
2
- - ree
3
- - 1.9.2
4
- - 1.9.3
data/Appraisals DELETED
@@ -1,8 +0,0 @@
1
- appraise "rails2" do
2
- gem "activerecord", "~>2.3.14"
3
- end
4
-
5
- appraise "rails3" do
6
- gem "activerecord", "~>3.2.7"
7
- gem 'railties'
8
- end
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gemspec
4
-
5
- gem 'appraisal'
6
- gem 'bump'
7
- gem 'rake'
8
- gem 'rspec', '~>2'
9
- gem 'sqlite3'
data/Gemfile.lock DELETED
@@ -1,51 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- ar_after_transaction (0.4.0)
5
- activerecord
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (3.2.13)
11
- activesupport (= 3.2.13)
12
- builder (~> 3.0.0)
13
- activerecord (3.2.13)
14
- activemodel (= 3.2.13)
15
- activesupport (= 3.2.13)
16
- arel (~> 3.0.2)
17
- tzinfo (~> 0.3.29)
18
- activesupport (3.2.13)
19
- i18n (= 0.6.1)
20
- multi_json (~> 1.0)
21
- appraisal (0.5.1)
22
- bundler
23
- rake
24
- arel (3.0.2)
25
- builder (3.0.4)
26
- bump (0.3.5)
27
- diff-lcs (1.1.3)
28
- i18n (0.6.1)
29
- multi_json (1.7.7)
30
- rake (10.0.2)
31
- rspec (2.12.0)
32
- rspec-core (~> 2.12.0)
33
- rspec-expectations (~> 2.12.0)
34
- rspec-mocks (~> 2.12.0)
35
- rspec-core (2.12.0)
36
- rspec-expectations (2.12.0)
37
- diff-lcs (~> 1.1.3)
38
- rspec-mocks (2.12.0)
39
- sqlite3 (1.3.6)
40
- tzinfo (0.3.37)
41
-
42
- PLATFORMS
43
- ruby
44
-
45
- DEPENDENCIES
46
- appraisal
47
- ar_after_transaction!
48
- bump
49
- rake
50
- rspec (~> 2)
51
- sqlite3
data/Rakefile DELETED
@@ -1,13 +0,0 @@
1
- require "bundler/setup"
2
- require "bundler/gem_tasks"
3
- require "appraisal"
4
- require "bump/tasks"
5
-
6
-
7
- task :spec do
8
- sh "rspec spec"
9
- end
10
-
11
- task :default do
12
- sh "bundle exec rake appraisal:install && bundle exec rake appraisal spec"
13
- end
@@ -1,12 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
- require 'ar_after_transaction/version'
3
-
4
- Gem::Specification.new "ar_after_transaction", ARAfterTransaction::VERSION do |s|
5
- s.summary = "Execute irreversible actions only when transactions are not rolled back"
6
- s.authors = ["Michael Grosser"]
7
- s.email = "michael@grosser.it"
8
- s.homepage = "http://github.com/grosser/ar_after_transaction"
9
- s.files = `git ls-files`.split("\n")
10
- s.add_runtime_dependency "activerecord"
11
- s.license = "MIT"
12
- end
@@ -1,12 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "bump"
7
- gem "rake"
8
- gem "rspec", "~>2"
9
- gem "sqlite3"
10
- gem "activerecord", "~>2.3.14"
11
-
12
- gemspec :path=>"../"
@@ -1,39 +0,0 @@
1
- PATH
2
- remote: /Users/mgrosser/code/tools/ar_after_transaction
3
- specs:
4
- ar_after_transaction (0.3.0)
5
- activerecord
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activerecord (2.3.14)
11
- activesupport (= 2.3.14)
12
- activesupport (2.3.14)
13
- appraisal (0.4.1)
14
- bundler
15
- rake
16
- bump (0.3.5)
17
- diff-lcs (1.1.3)
18
- rake (0.9.2.2)
19
- rspec (2.11.0)
20
- rspec-core (~> 2.11.0)
21
- rspec-expectations (~> 2.11.0)
22
- rspec-mocks (~> 2.11.0)
23
- rspec-core (2.11.1)
24
- rspec-expectations (2.11.3)
25
- diff-lcs (~> 1.1.3)
26
- rspec-mocks (2.11.3)
27
- sqlite3 (1.3.6)
28
-
29
- PLATFORMS
30
- ruby
31
-
32
- DEPENDENCIES
33
- activerecord (~> 2.3.14)
34
- appraisal
35
- ar_after_transaction!
36
- bump
37
- rake
38
- rspec (~> 2)
39
- sqlite3
@@ -1,13 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "appraisal"
6
- gem "bump"
7
- gem "rake"
8
- gem "rspec", "~>2"
9
- gem "sqlite3"
10
- gem "activerecord", "~>3.2.7"
11
- gem "railties"
12
-
13
- gemspec :path=>"../"
@@ -1,89 +0,0 @@
1
- PATH
2
- remote: /Users/mgrosser/code/tools/ar_after_transaction
3
- specs:
4
- ar_after_transaction (0.3.0)
5
- activerecord
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionpack (3.2.8)
11
- activemodel (= 3.2.8)
12
- activesupport (= 3.2.8)
13
- builder (~> 3.0.0)
14
- erubis (~> 2.7.0)
15
- journey (~> 1.0.4)
16
- rack (~> 1.4.0)
17
- rack-cache (~> 1.2)
18
- rack-test (~> 0.6.1)
19
- sprockets (~> 2.1.3)
20
- activemodel (3.2.8)
21
- activesupport (= 3.2.8)
22
- builder (~> 3.0.0)
23
- activerecord (3.2.8)
24
- activemodel (= 3.2.8)
25
- activesupport (= 3.2.8)
26
- arel (~> 3.0.2)
27
- tzinfo (~> 0.3.29)
28
- activesupport (3.2.8)
29
- i18n (~> 0.6)
30
- multi_json (~> 1.0)
31
- appraisal (0.4.1)
32
- bundler
33
- rake
34
- arel (3.0.2)
35
- builder (3.0.4)
36
- bump (0.3.5)
37
- diff-lcs (1.1.3)
38
- erubis (2.7.0)
39
- hike (1.2.3)
40
- i18n (0.6.1)
41
- journey (1.0.4)
42
- json (1.8.0)
43
- multi_json (1.3.6)
44
- rack (1.4.5)
45
- rack-cache (1.2)
46
- rack (>= 0.4)
47
- rack-ssl (1.3.3)
48
- rack
49
- rack-test (0.6.2)
50
- rack (>= 1.0)
51
- railties (3.2.8)
52
- actionpack (= 3.2.8)
53
- activesupport (= 3.2.8)
54
- rack-ssl (~> 1.3.2)
55
- rake (>= 0.8.7)
56
- rdoc (~> 3.4)
57
- thor (>= 0.14.6, < 2.0)
58
- rake (0.9.2.2)
59
- rdoc (3.12.2)
60
- json (~> 1.4)
61
- rspec (2.11.0)
62
- rspec-core (~> 2.11.0)
63
- rspec-expectations (~> 2.11.0)
64
- rspec-mocks (~> 2.11.0)
65
- rspec-core (2.11.1)
66
- rspec-expectations (2.11.3)
67
- diff-lcs (~> 1.1.3)
68
- rspec-mocks (2.11.3)
69
- sprockets (2.1.3)
70
- hike (~> 1.2)
71
- rack (~> 1.0)
72
- tilt (~> 1.1, != 1.3.0)
73
- sqlite3 (1.3.6)
74
- thor (0.18.1)
75
- tilt (1.4.1)
76
- tzinfo (0.3.33)
77
-
78
- PLATFORMS
79
- ruby
80
-
81
- DEPENDENCIES
82
- activerecord (~> 3.2.7)
83
- appraisal
84
- ar_after_transaction!
85
- bump
86
- railties
87
- rake
88
- rspec (~> 2)
89
- sqlite3
@@ -1,96 +0,0 @@
1
- require 'active_record'
2
- require File.expand_path '../setup_database', __FILE__
3
-
4
- if ActiveRecord::VERSION::MAJOR > 2
5
- require 'action_controller/railtie'
6
-
7
- Rack::Session::Cookie
8
-
9
- module Passthrough
10
- def self.extended( base )
11
- base.class_eval do
12
- class << self
13
- alias_method :transaction_without_passthrough, :transaction
14
- alias_method :transaction, :transaction_with_passthrough
15
- end
16
- end
17
- end
18
-
19
- def transaction_with_passthrough(*args, &block)
20
- transaction_without_passthrough(*args, &block)
21
- end
22
- end
23
-
24
- class Railtie < ::Rails::Railtie
25
- config.after_initialize do
26
- ActiveRecord::Base.send(:extend, Passthrough)
27
- end
28
- end
29
-
30
- module ARAfterTransaction
31
- class Application < ::Rails::Application
32
- config.active_support.deprecation = :log
33
-
34
- config.after_initialize do
35
- require 'ar_after_transaction'
36
- end
37
- end
38
- end
39
-
40
- Rack::Session::Cookie.send(:define_method, :warn){|_|} # seilence secret warning
41
- ARAfterTransaction::Application.initialize! # initialize app
42
-
43
- class AnExpectedError < Exception
44
- end
45
-
46
- class User
47
- cattr_accessor :test_callbacks, :test_stack
48
- self.test_stack = []
49
- self.test_callbacks = []
50
-
51
- after_create :do_it
52
- def do_it
53
- self.class.test_callbacks.map{|callback| send(callback)}.last
54
- end
55
-
56
- def do_after
57
- after_transaction do
58
- ActiveRecord::Base.transaction do
59
- # nested transaction should not cause infinitive recursion
60
- end
61
- self.class.test_stack << :after
62
- end
63
- end
64
-
65
- def do_normal
66
- self.class.test_stack << :normal
67
- end
68
-
69
- def oops
70
- raise AnExpectedError
71
- end
72
-
73
- def raise_rollback
74
- raise ActiveRecord::Rollback
75
- end
76
- end
77
-
78
- describe ARAfterTransaction do
79
- before do
80
- User.normally_open_transactions = nil
81
- User.send(:transactions_open?).should == false
82
- User.test_stack.clear
83
- User.test_callbacks.clear
84
- end
85
-
86
- it "has a VERSION" do
87
- ARAfterTransaction::VERSION.should =~ /^\d+\.\d+\.\d+$/
88
- end
89
-
90
- it "executes after a transaction" do
91
- User.test_callbacks = [:do_after, :do_normal]
92
- User.create!
93
- User.test_stack.should == [:normal, :after]
94
- end
95
- end
96
- end
@@ -1,148 +0,0 @@
1
- require "spec_helper"
2
-
3
- class AnExpectedError < Exception
4
- end
5
-
6
- class User
7
- cattr_accessor :test_callbacks, :test_stack
8
- self.test_stack = []
9
- self.test_callbacks = []
10
-
11
- after_create :do_it
12
- def do_it
13
- self.class.test_callbacks.map{|callback| send(callback)}.last
14
- end
15
-
16
- def do_after
17
- after_transaction do
18
- ActiveRecord::Base.transaction do
19
- # nested transaction should not cause infinitive recursion
20
- end
21
- self.class.test_stack << :after
22
- end
23
- end
24
-
25
- def do_normal
26
- self.class.test_stack << :normal
27
- end
28
-
29
- def oops
30
- raise AnExpectedError
31
- end
32
-
33
- def raise_rollback
34
- raise ActiveRecord::Rollback
35
- end
36
- end
37
-
38
- describe ARAfterTransaction do
39
- before do
40
- User.normally_open_transactions = nil
41
- User.send(:transactions_open?).should == false
42
- User.test_stack.clear
43
- User.test_callbacks.clear
44
- end
45
-
46
- it "has a VERSION" do
47
- ARAfterTransaction::VERSION.should =~ /^\d+\.\d+\.\d+$/
48
- end
49
-
50
- it "executes after a transaction" do
51
- User.test_callbacks = [:do_after, :do_normal]
52
- User.create!
53
- User.test_stack.should == [:normal, :after]
54
- end
55
-
56
- it "does not execute when transaction was rolled back" do
57
- User.test_callbacks = [:do_after, :do_normal, :oops]
58
- lambda{
59
- User.create!
60
- }.should raise_error(AnExpectedError)
61
- User.test_stack.should == [:normal]
62
- end
63
-
64
- it "does not execute when transaction gets rolled back by ActiveRecord::Rollback raised in an after_create callback" do
65
- User.test_callbacks = [:do_after, :do_normal, :raise_rollback]
66
- user = User.create!
67
- User.test_stack.should == [:normal]
68
- end
69
-
70
- it "does not execute when transaction gets rolled back by ActiveRecord::Rollback outside of the model" do
71
- User.test_callbacks = [:do_after, :do_normal]
72
- user = nil
73
- ActiveRecord::Base.transaction do
74
- user = User.create!
75
- raise ActiveRecord::Rollback
76
- end
77
- User.test_stack.should == [:normal]
78
- end
79
-
80
- it "clears transaction callbacks when transaction fails" do
81
- User.test_callbacks = [:do_after, :do_normal, :oops]
82
- lambda{
83
- User.create!
84
- }.should raise_error(AnExpectedError)
85
- User.test_callbacks = [:do_normal]
86
- User.create!
87
- User.test_stack.should == [:normal, :normal]
88
- end
89
-
90
- it "executes when no transaction is open" do
91
- user = User.new
92
- user.do_after
93
- user.do_normal
94
- User.test_stack.should == [:after, :normal]
95
- end
96
-
97
- it "executes when open transactions are normal" do
98
- User.normally_open_transactions = 1
99
- User.test_callbacks = [:do_after, :do_normal]
100
- User.create!
101
- User.test_stack.should == [:after, :normal]
102
- end
103
-
104
- it "does not execute the same callback twice when successful" do
105
- User.test_callbacks = [:do_after, :do_normal]
106
- User.create!
107
- User.create!
108
- User.test_stack.should == [:normal, :after, :normal, :after]
109
- end
110
-
111
- it "does not execute the same callback twice when failed" do
112
- User.test_callbacks = [:do_after, :do_normal, :oops]
113
- lambda{
114
- User.create!
115
- }.should raise_error(AnExpectedError)
116
- lambda{
117
- User.create!
118
- }.should raise_error(AnExpectedError)
119
- User.test_stack.should == [:normal, :normal]
120
- end
121
-
122
- it "does not crash with additional options" do
123
- User.transaction(:requires_new => true){}
124
- end
125
-
126
- describe :normally_open_transactions do
127
- it "uses 0 by default" do
128
- User.normally_open_transactions.should == 0
129
- end
130
-
131
- it "can set normally open transactions" do
132
- User.normally_open_transactions = 5
133
- User.normally_open_transactions.should == 5
134
- end
135
-
136
- it "sets them globally" do
137
- User.normally_open_transactions = 5
138
- ActiveRecord::Base.normally_open_transactions.should == 5
139
- end
140
- end
141
- end
142
-
143
- describe "A normal ActiveRecord subclass" do
144
- it "does not get polluted" do
145
- User.const_defined?(:VERSION).should be_false
146
- User.const_defined?(:Version).should be_false
147
- end
148
- end
@@ -1,19 +0,0 @@
1
- ActiveRecord::Base.establish_connection(
2
- :adapter => 'sqlite3',
3
- :database => ':memory:'
4
- )
5
-
6
- ActiveRecord::Migration.verbose = false
7
- ActiveRecord::Schema.define(:version => 1) do
8
- create_table :users do |t|
9
- t.string :name
10
- t.timestamps
11
- end
12
- end
13
-
14
- # for detailed debugging:
15
- #require 'logger'
16
- #ActiveRecord::Base.logger = Logger.new(STDOUT)
17
-
18
- class User < ActiveRecord::Base
19
- end
data/spec/spec_helper.rb DELETED
@@ -1,2 +0,0 @@
1
- require 'ar_after_transaction'
2
- require File.expand_path '../setup_database', __FILE__