1
\$\begingroup\$

I am parsing a file that exists in either app/components/ui or in app/components. If the one in the first dir (app/components/ui) doesn't exist, then fallback to the app/components directory. This is what I have currently.

  def front_matter
    return unless params[:filename].present?

    path = Rails.root.join('app', 'components', 'ui', params[:filename])
    path = Rails.root.join('app', 'components', params[:filename]) unless File.exist?(path)
    @front_matter ||= FrontMatterParser::Parser.parse_file(path)
  end

Is there a better way to write this?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

It seems OK to me. You might put the file logic into another method, perhaps, so that #front_matter is only doing one thing?

def front_matter_path
  path = Rails.root.join('app', 'components', 'ui', params[:filename])
  return path if File.exist?(path)
  Rails.root.join('app', 'components', params[:filename])
end

def front_matter
  return unless params[:filename].present?
  @front_matter ||= FrontMatterParser::Parser.parse_file(front_matter_path)
end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.