Class: RuboCop::Cop::Rails::TableNameAssignment

Inherits:
Base
  • Object
show all
Includes:
ActiveRecordHelper
Defined in:
lib/rubocop/cop/rails/table_name_assignment.rb

Overview

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: 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: 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: api.rubyonrails.org/classes/ActiveRecord/Inheritance.html

Examples:

# bad
self.table_name = 'some_table_name'
self.table_name = :some_other_name

Constant Summary collapse

MSG =
'Do not use `self.table_name =`.'

Constants included from ActiveRecordHelper

ActiveRecordHelper::WHERE_METHODS

Instance Method Summary collapse

Methods included from ActiveRecordHelper

#external_dependency_checksum, #foreign_key_of, #in_where?, #inherit_active_record_base?, #polymorphic?, #resolve_relation_into_column, #schema, #table_name

Instance Method Details

#on_class(class_node) ⇒ Object



36
37
38
39
40
# File 'lib/rubocop/cop/rails/table_name_assignment.rb', line 36

def on_class(class_node)
  return if base_class?(class_node)

  find_set_table_name(class_node).each { |node| add_offense(node) }
end