diff --git a/changelog/fix_an_error_for_rails_unused_ignored_columns.md b/changelog/fix_an_error_for_rails_unused_ignored_columns.md new file mode 100644 index 0000000000..960b1880f4 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/rails/schema_loader/schema.rb b/lib/rubocop/rails/schema_loader/schema.rb index 8c9b62022f..e4d13f82b6 100644 --- a/lib/rubocop/rails/schema_loader/schema.rb +++ b/lib/rubocop/rails/schema_loader/schema.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/rails/unused_ignored_columns_spec.rb b/spec/rubocop/cop/rails/unused_ignored_columns_spec.rb index 463c5a4414..600a27138f 100644 --- a/spec/rubocop/cop/rails/unused_ignored_columns_spec.rb +++ b/spec/rubocop/cop/rails/unused_ignored_columns_spec.rb @@ -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