Skip to content

Commit

Permalink
Merge pull request #347 from pocke/fix-lambada-after-commit
Browse files Browse the repository at this point in the history
[Fix #345] Fix error of `Rails/AfterCommitOverride` on `after_commit`with a lambda
  • Loading branch information
koic authored Sep 7, 2020
2 parents edd3277 + 81df724 commit 1bda5d2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#345](https://github.com/rubocop-hq/rubocop-rails/issues/345): Fix error of `Rails/AfterCommitOverride` on `after_commit` with a lambda. ([@pocke][])

## 2.8.0 (2020-09-04)

### New features
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/rails/after_commit_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def on_class(class_node)

def each_after_commit_callback(class_node)
class_send_nodes(class_node).each do |node|
yield node if after_commit_callback?(node)
yield node if after_commit_callback?(node) && named_callback?(node)
end
end

Expand All @@ -78,6 +78,13 @@ def class_send_nodes(class_node)
def after_commit_callback?(node)
AFTER_COMMIT_CALLBACKS.include?(node.method_name)
end

def named_callback?(node)
name = node.first_argument
return false unless name

name.sym_type?
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rails/after_commit_override_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ class User < ApplicationRecord
end
RUBY
end

it 'does not register an offense when the callbacks are defined with lambdas' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
after_commit -> { foo }
after_destroy_commit -> { foo }
end
RUBY
end

it 'ignores after_commit method call without arguments' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
after_commit
after_destroy_commit
end
RUBY
end
end

0 comments on commit 1bda5d2

Please sign in to comment.