As I wrote this method I knew it could be written better, but it's an MVP. Now I am trying to learn what the experts feel the approach to be and how to solve the cops. Constant Learning!
Here is the controller method in question:
def show
if params[:id] && numeric?(params[:id])
@job = Job.find_by(id: params[:id])
if @job
if @job.start_at
if @job.end_at && @job.end_at >= Date.today
@company = Company.find_by_id(@job.company_id)
render 'jobs/show', layout: 'nested/job/show'
else
route_expired
end
else
route_not_paid_for
end
else
route_not_found_error
end
else
route_not_found_error
end
end
In turn these call a series of routes to direct the user where to go based on the type of error they obtained:
private
def route_not_found_error
flash[:error] = t('flash_messages.jobs.id.not_found')
redirect_to new_job_url
end
def route_expired
flash[:error] = t('flash_messages.jobs.id.expired')
redirect_to new_job_url
end
def route_not_paid_for
flash[:error] = t('flash_messages.jobs.id.not_paid_for')
redirect_to job_payment_url(@job)
end
To the experts, how would you approach this? I know this must be cleaner.