Sha256: 4ca8973333a0762f7ba348dacb45d972827d613705f07ed5c997fc5808b2fded

Contents?: true

Size: 1.04 KB

Versions: 12

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks if the value of the option `class_name`, in
      # the definition of a reflection is a string.
      #
      # @example
      #   # bad
      #   has_many :accounts, class_name: Account
      #   has_many :accounts, class_name: Account.name
      #
      #   # good
      #   has_many :accounts, class_name: 'Account'
      class ReflectionClassName < Cop
        MSG = 'Use a string value for `class_name`.'

        def_node_matcher :association_with_reflection, <<-PATTERN
          (send nil? {:has_many :has_one :belongs_to} _
            (hash <$#reflection_class_name ...>)
          )
        PATTERN

        def_node_matcher :reflection_class_name, <<-PATTERN
          (pair (sym :class_name) [!dstr !str !sym])
        PATTERN

        def on_send(node)
          association_with_reflection(node) do |reflection_class_name|
            add_offense(node, location: reflection_class_name.loc.expression)
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
rubocop-rails-2.4.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.3.2 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.3.1 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.3.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.2.1 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.2.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.1.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.0.1 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-0.71.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-rails-2.0.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-0.70.0 lib/rubocop/cop/rails/reflection_class_name.rb
rubocop-0.69.0 lib/rubocop/cop/rails/reflection_class_name.rb