Sha256: 42af0233bf5bfab2b406c7073535ee904e3be0ad4e845c3ce109fa584403be77

Contents?: true

Size: 1.49 KB

Versions: 47

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # 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

Version data entries

47 entries across 44 versions & 7 rubygems

Version Path
rubocop-rails-2.29.0 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/table_name_assignment.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/table_name_assignment.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/table_name_assignment.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/table_name_assignment.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/table_name_assignment.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/table_name_assignment.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/table_name_assignment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/table_name_assignment.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/table_name_assignment.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/table_name_assignment.rb