简体   繁体   中英

rubocop string interpolation and size condition

在此处输入图片说明

Before I except these two methods I wanted to see if anyone in the community had a better idea to structure these and make the cops pass. The first one with to_s seems a bit crazy too. I was thinking of refactoring the other method but that would be a single line or two.

Thoughts?

Code Examples One:

  def destroy(resource_name, id)
    delete "#{resource_name.to_s.pluralize}/#{id}"
  end

Code Examples Two:

  def all_products
    products_map = fetch(:products).map { |x| [x['id'], x] }.to_h
    variants = fetch :variants
    variants.group_by { |x| x['product']['resource']['id'] }.to_a.map do |product_id, product_variants|
      product.merge 'variants' => product_variants if product == products_map[product_id]
    end.compact
  end

For Code example One, maybe this can be used:

delete [resource_name.to_s.pluralize, id].join('/')  

For Code example Two, yes you definitely need to refactor it.
Maybe you need to create a separate method that does all the grouping and merging, etc. for the variants part.
I am not sure if this is a good practice, but you can create a private method for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM