test_after_commit 0.0.1

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.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - ree
3
+ - 1.9.2
4
+ - 1.9.3
data/Appraisals ADDED
@@ -0,0 +1,12 @@
1
+ appraise "30" do
2
+ gem "activerecord", "~> 3.0.15"
3
+ end
4
+
5
+ appraise "31" do
6
+ gem "activerecord", "~> 3.1.6"
7
+ end
8
+
9
+ appraise "32" do
10
+ gem "activerecord", "~> 3.2.6"
11
+ end
12
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ gem 'rake'
5
+ gem 'rspec', '~>2'
6
+ gem 'sqlite3'
7
+ gem 'appraisal'
8
+ gem 'activerecord', :require => 'active_record'
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ test_after_commit (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.0.15)
10
+ activesupport (= 3.0.15)
11
+ builder (~> 2.1.2)
12
+ i18n (~> 0.5.0)
13
+ activerecord (3.0.15)
14
+ activemodel (= 3.0.15)
15
+ activesupport (= 3.0.15)
16
+ arel (~> 2.0.10)
17
+ tzinfo (~> 0.3.23)
18
+ activesupport (3.0.15)
19
+ appraisal (0.4.1)
20
+ bundler
21
+ rake
22
+ arel (2.0.10)
23
+ builder (2.1.2)
24
+ diff-lcs (1.1.3)
25
+ i18n (0.5.0)
26
+ rake (0.9.2)
27
+ rspec (2.6.0)
28
+ rspec-core (~> 2.6.0)
29
+ rspec-expectations (~> 2.6.0)
30
+ rspec-mocks (~> 2.6.0)
31
+ rspec-core (2.6.4)
32
+ rspec-expectations (2.6.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.6.0)
35
+ sqlite3 (1.3.6)
36
+ tzinfo (0.3.33)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ activerecord
43
+ appraisal
44
+ rake
45
+ rspec (~> 2)
46
+ sqlite3
47
+ test_after_commit!
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'appraisal'
3
+
4
+ task :spec do
5
+ sh "rspec spec/"
6
+ end
7
+
8
+ task :default do
9
+ sh "bundle exec rake appraisal:install && bundle exec rake appraisal spec"
10
+ end
11
+
12
+ # extracted from https://github.com/grosser/project_template
13
+ rule /^version:bump:.*/ do |t|
14
+ sh "git status | grep 'nothing to commit'" # ensure we are not dirty
15
+ index = ['major', 'minor','patch'].index(t.name.split(':').last)
16
+ file = 'lib/test_after_commit/version.rb'
17
+
18
+ version_file = File.read(file)
19
+ old_version, *version_parts = version_file.match(/(\d+)\.(\d+)\.(\d+)/).to_a
20
+ version_parts[index] = version_parts[index].to_i + 1
21
+ version_parts[2] = 0 if index < 2 # remove patch for minor
22
+ version_parts[1] = 0 if index < 1 # remove minor for major
23
+ new_version = version_parts * '.'
24
+ File.open(file,'w'){|f| f.write(version_file.sub(old_version, new_version)) }
25
+
26
+ sh "bundle && git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
27
+ end
data/Readme.md ADDED
@@ -0,0 +1,19 @@
1
+ Make after_commit callbacks fire in tests for Rails 3+ with transactional_fixtures = true.
2
+
3
+ Install
4
+ =======
5
+
6
+ gem install test_after_commit
7
+
8
+ # Gemfile
9
+ gem 'test_after_commit', :group => :test
10
+
11
+ Author
12
+ ======
13
+
14
+ Inspired by https://gist.github.com/1305285
15
+
16
+ [Michael Grosser](http://grosser.it)<br/>
17
+ michael@grosser.it<br/>
18
+ License: MIT<br/>
19
+ [![Build Status](https://secure.travis-ci.org/grosser/test_after_commit.png)](http://travis-ci.org/grosser/test_after_commit)
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source :rubygems
4
+
5
+ gem "rake"
6
+ gem "rspec", "~>2"
7
+ gem "sqlite3"
8
+ gem "appraisal"
9
+ gem "activerecord", "~> 3.0.15"
10
+
11
+ gemspec :path=>"../"
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: /Users/mgrosser/code/tools/test_after_commit
3
+ specs:
4
+ test_after_commit (0.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.0.16)
10
+ activesupport (= 3.0.16)
11
+ builder (~> 2.1.2)
12
+ i18n (~> 0.5.0)
13
+ activerecord (3.0.16)
14
+ activemodel (= 3.0.16)
15
+ activesupport (= 3.0.16)
16
+ arel (~> 2.0.10)
17
+ tzinfo (~> 0.3.23)
18
+ activesupport (3.0.16)
19
+ appraisal (0.4.1)
20
+ bundler
21
+ rake
22
+ arel (2.0.10)
23
+ builder (2.1.2)
24
+ diff-lcs (1.1.3)
25
+ i18n (0.5.0)
26
+ rake (0.9.2.2)
27
+ rspec (2.11.0)
28
+ rspec-core (~> 2.11.0)
29
+ rspec-expectations (~> 2.11.0)
30
+ rspec-mocks (~> 2.11.0)
31
+ rspec-core (2.11.1)
32
+ rspec-expectations (2.11.2)
33
+ diff-lcs (~> 1.1.3)
34
+ rspec-mocks (2.11.1)
35
+ sqlite3 (1.3.6)
36
+ tzinfo (0.3.33)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ activerecord (~> 3.0.15)
43
+ appraisal
44
+ rake
45
+ rspec (~> 2)
46
+ sqlite3
47
+ test_after_commit!
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source :rubygems
4
+
5
+ gem "rake"
6
+ gem "rspec", "~>2"
7
+ gem "sqlite3"
8
+ gem "appraisal"
9
+ gem "activerecord", "~> 3.1.6"
10
+
11
+ gemspec :path=>"../"
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: /Users/mgrosser/code/tools/test_after_commit
3
+ specs:
4
+ test_after_commit (0.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.1.7)
10
+ activesupport (= 3.1.7)
11
+ builder (~> 3.0.0)
12
+ i18n (~> 0.6)
13
+ activerecord (3.1.7)
14
+ activemodel (= 3.1.7)
15
+ activesupport (= 3.1.7)
16
+ arel (~> 2.2.3)
17
+ tzinfo (~> 0.3.29)
18
+ activesupport (3.1.7)
19
+ multi_json (>= 1.0, < 1.3)
20
+ appraisal (0.4.1)
21
+ bundler
22
+ rake
23
+ arel (2.2.3)
24
+ builder (3.0.0)
25
+ diff-lcs (1.1.3)
26
+ i18n (0.6.0)
27
+ multi_json (1.2.0)
28
+ rake (0.9.2.2)
29
+ rspec (2.11.0)
30
+ rspec-core (~> 2.11.0)
31
+ rspec-expectations (~> 2.11.0)
32
+ rspec-mocks (~> 2.11.0)
33
+ rspec-core (2.11.1)
34
+ rspec-expectations (2.11.2)
35
+ diff-lcs (~> 1.1.3)
36
+ rspec-mocks (2.11.1)
37
+ sqlite3 (1.3.6)
38
+ tzinfo (0.3.33)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ activerecord (~> 3.1.6)
45
+ appraisal
46
+ rake
47
+ rspec (~> 2)
48
+ sqlite3
49
+ test_after_commit!
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source :rubygems
4
+
5
+ gem "rake"
6
+ gem "rspec", "~>2"
7
+ gem "sqlite3"
8
+ gem "appraisal"
9
+ gem "activerecord", "~> 3.2.6"
10
+
11
+ gemspec :path=>"../"
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: /Users/mgrosser/code/tools/test_after_commit
3
+ specs:
4
+ test_after_commit (0.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.2.7)
10
+ activesupport (= 3.2.7)
11
+ builder (~> 3.0.0)
12
+ activerecord (3.2.7)
13
+ activemodel (= 3.2.7)
14
+ activesupport (= 3.2.7)
15
+ arel (~> 3.0.2)
16
+ tzinfo (~> 0.3.29)
17
+ activesupport (3.2.7)
18
+ i18n (~> 0.6)
19
+ multi_json (~> 1.0)
20
+ appraisal (0.4.1)
21
+ bundler
22
+ rake
23
+ arel (3.0.2)
24
+ builder (3.0.0)
25
+ diff-lcs (1.1.3)
26
+ i18n (0.6.0)
27
+ multi_json (1.3.6)
28
+ rake (0.9.2.2)
29
+ rspec (2.11.0)
30
+ rspec-core (~> 2.11.0)
31
+ rspec-expectations (~> 2.11.0)
32
+ rspec-mocks (~> 2.11.0)
33
+ rspec-core (2.11.1)
34
+ rspec-expectations (2.11.2)
35
+ diff-lcs (~> 1.1.3)
36
+ rspec-mocks (2.11.1)
37
+ sqlite3 (1.3.6)
38
+ tzinfo (0.3.33)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ activerecord (~> 3.2.6)
45
+ appraisal
46
+ rake
47
+ rspec (~> 2)
48
+ sqlite3
49
+ test_after_commit!
@@ -0,0 +1,65 @@
1
+ require 'test_after_commit/version'
2
+
3
+ module TestAfterCommit
4
+ end
5
+
6
+ # https://gist.github.com/1305285 + removed RSpec specific code
7
+ ActiveRecord::ConnectionAdapters::DatabaseStatements.class_eval do
8
+ #
9
+ # Run the normal transaction method; when it's done, check to see if there
10
+ # is exactly one open transaction. If so, that's the transactional
11
+ # fixtures transaction; from the model's standpoint, the completed
12
+ # transaction is the real deal. Send commit callbacks to models.
13
+ #
14
+ # If the transaction block raises a Rollback, we need to know, so we don't
15
+ # call the commit hooks. Other exceptions don't need to be explicitly
16
+ # accounted for since they will raise uncaught through this method and
17
+ # prevent the code after the hook from running.
18
+ #
19
+ def transaction_with_transactional_fixtures(*args)
20
+ return_value = nil
21
+ rolled_back = false
22
+
23
+ transaction_without_transactional_fixtures(*args) do
24
+ begin
25
+ return_value = yield
26
+ rescue ActiveRecord::Rollback
27
+ rolled_back = true
28
+ raise
29
+ end
30
+ end
31
+
32
+ commit_transaction_records(false) if not rolled_back and open_transactions == 1
33
+
34
+ return_value
35
+ end
36
+ alias_method_chain :transaction, :transactional_fixtures
37
+
38
+ #
39
+ # The @_current_transaction_records is a stack of arrays, each one
40
+ # containing the records associated with the corresponding transaction
41
+ # in the transaction stack. This is used by the
42
+ # `rollback_transaction_records` method (to only send a rollback hook to
43
+ # models attached to the transaction being rolled back) but is usually
44
+ # ignored by the `commit_transaction_records` method. Here we
45
+ # monkey-patch it to temporarily replace the array with only the records
46
+ # for the top-of-stack transaction, so the real
47
+ # `commit_transaction_records` method only sends callbacks to those.
48
+ #
49
+ def commit_transaction_records_with_transactional_fixtures(commit = true)
50
+ return commit_transaction_records_without_transactional_fixtures if commit
51
+
52
+ preserving_current_transaction_records do
53
+ @_current_transaction_records = @_current_transaction_records.pop || []
54
+ commit_transaction_records_without_transactional_fixtures
55
+ end
56
+ end
57
+ alias_method_chain :commit_transaction_records, :transactional_fixtures
58
+
59
+ def preserving_current_transaction_records
60
+ old_current_transaction_records = @_current_transaction_records.dup
61
+ yield
62
+ ensure
63
+ @_current_transaction_records = old_current_transaction_records
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module TestAfterCommit
2
+ VERSION = '0.0.1'
3
+ end
data/spec/database.rb ADDED
@@ -0,0 +1,51 @@
1
+ # setup database
2
+ require 'active_record'
3
+
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => 'sqlite3',
6
+ :database => ':memory:'
7
+ )
8
+
9
+ ActiveRecord::Migration.verbose = false
10
+
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table "cars", :force => true do |t|
13
+ end
14
+ end
15
+
16
+ class Car < ActiveRecord::Base
17
+ after_commit :simple_after_commit
18
+ after_commit :simple_after_commit_on_create, :on => :create
19
+ after_commit :simple_after_commit_on_update, :on => :update
20
+
21
+ after_save :trigger_rollback
22
+
23
+ attr_accessor :make_rollback
24
+
25
+ def self.called(x=nil)
26
+ @called ||= []
27
+ if x
28
+ @called << x
29
+ else
30
+ @called
31
+ end
32
+ end
33
+
34
+ def trigger_rollback
35
+ raise ActiveRecord::Rollback if make_rollback
36
+ end
37
+
38
+ private
39
+
40
+ def simple_after_commit
41
+ self.class.called :always
42
+ end
43
+
44
+ def simple_after_commit_on_create
45
+ self.class.called :create
46
+ end
47
+
48
+ def simple_after_commit_on_update
49
+ self.class.called :update
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require File.expand_path '../database', __FILE__
3
+ require 'test_after_commit'
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe TestAfterCommit do
4
+ around do |example|
5
+ Car.transaction do # simulate transactional fixtures
6
+ Car.called.clear
7
+ example.call
8
+ end
9
+ end
10
+
11
+ it "has a VERSION" do
12
+ TestAfterCommit::VERSION.should =~ /^[\.\da-z]+$/
13
+ end
14
+
15
+ it "fires on create" do
16
+ Car.create
17
+ Car.called.should == [:create, :always]
18
+ end
19
+
20
+ it "fires on update" do
21
+ car = Car.create
22
+ Car.called.clear
23
+ car.save!
24
+ Car.called.should == [:update, :always]
25
+ end
26
+
27
+ it "does not fire on rollback" do
28
+ car = Car.new
29
+ car.make_rollback = true
30
+ car.save.should == nil
31
+ Car.called.should == []
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ name = "test_after_commit"
3
+ require "#{name}/version"
4
+
5
+ Gem::Specification.new name, TestAfterCommit::VERSION do |s|
6
+ s.summary = "makes after_commit callbacks testable in Rails 3+ with transactional_fixtures"
7
+ s.authors = ["Michael Grosser"]
8
+ s.email = "michael@grosser.it"
9
+ s.homepage = "http://github.com/grosser/#{name}"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.license = 'MIT'
12
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_after_commit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Grosser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: michael@grosser.it
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .travis.yml
21
+ - Appraisals
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - Rakefile
25
+ - Readme.md
26
+ - gemfiles/30.gemfile
27
+ - gemfiles/30.gemfile.lock
28
+ - gemfiles/31.gemfile
29
+ - gemfiles/31.gemfile.lock
30
+ - gemfiles/32.gemfile
31
+ - gemfiles/32.gemfile.lock
32
+ - lib/test_after_commit.rb
33
+ - lib/test_after_commit/version.rb
34
+ - spec/database.rb
35
+ - spec/spec_helper.rb
36
+ - spec/test_after_commit_spec.rb
37
+ - test_after_commit.gemspec
38
+ homepage: http://github.com/grosser/test_after_commit
39
+ licenses:
40
+ - MIT
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ segments:
52
+ - 0
53
+ hash: -3378510041846892238
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ segments:
61
+ - 0
62
+ hash: -3378510041846892238
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.24
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: makes after_commit callbacks testable in Rails 3+ with transactional_fixtures
69
+ test_files: []