Skip to content

Commit

Permalink
[Fix #1130] Fix crash for Rails/UniqueValidationWithoutIndex with bar…
Browse files Browse the repository at this point in the history
…e validate
  • Loading branch information
jamiemccarthy committed Sep 24, 2023
1 parent d3117ff commit bd4f92b
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 bd4f92b

Please sign in to comment.