extra-after-commit-callbacks 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011 Charles Barbier <http://github.com/unixcharles>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ extra-after-commit-callbacks
2
+ ============================
3
+
4
+ Add extra callbacks for ActiveRecord models and observers.
5
+
6
+ Replacement for ActiveRecord models `after_commit :method_name, :on => :create`.
7
+
8
+ Getting started
9
+ ===============
10
+
11
+ ```
12
+ gem 'extra-after-commit-callbacks'
13
+ ```
14
+
15
+ #### Directly from the an ActiveRecord model:
16
+
17
+ ```ruby
18
+ class Model < ActiveRecord::Base
19
+ after_commit_on_create :test
20
+ end
21
+ ```
22
+
23
+ #### From an ActiveRecord observer:
24
+
25
+ ```ruby
26
+ class AnotherObserver < ActiveRecord::Observer
27
+ observe :model
28
+
29
+ def after_commit(object)
30
+ # ...
31
+ end
32
+
33
+ def after_commit_on_create(object)
34
+ # ...
35
+ end
36
+
37
+ def after_commit_on_save(object)
38
+ # ...
39
+ end
40
+
41
+ def after_commit_on_update(object)
42
+ # ...
43
+ end
44
+
45
+ def after_commit_on_destroy(object)
46
+ # ...
47
+ end
48
+ end
49
+ ```
50
+
51
+ ### How this differ from `after_commit :on => :{create/update/destroy}`
52
+
53
+ First, you can't use the `:on` option from the observers.
54
+
55
+ Also, the behaviour of this Gem is different from the `:on` option because it doesn't interfere with other object inside the transaction.
56
+
57
+ When you `:on` with `after_commit` in Rails, if any of the objects being commited within the transaction fullfill the requirement, the callback will be triggered.
58
+
59
+ #### i.e.:
60
+
61
+ With `after_commit :on` option:
62
+
63
+ ```ruby
64
+ class Boat < ActiveRecord::Base
65
+ after_commit :on => :destroy do
66
+ puts 'The boat sink...'
67
+ end
68
+ end
69
+
70
+ class Plane < ActiveRecord::Base
71
+ after_commit :on => :destroy do
72
+ puts 'The plane crash...'
73
+ end
74
+ end
75
+
76
+ > boat, plane = Boat.create, Plane.create; ActiveRecord::Base.transaction { boat.save; plane.destroy }
77
+ (0.3ms) BEGIN
78
+ ...
79
+ (0.2ms) COMMIT
80
+ 'The boat sink...'
81
+ 'The plane crash...'
82
+ => nil
83
+ ```
84
+
85
+ Pull request?
86
+ =============
87
+ Yes.
@@ -0,0 +1,2 @@
1
+ require 'extra-after-commit-callbacks/active_record_extension'
2
+ require 'extra-after-commit-callbacks/railtie' if defined?(Rails)
@@ -0,0 +1,62 @@
1
+ require 'active_support/concern'
2
+
3
+ module ExtraAfterCommitCallbacks
4
+ module ActiveRecordExtension
5
+ extend ActiveSupport::Concern
6
+
7
+ EXTRA_AFTER_COMMIT_CALLBACK_TYPES = [:commit_on_save, :commit_on_create, :commit_on_update, :commit_on_destroy]
8
+
9
+ included do
10
+ EXTRA_AFTER_COMMIT_CALLBACK_TYPES.each do |callback_type|
11
+ define_model_callbacks callback_type, :only => :after
12
+ end
13
+
14
+ before_save :_mark_for_after_commit_on_save!
15
+ before_create :_mark_for_after_commit_on_create!
16
+ before_update :_mark_for_after_commit_on_update!
17
+ before_destroy :_mark_for_after_commit_on_destroy!
18
+
19
+ after_commit :_run_extra_after_commit_callbacks!
20
+ after_rollback :_clear_after_commit_callbacks!
21
+ end
22
+
23
+ private
24
+
25
+ def _mark_for_after_commit_on_save!
26
+ @_trigger_after_commit_on_save_for_successfull_transaction = true
27
+ end
28
+
29
+ def _mark_for_after_commit_on_create!
30
+ @_trigger_after_commit_on_create_for_successfull_transaction = true
31
+ end
32
+
33
+ def _mark_for_after_commit_on_update!
34
+ @_trigger_after_commit_on_update_for_successfull_transaction = true
35
+ end
36
+
37
+ def _mark_for_after_commit_on_destroy!
38
+ @_trigger_after_commit_on_destroy_for_successfull_transaction = true
39
+ end
40
+
41
+ def _run_extra_after_commit_callbacks!
42
+ notify_observers(:after_commit_on_save) if @_trigger_after_commit_on_save_for_successfull_transaction
43
+ notify_observers(:after_commit_on_create) if @_trigger_after_commit_on_create_for_successfull_transaction
44
+ notify_observers(:after_commit_on_update) if @_trigger_after_commit_on_update_for_successfull_transaction
45
+ notify_observers(:after_commit_on_destroy) if @_trigger_after_commit_on_destroy_for_successfull_transaction
46
+
47
+ run_callbacks :commit_on_save, :only => :after if @_trigger_after_commit_on_save_for_successfull_transaction
48
+ run_callbacks :commit_on_create, :only => :after if @_trigger_after_commit_on_create_for_successfull_transaction
49
+ run_callbacks :commit_on_update, :only => :after if @_trigger_after_commit_on_update_for_successfull_transaction
50
+ run_callbacks :commit_on_destroy, :only => :after if @_trigger_after_commit_on_destroy_for_successfull_transaction
51
+
52
+ _clear_after_commit_callbacks!
53
+ end
54
+
55
+ def _clear_after_commit_callbacks!
56
+ @_trigger_after_commit_on_save_for_successfull_transaction = false
57
+ @_trigger_after_commit_on_create_for_successfull_transaction = false
58
+ @_trigger_after_commit_on_update_for_successfull_transaction = false
59
+ @_trigger_after_commit_on_destroy_for_successfull_transaction = false
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ module ExtraAfterCommitCallbacks
2
+ class Initialization < Rails::Railtie
3
+ initializer "extra_after_commit_callbacks" do
4
+ ::ActiveRecord::Base.send(:include, ::ExtraAfterCommitCallbacks::ActiveRecordExtension)
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extra-after-commit-callbacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Barbier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.1.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.1.0
46
+ description:
47
+ email: unixcharles@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - README.md
53
+ - LICENSE
54
+ - lib/extra-after-commit-callbacks/active_record_extension.rb
55
+ - lib/extra-after-commit-callbacks/railtie.rb
56
+ - lib/extra-after-commit-callbacks.rb
57
+ homepage: http://github.com/unixcharles/extra-after-commit-callbacks
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.23
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Fine grained after_commit callbacks for ActiveRecord observers
81
+ test_files: []