Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vfonic opened this issue May 30, 2022 · 1 comment · Fixed by #708
Closed

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

vfonic opened this issue May 30, 2022 · 1 comment · Fixed by #708

Comments

@vfonic
Copy link
Contributor

vfonic commented May 30, 2022

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!

@koic koic transferred this issue from rubocop/rubocop May 30, 2022
nobuyo added a commit to nobuyo/rubocop-rails that referenced this issue May 31, 2022
nobuyo added a commit to nobuyo/rubocop-rails that referenced this issue May 31, 2022
…hen a variable is passed to has_many or has_one with double splat
@koic koic closed this as completed in #708 Jun 1, 2022
koic added a commit that referenced this issue Jun 1, 2022
…endent

[Fix #707] Fix an error when a variable is passed to has_many or has_one with double splat
@vfonic
Copy link
Contributor Author

vfonic commented Jun 1, 2022

Thank you, @nobuyo and @koic! 🙏 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant