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

Add new Rails/DuplicateIgnoredColumns cop #761

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#761](https://github.com/rubocop/rubocop-rails/pull/761): Add new `Rails/AvoidDuplicateIgnoredColumns` cop. ([@sikachu][])
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ Rails/AttributeDefaultBlockValue:
Include:
- 'app/models/**/*'

Rails/AvoidDuplicateIgnoredColumns:
Description: 'Checks for duplicate calls to `ActiveRecord::Base.ignore_columns=` in a single file.'
Enabled: 'pending'
VersionAdded: '<<next>>'

Rails/BelongsTo:
Description: >-
Use `optional: true` instead of `required: false` for
Expand Down
34 changes: 34 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,40 @@ end
| Array
|===

== Rails/AvoidDuplicateIgnoredColumns

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| -
| -
|===

Checks for duplicate calls to ActiveRecord::Base.ignore_columns= as the
subsequent calls will override the first call.

=== Examples

[source,ruby]
----
# bad
class User < ApplicationRecord
self.ignored_columns = %w(profile)

# ...

self.ignored_columns = %w(birthday)
end

# good
class User < ApplicationRecord
self.ignored_columns = %w(profile birthday)
end
----

== Rails/BelongsTo

|===
Expand Down
81 changes: 81 additions & 0 deletions lib/rubocop/cop/rails/avoid_duplicate_ignored_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# Checks for duplicate calls to ActiveRecord::Base.ignore_columns= as the
sikachu marked this conversation as resolved.
Show resolved Hide resolved
# subsequent calls will override the first call.
#
# @example
# # bad
# class User < ApplicationRecord
# self.ignored_columns = %w(profile)
# self.ignored_columns = %w(birthday)
# end
#
# # good
# class User < ApplicationRecord
# self.ignored_columns = %w(profile birthday)
# end
#
class AvoidDuplicateIgnoredColumns < Base
sikachu marked this conversation as resolved.
Show resolved Hide resolved
MESSAGE = 'self.ignored_columns= has already been called on line %<other_line_number>s.'
sikachu marked this conversation as resolved.
Show resolved Hide resolved
sikachu marked this conversation as resolved.
Show resolved Hide resolved
sikachu marked this conversation as resolved.
Show resolved Hide resolved
ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveRecord::Base].freeze
sikachu marked this conversation as resolved.
Show resolved Hide resolved

def initialize(config = nil, options = nil)
super

@found_nodes = {}
@reported_lines = []
end

def on_send(current_node)
sikachu marked this conversation as resolved.
Show resolved Hide resolved
return unless current_node.method?(:ignored_columns=)
sikachu marked this conversation as resolved.
Show resolved Hide resolved
return unless current_node.self_receiver?

parent_class_name = find_parent_class_name(current_node)
return unless active_model?(parent_class_name)
sikachu marked this conversation as resolved.
Show resolved Hide resolved

@found_nodes[current_node.first_line] = current_node
return if @found_nodes.size == 1

add_offenses
end

private

def active_model?(parent_class_name)
ACTIVE_RECORD_CLASSES.include?(parent_class_name)
end

def find_parent_class_name(node)
return nil unless node

if node.class_type?
parent_class_name = node.node_parts[1]

return nil if parent_class_name.nil?

return parent_class_name.source
end

find_parent_class_name(node.parent)
end

sikachu marked this conversation as resolved.
Show resolved Hide resolved
def add_offenses
# Remove the earliest found node as we don't want to report offense on it.
first_occurance_line_number = @found_nodes.keys.min
sikachu marked this conversation as resolved.
Show resolved Hide resolved
@found_nodes.delete(first_occurance_line_number)

@found_nodes.each do |line_number, node|
next if @reported_lines.include?(line_number)

add_offense(node, message: format(MESSAGE, other_line_number: first_occurance_line_number))

@reported_lines << line_number
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require_relative 'rails/arel_star'
require_relative 'rails/assert_not'
require_relative 'rails/attribute_default_block_value'
require_relative 'rails/avoid_duplicate_ignored_columns'
require_relative 'rails/belongs_to'
require_relative 'rails/blank'
require_relative 'rails/bulk_change_table'
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/rails/avoid_duplicate_ignored_columns_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::AvoidDuplicateIgnoredColumns, :config do
context 'with no duplicate self.ignored_columns= call' do
sikachu marked this conversation as resolved.
Show resolved Hide resolved
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = %w(profile birthday)
end
RUBY
end
end

context 'with duplicate self.ignored_columns= call' do
sikachu marked this conversation as resolved.
Show resolved Hide resolved
it 'registers an offense' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = %w(profile)
self.ignored_columns = %w(birthday)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ self.ignored_columns= has already been called on line 2.
end
RUBY
end
end
end