Skip to content

Commit

Permalink
[Fix rubocop#214] Fix an error for Rails/UniqueValidationWithoutIndex
Browse files Browse the repository at this point in the history
Fixes rubocop#214.

This PR fixes an error for `Rails/UniqueValidationWithoutIndex`
when a table has no column definition.
  • Loading branch information
koic committed Mar 27, 2020
1 parent 789a8f2 commit fdeeba5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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

0 comments on commit fdeeba5

Please sign in to comment.