简体   繁体   中英

Ruby/Rails expansion of the if block from one line - rubocop

I have to modify my if block from one line.

def filename
 "#{model.external_id}-inquiry_process_summary_#{timestamp}.pdf" if original_filename
end

to something like this:

def filename
  if original_filename
    result = model.inquiry_field_responses.joins(:inquiry_field).where(inquiry_fields: { name: 'company_name' })
    "#{result.first&.value}-inquiry_process_summary_#{timestamp}.pdf"
  end
end

This is simple but my rubocop's screaming Use a guard clause instead of wrapping the code inside a conditional expression . If I add Success before last end few of my specs fail with error

expected the response to have status code (422) but it was:ok (200)

Without Success everything pass as it should.

AFAIK, Rubocop is complaining about not returning early. This should solve the Rubocop problem:

def filename
  return unless original_filename

  result = model.inquiry_field_responses.joins(:inquiry_field).where(inquiry_fields: { name: 'company_name' })
  "#{result.first&.value}-inquiry_process_summary_#{timestamp}.pdf"
end

Read more about the guard clause .

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