test_after_commit 0.2.0 → 0.2.1

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
+ SHA1:
3
+ metadata.gz: 0771c33d6db21be88b98458395d484ee9414352a
4
+ data.tar.gz: 154e46ea5761f401201401913c2af9dc9ebf698e
5
+ SHA512:
6
+ metadata.gz: 9188eaac46d1ea76037eebbde65b5ee700d173ab597a9e0e04165fcc689875c3382a84acdc474dbccd7949ed75cfa2958975547552136a548b53670209d783aa
7
+ data.tar.gz: 0cbf8d71596dfd913df0160e1a5932b7bbc844472cab2e968cb5f075b1ae1597f73e4fcad5837adbbaca6a3a784367a10831b781141b5bcf99415c2bf257ad17
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- test_after_commit (0.2.0)
4
+ test_after_commit (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Readme.md CHANGED
@@ -5,9 +5,31 @@ Install
5
5
 
6
6
  gem install test_after_commit
7
7
 
8
- # Gemfile
8
+ # Gemfile (never include in :development !)
9
9
  gem 'test_after_commit', :group => :test
10
10
 
11
+ Usage
12
+ =====
13
+ Test that the methods get called or the side-effect of the methods, something like:
14
+
15
+ ```Ruby
16
+ class Car < ActiveRecord::Base
17
+ after_commit :foo, :on => :update
18
+
19
+ def foo
20
+ $foo = 1
21
+ end
22
+ end
23
+
24
+ ...
25
+
26
+ it "sets $foo on commit" do
27
+ $foo.should == nil
28
+ car.save!
29
+ $foo.should == 1
30
+ end
31
+ ```
32
+
11
33
  TIPS
12
34
  ====
13
35
  - hooks do not re-raise errors (with or without this gem)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/test_after_commit
3
3
  specs:
4
- test_after_commit (0.1.1)
4
+ test_after_commit (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/test_after_commit
3
3
  specs:
4
- test_after_commit (0.1.1)
4
+ test_after_commit (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/test_after_commit
3
3
  specs:
4
- test_after_commit (0.1.1)
4
+ test_after_commit (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/mgrosser/code/tools/test_after_commit
3
3
  specs:
4
- test_after_commit (0.1.1)
4
+ test_after_commit (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -11,14 +11,14 @@ ActiveRecord::ConnectionAdapters::DatabaseStatements.class_eval do
11
11
  @test_open_transactions += 1
12
12
  result = yield
13
13
  rescue ActiveRecord::Rollback => e
14
+ rolled_back = true
14
15
  raise e
15
- else
16
- if @test_open_transactions == 1
16
+ ensure
17
+ if @test_open_transactions == 1 && !rolled_back
17
18
  test_commit_records
18
19
  end
19
- result
20
- ensure
21
20
  @test_open_transactions -= 1
21
+ result
22
22
  end
23
23
  end
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module TestAfterCommit
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
data/spec/database.rb CHANGED
@@ -18,6 +18,16 @@ ActiveRecord::Schema.define(:version => 1) do
18
18
  t.integer :car_id
19
19
  t.timestamps
20
20
  end
21
+
22
+ create_table "addresses", :force => true do |t|
23
+ t.integer :number_of_residents, :default => 0, :null => false
24
+ t.timestamps
25
+ end
26
+
27
+ create_table "people", :force => true do |t|
28
+ t.belongs_to :address
29
+ t.timestamps
30
+ end
21
31
  end
22
32
 
23
33
  class Car < ActiveRecord::Base
@@ -46,6 +56,12 @@ class Car < ActiveRecord::Base
46
56
  raise ActiveRecord::Rollback if make_rollback
47
57
  end
48
58
 
59
+ def self.returning_method_with_transaction
60
+ Car.transaction do
61
+ return Car.create
62
+ end
63
+ end
64
+
49
65
  private
50
66
 
51
67
  def save_once
@@ -91,3 +107,30 @@ class Bar < ActiveRecord::Base
91
107
  self.table_name = "cars"
92
108
  has_many :bars, :foreign_key => :car_id
93
109
  end
110
+
111
+ class Address < ActiveRecord::Base
112
+ has_many :people
113
+
114
+ after_commit :create_residents, :on => :create
115
+
116
+ def create_residents
117
+ if ActiveRecord::VERSION::MAJOR == 3
118
+ # stupid hack because nested after_commit is broken on rails 3 and loops
119
+ return if @create_residents
120
+ @create_residents = true
121
+ end
122
+
123
+ Person.create!(:address => self)
124
+ Person.create!(:address => self)
125
+ end
126
+ end
127
+
128
+ class Person < ActiveRecord::Base
129
+ belongs_to :address
130
+
131
+ after_commit :update_number_of_residents_on_address, :on => :create
132
+
133
+ def update_number_of_residents_on_address
134
+ address.update_attributes(:number_of_residents => address.number_of_residents + 1)
135
+ end
136
+ end
data/spec/spec_helper.rb CHANGED
@@ -13,7 +13,7 @@ RSpec.configure do |config|
13
13
  # open a transaction without using .transaction as activerecord use_transactional_fixtures does
14
14
  if ActiveRecord::VERSION::MAJOR > 3
15
15
  connection = ActiveRecord::Base.connection_handler.connection_pool_list.map(&:connection).first
16
- connection.begin_transaction joinable: false
16
+ connection.begin_transaction :joinable => false
17
17
  else
18
18
  connection = ActiveRecord::Base.connection_handler.connection_pools.values.map(&:connection).first
19
19
  connection.increment_open_transactions
@@ -22,6 +22,13 @@ describe TestAfterCommit do
22
22
  Car.called.should == [:update, :always]
23
23
  end
24
24
 
25
+ it "fires on update_attribute" do
26
+ car = Car.create
27
+ Car.called.clear
28
+ car.update_attribute :counter, 123
29
+ Car.called.should == [:update, :always]
30
+ end
31
+
25
32
  it "does not fire on rollback" do
26
33
  car = Car.new
27
34
  car.make_rollback = true
@@ -40,6 +47,11 @@ describe TestAfterCommit do
40
47
  Car.called.should == [:create, :always]
41
48
  end
42
49
 
50
+ it "fires when transaction block returns from method" do
51
+ Car.returning_method_with_transaction
52
+ Car.called.should == [:create, :always]
53
+ end
54
+
43
55
  it "does not raises errors" do
44
56
  car = Car.new
45
57
  car.raise_error = true
@@ -86,4 +98,20 @@ describe TestAfterCommit do
86
98
  Car.called.should == [:observed_after_commit, :create, :always]
87
99
  end
88
100
  end
101
+
102
+ context "nested after_commit" do
103
+ before do
104
+ @address = Address.create!
105
+ end
106
+
107
+ it 'is executed' do
108
+ pending unless ENV["REAL"] && ActiveRecord::VERSION::MAJOR == 4
109
+ lambda { Person.create!(:address => @address) }.should change(@address, :number_of_residents).by(1)
110
+
111
+ # one from the line above and two from the after_commit
112
+ @address.people.count.should == 3
113
+
114
+ @address.number_of_residents.should == 3
115
+ end
116
+ end
89
117
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test_after_commit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.2.1
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-04-04 00:00:00.000000000 Z
11
+ date: 2013-08-22 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description:
15
14
  email: michael@grosser.it
@@ -40,32 +39,25 @@ files:
40
39
  homepage: http://github.com/grosser/test_after_commit
41
40
  licenses:
42
41
  - MIT
42
+ metadata: {}
43
43
  post_install_message:
44
44
  rdoc_options: []
45
45
  require_paths:
46
46
  - lib
47
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
48
  requirements:
50
- - - ! '>='
49
+ - - '>='
51
50
  - !ruby/object:Gem::Version
52
51
  version: '0'
53
- segments:
54
- - 0
55
- hash: 1962794305227048949
56
52
  required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
53
  requirements:
59
- - - ! '>='
54
+ - - '>='
60
55
  - !ruby/object:Gem::Version
61
56
  version: '0'
62
- segments:
63
- - 0
64
- hash: 1962794305227048949
65
57
  requirements: []
66
58
  rubyforge_project:
67
- rubygems_version: 1.8.25
59
+ rubygems_version: 2.0.6
68
60
  signing_key:
69
- specification_version: 3
61
+ specification_version: 4
70
62
  summary: makes after_commit callbacks testable in Rails 3+ with transactional_fixtures
71
63
  test_files: []