-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #574 from MaximeDucheneS/add_table_name_cop
Add `TableName` cop
- Loading branch information
Showing
8 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#574](https://github.com/rubocop/rubocop-rails/pull/574): Add new `Rails/TableNameAssignment` cop. ([@MaximeDucheneS][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop enforces the absence of explicit table name assignment. | ||
# | ||
# `self.table_name=` should only be used for very good reasons, | ||
# such as not having control over the database, or working | ||
# on a legacy project. | ||
# | ||
# If you need to change how your model's name is translated to | ||
# a table name, you may want to look at Inflections: | ||
# https://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html | ||
# | ||
# If you wish to add a prefix in front of your model, or wish to change | ||
# the default prefix, `self.table_name_prefix` might better suit your needs: | ||
# https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-table_name_prefix-3D | ||
# | ||
# STI base classes named `Base` are ignored by this cop. | ||
# For more information: https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html | ||
# | ||
# @example | ||
# # bad | ||
# self.table_name = 'some_table_name' | ||
# self.table_name = :some_other_name | ||
class TableNameAssignment < Base | ||
include ActiveRecordHelper | ||
|
||
MSG = 'Do not use `self.table_name =`.' | ||
|
||
def_node_matcher :base_class?, <<~PATTERN | ||
(class (const ... :Base) ...) | ||
PATTERN | ||
|
||
def on_class(class_node) | ||
return if base_class?(class_node) | ||
|
||
find_set_table_name(class_node).each { |node| add_offense(node) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::TableNameAssignment, :config do | ||
context 'when table_name is defined' do | ||
context 'when string' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class AModule::SomeModel < ApplicationRecord | ||
self.table_name = 'some_other_table_name' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when string = foo' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class AModule::SomeModel < ApplicationRecord | ||
self.table_name = 'foo' | ||
^^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when symbol' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class AModule::SomeModel < ApplicationRecord | ||
self.table_name = :some_other_table_name | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when symbol = :foo' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
class AModule::SomeModel < ApplicationRecord | ||
self.table_name = :foo | ||
^^^^^^^^^^^^^^^^^^^^^^ Do not use `self.table_name =`. | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'when table_name is not defined' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class AModule::SomeModel < ApplicationRecord | ||
has_many :other_thing | ||
has_one :parent_thing | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
# Case for STI base classes | ||
context 'when class is named `Base`' do | ||
context 'when class is declared with module' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class A::B::Base < ApplicationRecord | ||
has_many :other_thing | ||
has_one :parent_thing | ||
self.table_name = 'special_table_name' | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when class is declared within module' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
module A | ||
module B | ||
class Base < ApplicationRecord | ||
has_many :other_thing | ||
has_one :parent_thing | ||
self.table_name = 'special_table_name' | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
end |