Skip to content

Commit

Permalink
Enable ActiveRecord 4.2 support.
Browse files Browse the repository at this point in the history
Merges Envek/after_commit_everywhere#1

Merge branch 'ar-4-2-support'

* ar-4-2-support:
  On AR 4.2 raise exception on before_commit call as it's not implemented
  Test with ActiveRecord 4.2
  Support Rails 4.2
  • Loading branch information
andrebarretofv authored and Envek committed May 1, 2018
2 parents 7f7053c + 1faeb73 commit 1ef6eb2
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ addons:

matrix:
include:
- rvm: 2.5.1
gemfile: gemfiles/activerecord_4_2.gemfile
- rvm: 2.5.1
gemfile: gemfiles/activerecord_5_0.gemfile
- rvm: 2.5.1
Expand Down
4 changes: 4 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# frozen_string_literal: true
#
appraise 'activerecord-4-2' do
gem 'activerecord', '~> 4.2.0'
end

appraise 'activerecord-5-0' do
gem 'activerecord', '~> 5.0.0'
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Will be executed right before outermost transaction will be commited _(I can't i

If called outside transaction will execute callback immediately.

Supported only starting from ActiveRecord 5.0.

#### `after_rollback`

Will be executed right after transaction in which it have been declared was rolled back (this might be nested savepoint transaction block with `requires_new: true`).
Expand Down
2 changes: 1 addition & 1 deletion after_commit_everywhere.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'activerecord', '>= 5.0'
spec.add_dependency 'activerecord', '>= 4.2'
spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'pry'
Expand Down
7 changes: 7 additions & 0 deletions gemfiles/activerecord_4_2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "activerecord", "~> 4.2.0"

gemspec path: "../"
5 changes: 5 additions & 0 deletions lib/after_commit_everywhere.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ def after_commit(connection: ActiveRecord::Base.connection, &callback)
#
# If called outside transaction it will execute callback immediately.
#
# Available only since Ruby on Rails 5.0. See https://github.com/rails/rails/pull/18936
#
# @param connection [ActiveRecord::ConnectionAdapters::AbstractAdapter]
# @param callback [#call] Callback to be executed
# @return void
def before_commit(connection: ActiveRecord::Base.connection, &callback)
if ActiveRecord::VERSION::MAJOR < 5
raise NotImplementedError, "#{__callee__} works only with Rails 5.0+"
end
AfterCommitEverywhere.register_callback(
connection: connection,
name: __callee__,
Expand Down
2 changes: 1 addition & 1 deletion lib/after_commit_everywhere/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AfterCommitEverywhere
VERSION = '0.1.1'
VERSION = '0.1.2'
end
10 changes: 10 additions & 0 deletions spec/after_commit_everywhere_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@
end
end

if ActiveRecord::VERSION::MAJOR < 5
it 'fails because it is unsupported in Rails 4' do
expect { subject }.to raise_error(NotImplementedError) do |ex|
expect(ex.message).to match(/Rails 5\.0\+/)
end
end

next
end

context 'within transaction' do
it 'executes code only before commit' do
ActiveRecord::Base.transaction do
Expand Down

0 comments on commit 1ef6eb2

Please sign in to comment.