Skip to content

An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting #707

Closed
@vfonic

Description

@vfonic

Std output:

An error occurred while Rails/HasManyOrHasOneDependent cop was inspecting /Users/viktor/Developer/Ruby/CivicLift/app/models/concerns/has_many_records.rb:23:8.
undefined method `pairs' for s(:lvar, :opts):RuboCop::AST::Node

          options = options.first.children.first.pairs if options.first.kwsplat_type?
                                                ^^^^^^
/Users/viktor/.rvm/gems/ruby-3.1.1/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:119:in `valid_options?'
/Users/viktor/.rvm/gems/ruby-3.1.1/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb:80:in `on_send'

File that produces this error:

# frozen_string_literal: true

module HasManyRecords
  extend ActiveSupport::Concern

  class_methods do
    # creates has_many relationship for each of the record types
    # example:
    #   has_many_records -> { pending }, prefix: :pending, class_name: true
    # which generates:
    #   has_many :pending_events, -> { pending }, class_name: 'Event'
    def has_many_records(scope = nil, records_types: RecordsType::ALL_TYPES, prefix: nil, suffix: nil, **options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Naming/PredicateName
      opts = options.dup
      class_name = opts.delete(:class_name)
      source_type = opts.delete(:source_type)
      source = opts.delete(:source) if opts[:source] == true

      records_types.each_key do |records_type|
        record_model_name = records_type.classify
        opts[:class_name] = record_model_name if class_name
        opts[:source_type] = record_model_name if source_type
        opts[:source] = records_type if source == true
        has_many [prefix, records_type, suffix].compact.join('_').to_sym, scope, **opts
      end
    end
  end
end

RuboCop version

$ bundle exec rubocop -V
1.30.0 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 3.1.1 x86_64-darwin21)
  - rubocop-performance 1.14.0
  - rubocop-rails 2.14.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.11.1

Let me know if there's any other useful info I can provide. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions