activerecord-after-transaction 0.0.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: b1e3edc231e68ba16701de6e1e278e48da20622c
4
+ data.tar.gz: e963ec2be9923994eed49b14e1fc4adb7c27f764
5
+ SHA512:
6
+ metadata.gz: a59648ccafe1fcd5397957aa11a8db00dc85eb0cb843f6c98cb6ffa9a9ac69a2d314b19ce0dd5213d9a2cc5e9baa9dde5606d523117e50a6567aca3b777530d6
7
+ data.tar.gz: c0dae729f4d14bfc08d9ca7c44e5e3e8db98889648ee91375961809474a0604ee032e0539fdf180e1f89422775ed5b2d151d5f19327ab964a4a21b525bb7a4ed
data/.gitignore ADDED
@@ -0,0 +1,33 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+ /log/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalisation:
25
+ /.bundle/
26
+ /lib/bundler/man/
27
+
28
+ ## Misc
29
+ Gemfile.lock
30
+ gemfiles/*.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+
8
+ env:
9
+ -
10
+ - ACTIVE_RECORD_VERSION=3.2.0
11
+ - ACTIVE_RECORD_VERSION=4.0.0
12
+ - ACTIVE_RECORD_VERSION=4.1.0
13
+ - ACTIVE_RECORD_VERSION=4.2.0.beta4
14
+
15
+ script: "bundle exec rake spec"
16
+
17
+ branches:
18
+ only:
19
+ - master
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', "~> #{ENV['ACTION_RECORD_VERSION']}" if ENV['ACTION_RECORD_VERSION'].to_s != ''
4
+
5
+ gem "sqlite3"
6
+ gem 'database_cleaner'
7
+ gem "codeclimate-test-reporter", require: nil
8
+
9
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Daisuke Taniwaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # activerecord-after-transaction
2
+
3
+ [![Gem Version][gem-image]][gem-link]
4
+ [![Dependency Status][deps-image]][deps-link]
5
+ [![Build Status][build-image]][build-link]
6
+ [![Coverage Status][cov-image]][cov-link]
7
+ [![Code Climate][gpa-image]][gpa-link]
8
+
9
+ Execute a proc after transaction.
10
+
11
+ You basically don't need this gem if you use `after_commit`. However, let's say you have `state_machine` gem and want to do something after transition, the gem doesn't provide a way to do it. In this case, you might have written the following code.
12
+
13
+ ```ruby
14
+ def after_commit(record)
15
+ if record.previous_changes[:state] == ['draft', 'published']
16
+ UserMailer.notify_publish(record).deliver
17
+ elsif record.previous_changes[:state] == ['draft', 'deleted']
18
+ UserMailer.notify_delete(record).deliver
19
+ end
20
+ end
21
+ ```
22
+
23
+ It spoils the readability of `state_machine`. You definitely want to write the following instead.
24
+
25
+ ```ruby
26
+ after_transition from: :draft, to: :published do
27
+ UserMailer.notify_publish(record).deliver
28
+ end
29
+ after_transition from: :draft, to: :deleted do
30
+ UserMailer.notify_publish(record).deliver
31
+ end
32
+ ```
33
+
34
+ That's why I made this gem. :)
35
+
36
+ ## Installation
37
+
38
+ Add the activerecord-after-transaction gem to your Gemfile.
39
+
40
+ ```ruby
41
+ gem "activerecord-after-transaction"
42
+ ```
43
+
44
+ And run `bundle install`.
45
+
46
+ ## Usage
47
+
48
+ ```ruby
49
+ class Foo < ActiveRecord::Base
50
+ after_transition from: :draft, to: :published do
51
+ after_transaction do
52
+ UserMailer.notify_publish(record).deliver
53
+ end
54
+ end
55
+ after_transition from: :draft, to: :deleted do
56
+ after_transaction do
57
+ UserMailer.notify_publish(record).deliver
58
+ end
59
+ end
60
+ end
61
+ ```
62
+
63
+ Then, the proc will be executed after the transaction. If it's not in the transaction, it will be executed immediately.
64
+
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new [Pull Request](../../pull/new/master)
73
+
74
+ ## Copyright
75
+
76
+ Copyright (c) 2014 Daisuke Taniwaki. See [LICENSE](LICENSE) for details.
77
+
78
+
79
+
80
+ [gem-image]: https://badge.fury.io/rb/activerecord-after-transaction.svg
81
+ [gem-link]: http://badge.fury.io/rb/activerecord-after-transaction
82
+ [build-image]: https://secure.travis-ci.org/dtaniwaki/activerecord-after-transaction.png
83
+ [build-link]: http://travis-ci.org/dtaniwaki/activerecord-after-transaction
84
+ [deps-image]: https://gemnasium.com/dtaniwaki/activerecord-after-transaction.svg
85
+ [deps-link]: https://gemnasium.com/dtaniwaki/activerecord-after-transaction
86
+ [cov-image]: https://codeclimate.com/github/dtaniwaki/activerecord-after-transaction/badges/coverage.svg
87
+ [cov-link]: https://codeclimate.com/github/dtaniwaki/activerecord-after-transaction
88
+ [gpa-image]: https://codeclimate.com/github/dtaniwaki/activerecord-after-transaction.png
89
+ [gpa-link]: https://codeclimate.com/github/dtaniwaki/activerecord-after-transaction
90
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "activerecord-after-transaction"
3
+ gem.version = ::File.read(::File.expand_path('../VERSION', __FILE__)).to_s.strip
4
+ gem.platform = Gem::Platform::RUBY
5
+ gem.authors = ["Daisuke Taniwaki"]
6
+ gem.email = ["daisuketaniwaki@gmail.com"]
7
+ gem.homepage = "https://github.com/dtaniwaki/activerecord-after-transaction"
8
+ gem.summary = "Exec Proc After Transaction"
9
+ gem.description = "Exec Proc After Transaction"
10
+ gem.license = "MIT"
11
+
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ gem.require_paths = ['lib']
16
+
17
+ gem.add_dependency "activerecord", ">= 3.2"
18
+
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec", ">= 3.0"
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+ require 'activerecord_after_transaction/version'
3
+ require 'activerecord_after_transaction/methods'
4
+
5
+ ActiveSupport.on_load :active_record do
6
+ include ActiveRecord::AfterTransaction::Methods
7
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord::AfterTransaction
4
+ module Methods
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ after_commit :execute_after_transaction
8
+ after_rollback :clear_after_transaction
9
+ end
10
+ end
11
+
12
+ def after_transaction(&block)
13
+ return block.call unless self.class.connection.open_transactions
14
+ @after_transaction_queue ||= []
15
+ @after_transaction_queue.push block
16
+ logger && logger.debug("Push #{block}")
17
+ end
18
+
19
+ def execute_after_transaction
20
+ logger && logger.debug("Execute the queue")
21
+ @after_transaction_queue.each do |block|
22
+ logger && logger.debug("Execute #{block}")
23
+ block.call
24
+ end
25
+ clear_after_transaction
26
+ end
27
+
28
+ def clear_after_transaction
29
+ logger && logger.debug("Clear the queue")
30
+ @after_transaction_queue.clear
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecord::AfterTransaction
2
+ VERSION = ::File.read(::File.expand_path('../../../../VERSION', __FILE__)).to_s.strip
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ require 'activerecord-after-transaction/methods'
3
+
4
+ describe ActiveRecord::AfterTransaction::Methods do
5
+ let(:klass) do
6
+ create_tmp_model foo: :integer do
7
+ include ActiveRecord::AfterTransaction::Methods
8
+ end
9
+ end
10
+ subject { klass.new }
11
+
12
+ describe 'in a transaction' do
13
+ it 'executes the proc after transaction' do
14
+ step = 1
15
+ klass.transaction do
16
+ subject.after_transaction do
17
+ step = 2
18
+ end
19
+ subject.save!
20
+ step = 3
21
+ end
22
+ expect(step).to eq 2
23
+ end
24
+ it 'clears executed procs' do
25
+ step = 1
26
+ klass.transaction do
27
+ subject.after_transaction do
28
+ step += 1
29
+ end
30
+ subject.save!
31
+ subject.save!
32
+ end
33
+ expect(step).to eq 2
34
+ end
35
+ it 'does not execute the proc after rollback' do
36
+ step = 1
37
+ klass.transaction do
38
+ subject.after_transaction do
39
+ step = 2
40
+ end
41
+ subject.save!
42
+ raise ActiveRecord::Rollback
43
+ end
44
+ expect(step).to eq 1
45
+ end
46
+ end
47
+ describe 'not in a transaction' do
48
+ it 'executes the proc immediately' do
49
+ step = 1
50
+
51
+ subject.after_transaction do
52
+ step = 2
53
+ end
54
+ expect(step).to eq 1
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ #require 'database_cleaner'
3
+ require 'active_record'
4
+ require 'simplecov'
5
+ require "codeclimate-test-reporter"
6
+ CodeClimate::TestReporter.start
7
+ # require 'coveralls'
8
+ # Coveralls.wear!
9
+
10
+ SimpleCov.start do
11
+ add_filter 'spec/'
12
+ end
13
+
14
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
15
+
16
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
17
+
18
+ RSpec.configure do |c|
19
+ # c.before :suite do
20
+ # ActiveRecord::Migration.verbose = false
21
+ # DatabaseCleaner.clean_with :truncation
22
+ # end
23
+ # c.before :example do
24
+ # DatabaseCleaner.strategy = :truncation
25
+ # DatabaseCleaner.start
26
+ # end
27
+ # c.after :example do
28
+ # DatabaseCleaner.clean
29
+ # end
30
+ end
31
+
@@ -0,0 +1,26 @@
1
+ def create_tmp_model(_columns = {}, &_block)
2
+ time = Time.now.to_f.to_s.gsub('.', '_')
3
+ _model_name = "TmpClass#{time}"
4
+ _table_name = "tmp_class#{time}s"
5
+ # if ActiveRecord::Base.connection.table_exists? _table_name
6
+ # ActiveRecord::Migration.drop_table _table_name
7
+ # end
8
+ begin
9
+ Object.send :remove_const, _model_name
10
+ rescue NameError
11
+ end
12
+
13
+ ActiveRecord::Migration.create_table _table_name do |t|
14
+ _columns.each do |name, type|
15
+ t.column name, type
16
+ end
17
+ end
18
+ Object.class_eval <<-EOS
19
+ class #{_model_name} < ActiveRecord::Base
20
+ self.table_name = '#{_table_name}'
21
+ end
22
+ EOS
23
+ klass = Object.const_get _model_name
24
+ klass.class_exec(&_block) unless _block.nil?
25
+ klass
26
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-after-transaction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daisuke Taniwaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-08 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: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Exec Proc After Transaction
56
+ email:
57
+ - daisuketaniwaki@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - VERSION
69
+ - activerecord-after-transaction.gemspec
70
+ - lib/activerecord-after-transaction.rb
71
+ - lib/activerecord-after-transaction/methods.rb
72
+ - lib/activerecord-after-transaction/version.rb
73
+ - spec/after_transaction/methods_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/support/tmp_model.rb
76
+ homepage: https://github.com/dtaniwaki/activerecord-after-transaction
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Exec Proc After Transaction
100
+ test_files:
101
+ - spec/after_transaction/methods_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/support/tmp_model.rb