Skip to content

Commit

Permalink
Merge pull request #693 from koic/fix_an_error_for_rails_unused_ignor…
Browse files Browse the repository at this point in the history
…ed_columns

[Fix #692] Fix an error for `Rails/UnusedIgnoredColumns`
  • Loading branch information
koic committed Apr 22, 2022
2 parents 944394b + 526a433 commit 6382db9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_rails_unused_ignored_columns.md
@@ -0,0 +1 @@
* [#692](https://github.com/rubocop/rubocop-rails/issues/692): Fix an error for `Rails/UnusedIgnoredColumns` when using no tables db/schema.rb. ([@koic][])
3 changes: 3 additions & 0 deletions lib/rubocop/rails/schema_loader/schema.rb
Expand Up @@ -32,6 +32,8 @@ def build!(ast)
raise "Unexpected type: #{ast.type}" unless ast.block_type?

each_table(ast) do |table_def|
next unless table_def.method?(:create_table)

@tables << Table.new(table_def)
end

Expand All @@ -56,6 +58,7 @@ def each_table(ast)

def each_add_index(ast)
ast.body.children.each do |node|
next unless node.respond_to?(:send_type?)
next if !node&.send_type? || !node.method?(:add_index)

yield(node)
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/rails/unused_ignored_columns_spec.rb
Expand Up @@ -106,4 +106,25 @@ class User < ApplicationRecord
end
end
end

context 'with no tables db/schema.rb' do
include_context 'with SchemaLoader'

let(:schema) { <<~RUBY }
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
enable_extension 'plpgsql'
end
RUBY

context 'with an unused ignored column as a Symbol' do
# NOTE: For example, it is not possible to track externally managed databases.
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = [:real_name]
end
RUBY
end
end
end
end

0 comments on commit 6382db9

Please sign in to comment.