Skip to content

Change format of Rails/FindByOrAssignmentMemoization auto correct #1513

@schinery

Description

@schinery

Is your feature request related to a problem? Please describe.

The unsafe auto correct for Rails/FindByOrAssignmentMemoization added too many lines to a couple of admittedly bloated classes.

Describe the solution you'd like

When running the new Rails/FindByOrAssignmentMemoization cop with the -A flag it will do the following:

Changes:

def product
  @product ||= Product.find_by(code: params[:code])
end

To:

def product
  if instance_variable_defined?(:@product)
    @product
  else
    @product = Product.find_by(code: params[:code])
  end
end

To save a few lines, I was wondering if the format could be changed to?

def product
  return @product if instance_variable_defined?(:@product)

  @product = Product.find_by(code: params[:code])
end

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