Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #214] Fix an error for Rails/UniqueValidationWithoutIndex #218

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#213](https://github.com/rubocop-hq/rubocop-rails/pull/213): Fix a false positive for `Rails/UniqueValidationWithoutIndex` when using conditions. ([@sunny][])
* [#215](https://github.com/rubocop-hq/rubocop-rails/issues/215): Fix a false positive for `Rails/UniqueValidationWithoutIndex` when using Expression Indexes. ([@koic][])
* [#214](https://github.com/rubocop-hq/rubocop-rails/issues/214): Fix an error for `Rails/UniqueValidationWithoutIndex`when a table has no column definition. ([@koic][])

## 2.5.0 (2020-03-24)

Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/rails/schema_loader/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def with_column?(name:)

def build_columns(node)
each_content(node).map do |child|
next unless child.send_type?
next unless child&.send_type?
next if child.method?(:index)

Column.new(child)
Expand All @@ -69,7 +69,7 @@ def build_columns(node)

def build_indices(node)
each_content(node).map do |child|
next unless child.send_type?
next unless child&.send_type?
next unless child.method?(:index)

Index.new(child)
Expand All @@ -79,7 +79,7 @@ def build_indices(node)
def each_content(node)
return enum_for(__method__, node) unless block_given?

case node.body.type
case node.body&.type
when :begin
node.body.children.each do |child|
yield(child)
Expand Down
17 changes: 17 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 @@ -303,6 +303,23 @@ class Article
end
end

context 'when a table has no column definition' do
let(:schema) { <<~RUBY }
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
create_table "users", force: :cascade do |t|
end
end
RUBY

it 'ignores it' do
expect_no_offenses(<<~RUBY)
class User
validates :account, uniqueness: true
end
RUBY
end
end

context 'when the validation is for a relation with foreign_key: option' do
context 'without proper index' do
let(:schema) { <<~RUBY }
Expand Down