Sha256: 790dea2f188c9b23923b813c47cee91b2bfe93863e5c9cabba0c552e03b8472c

Contents?: true

Size: 1.12 KB

Versions: 13

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop prevents usage of `"*"` on an Arel::Table column reference.
      #
      # Using `arel_table["*"]` causes the outputted string to be a literal
      # quoted asterisk (e.g. <tt>`my_model`.`*`</tt>). This causes the
      # database to look for a column named <tt>`*`</tt> (or `"*"`) as opposed
      # to expanding the column list as one would likely expect.
      #
      # @example
      #   # bad
      #   MyTable.arel_table["*"]
      #
      #   # good
      #   MyTable.arel_table[Arel.star]
      #
      class ArelStar < Base
        extend AutoCorrector

        MSG = 'Use `Arel.star` instead of `"*"` for expanded column lists.'

        RESTRICT_ON_SEND = %i[[]].freeze

        def_node_matcher :star_bracket?, <<~PATTERN
          (send {const (send _ :arel_table)} :[] $(str "*"))
        PATTERN

        def on_send(node)
          return unless (star = star_bracket?(node))

          add_offense(star) do |corrector|
            corrector.replace(star.loc.expression, 'Arel.star')
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-rails-2.12.4 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/arel_star.rb
rubocop-rails-2.9.0 lib/rubocop/cop/rails/arel_star.rb