Skip to content

Commit

Permalink
Merge pull request #1131 from jamiemccarthy/jm-unique-validation-fix
Browse files Browse the repository at this point in the history
[Fix #1130] Fix crash for Rails/UniqueValidationWithoutIndex with bare validate
  • Loading branch information
koic authored Sep 25, 2023
2 parents d3117ff + bd4f92b commit 7a0208b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_bug_unique_validation_without_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1130](https://github.com/rubocop/rubocop-rails/issues/1130): Fix crash for `Rails/UniqueValidationWithoutIndex` with bare validate. ([@jamiemccarthy][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/unique_validation_without_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def class_node(node)

def uniqueness_part(node)
pairs = node.arguments.last
return unless pairs.hash_type?
return unless pairs&.hash_type?

pairs.each_pair.find do |pair|
next unless pair.key.sym_type? && pair.key.value == :uniqueness
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rails/unique_validation_without_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ class User
end
RUBY
end

it 'ignores a bare validates directive by itself' do
expect_no_offenses(<<~RUBY)
class User
validates
end
RUBY
end

it 'ignores a bare validates directive among others' do
expect_no_offenses(<<~RUBY)
class User
validates
after_commit :foo
def foo; true; end
end
RUBY
end
end

context 'when the table has an index but it is not unique' do
Expand Down

0 comments on commit 7a0208b

Please sign in to comment.