Skip to content

Commit

Permalink
Remove explicit after_commit_action dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
stokarenko committed Mar 9, 2020
1 parent 4751240 commit 55a01ec
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Appraisals
Expand Up @@ -65,7 +65,9 @@ appraise 'rails_5.2' do
end

appraise 'norails' do
gem 'sqlite3', '~> 1.3', '>= 1.3.5', platforms: :ruby
gem 'rails', install_if: false
gem 'after_commit_action', install_if: false
gem 'sequel'
gem 'redis-objects'
end
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -4,3 +4,4 @@ gemspec

gem 'sqlite3', '~> 1.3.5', :platforms => :ruby
gem 'rails', '5.1.4'
gem 'after_commit_action', '~> 1.0'
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -981,6 +981,12 @@ job.run
job.save! #notify_about_running_job is not run
```

Please note that `:after_commit` AASM callbacks behaves around custom implementation
of transaction pattern rather than a real-life DB transaction. This fact still causes
the race conditions and redundant callback calls within nested transaction. In order
to fix that it's highly recommended to add `gem 'after_commit_action', '~> 1.0'` to your
`Gemfile`.

If you want to encapsulate state changes within an own transaction, the behavior
of this nested transaction might be confusing. Take a look at
[ActiveRecord Nested Transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html)
Expand Down
1 change: 0 additions & 1 deletion aasm.gemspec
Expand Up @@ -17,7 +17,6 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 1.9.3'

s.add_dependency 'concurrent-ruby', '~> 1.0'
s.add_dependency 'after_commit_action', '~> 1.0'

s.add_development_dependency 'rake'
s.add_development_dependency 'sdoc'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/norails.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3", ">= 1.3.5", platforms: :ruby
gem "rails", install_if: false
gem "after_commit_action", install_if: false
gem "sequel"
gem "redis-objects"

Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_3.2.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "~> 3.2.22"
gem "after_commit_action", "~> 1.0"
gem "mongoid", "~> 3.1"
gem "sequel"
gem "bson_ext", platforms: :ruby
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_4.2.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "4.2.5"
gem "after_commit_action", "~> 1.0"
gem "nokogiri", "1.6.8.1", platforms: [:ruby_19]
gem "mime-types", "~> 2", platforms: [:ruby_19, :jruby]
gem "mongoid", "~> 4.0"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_4.2_mongoid_5.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "4.2.5"
gem "after_commit_action", "~> 1.0"
gem "mime-types", "~> 2", platforms: [:ruby_19, :jruby]
gem "mongoid", "~> 5.0"
gem "activerecord-jdbcsqlite3-adapter", "1.3.24", platforms: :jruby
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_4.2_nobrainer.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "4.2.5"
gem "after_commit_action", "~> 1.0"
gem "nobrainer", "~> 0.33.0"

gemspec path: "../"
1 change: 1 addition & 0 deletions gemfiles/rails_5.0.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "5.0.0"
gem "after_commit_action", "~> 1.0"
gem "mongoid", "~> 6.0"
gem "sequel"
gem "dynamoid", "~> 1.3", platforms: :ruby
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_5.0_nobrainer.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "5.0.0"
gem "after_commit_action", "~> 1.0"
gem "nobrainer", "~> 0.33.0"

gemspec path: "../"
1 change: 1 addition & 0 deletions gemfiles/rails_5.1.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "5.1"
gem "after_commit_action", "~> 1.0"
gem "mongoid", "~>6.0"
gem "sequel"
gem "dynamoid", "~> 1.3", platforms: :ruby
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_5.2.gemfile
Expand Up @@ -4,6 +4,7 @@ source "https://rubygems.org"

gem "sqlite3", "~> 1.3.5", platforms: :ruby
gem "rails", "5.2"
gem "after_commit_action", "~> 1.0"
gem "mongoid", "~>6.0"
gem "sequel"
gem "dynamoid", "~>2.2", platforms: :ruby
Expand Down
19 changes: 11 additions & 8 deletions lib/aasm/persistence/active_record_persistence.rb
@@ -1,4 +1,3 @@
require 'after_commit_action'
require 'aasm/persistence/orm'
module AASM
module Persistence
Expand Down Expand Up @@ -29,7 +28,17 @@ module ActiveRecordPersistence
# end
#
def self.included(base)
base.send(:include, ::AfterCommitAction) unless base.include?(::AfterCommitAction)
begin
require 'after_commit_action'
base.send(:include, ::AfterCommitAction) unless base.include?(::AfterCommitAction)
base.send(:alias_method, :aasm_execute_after_commit, :execute_after_commit)
rescue LoadError
warn <<-MSG
[DEPRECATION] :after_commit AASM callback is not safe in terms of race conditions and redundant calls.
Please add `gem 'after_commit_action', '~> 1.0'` to your Gemfile in order to fix that.
MSG
end

base.send(:include, AASM::Persistence::Base)
base.send(:include, AASM::Persistence::ORM)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
Expand Down Expand Up @@ -90,12 +99,6 @@ def aasm_transaction(requires_new, requires_lock)
end
end

def aasm_execute_after_commit
execute_after_commit do
yield
end
end

def aasm_enum(name=:default)
case AASM::StateMachineStore.fetch(self.class, true).machine(name).config.enum
when false then nil
Expand Down

0 comments on commit 55a01ec

Please sign in to comment.