after_commit_helper 0.0.2

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: a2fba451aa4ea84ab0ce1540ce2a5fcdb7dc3bad
4
+ data.tar.gz: 2de62856585a5d4467d1cd1031e177cb62d06092
5
+ SHA512:
6
+ metadata.gz: dd4d05958e8e74079d8ac2de7dfabf236ffc84159da77ca57ea664a9e08c1d78db0505bdd715a4051a15dbd7640454b468f2d335d0509f658d5be64990c60fb3
7
+ data.tar.gz: f1a4fa47973161e23901860a25eef9e98e6c7fa591b81e5eb367eeb37744723eac183bf939d77e6161124a4a382c32544badbac1895d2f6c912dc495f3a313e5
@@ -0,0 +1,57 @@
1
+ module AfterCommitHelper
2
+ module Concern
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # define the after_commit callbacks
7
+ after_commit :process_after_commit_callbacks, on: [:create, :update, :destroy]
8
+ end
9
+
10
+ # returns the after commit callbacks to be executed
11
+ def after_commit_callbacks
12
+ @after_commit_callbacks ||= []
13
+ end
14
+
15
+ # resets the after commit callbacks
16
+ def reset_after_commit_callbacks
17
+ @after_commit_callbacks = []
18
+ end
19
+
20
+ # Add a callback to be fired once this record commits
21
+ def add_after_commit_callback(&callback)
22
+ after_commit_callbacks << callback
23
+ AfterCommitHelper::Logger.logger.debug 'callback added for after_commit hook'
24
+ end
25
+
26
+ # Fires all of the procs one at a time, any exceptions
27
+ # will be rescued and logged, but will not be rethrown
28
+ def process_after_commit_callbacks
29
+ AfterCommitHelper::Logger.logger.debug "Processing #{after_commit_callbacks.length} callback for the after_commit hook" if after_commit_callbacks.length > 0
30
+
31
+ @after_commit_callbacks.each do |callback|
32
+ begin
33
+ callback.call
34
+ rescue Exception => e
35
+ AfterCommitHelper::Logger.logger.debug "Exception while processing AfterCommitHelper proc: #{e.message}"
36
+ end
37
+ end
38
+
39
+ # remove all of the procs, we execute one time only
40
+ reset_after_commit_callbacks
41
+ end
42
+ end
43
+
44
+ module Logger
45
+ require 'logger'
46
+
47
+ class << self
48
+ attr_writer :logger
49
+
50
+ def logger
51
+ @logger ||= ::Logger.new($stdout).tap do |log|
52
+ log.progname = self.name
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
data/spec/database.rb ADDED
@@ -0,0 +1,29 @@
1
+ # setup database
2
+ require 'active_record'
3
+ require 'after_commit_helper'
4
+
5
+ ActiveRecord::Base.establish_connection(
6
+ :adapter => 'sqlite3',
7
+ :database => ':memory:'
8
+ )
9
+
10
+ ActiveRecord::Migration.verbose = false
11
+
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table 'tests', :force => true do |t|
14
+ t.integer :value, :default => 0, :null => false
15
+ t.timestamps :null => true
16
+ end
17
+ end
18
+
19
+ class Test < ActiveRecord::Base
20
+ include AfterCommitHelper::Concern
21
+
22
+ def test_values
23
+ @test_values ||= []
24
+ end
25
+
26
+ def called
27
+ $called = true
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require File.expand_path '../database', __FILE__
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'after_commit_helper' do
4
+ before(:each) do
5
+ $called = nil
6
+ end
7
+
8
+ after(:each) do
9
+ $called = nil
10
+ end
11
+
12
+ it 'should leave called false' do
13
+ test = Test.new value: 1
14
+
15
+ expect($called).to eq nil
16
+
17
+ test.save!
18
+
19
+ expect($called).to eq nil
20
+ end
21
+
22
+ it 'should set called to true' do
23
+ test = Test.new value: 2
24
+
25
+ expect($called).to eq nil
26
+
27
+ test.add_after_commit_callback do
28
+ test.called
29
+ end
30
+
31
+ expect($called).to eq nil
32
+
33
+ test.save!
34
+
35
+ expect($called).to eq true
36
+ end
37
+
38
+ it 'should set called to true only after transaction commits' do
39
+ Test.transaction do
40
+ test = Test.new value: 3
41
+
42
+ expect($called).to eq nil
43
+
44
+ test.add_after_commit_callback do
45
+ test.called
46
+ end
47
+
48
+ expect($called).to eq nil
49
+
50
+ test.save!
51
+
52
+ expect($called).to eq nil
53
+ end
54
+
55
+ # Now it should be set since transaction is committed
56
+ expect($called).to eq true
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: after_commit_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gregory Hodum
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-22 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: sqlite3
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Conditionally execute after_commit for ActiveRecord
70
+ email: gregory.hodum@yahoo.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/after_commit_helper.rb
76
+ - spec/database.rb
77
+ - spec/spec_helper.rb
78
+ - spec/test_after_commit_helper_spec.rb
79
+ homepage: https://github.com/ghodum
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: AfterCommitHelper
103
+ test_files:
104
+ - spec/database.rb
105
+ - spec/spec_helper.rb
106
+ - spec/test_after_commit_helper_spec.rb