Skip to content

Instantly share code, notes, and snippets.

@pete-a
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-a/985048d97a36718efd9e to your computer and use it in GitHub Desktop.
Save pete-a/985048d97a36718efd9e to your computer and use it in GitHub Desktop.
Nested if refactor
# https://github.com/mogeneration/adomatic/blob/ruth/feature/duplicate_job/app/controllers/jobs_controller.rb#L32
if check_required_fields @job
if @job.save
@job.send_status_email
if current_user.is_admin?
redirect_to admin_organisation_job_queue_job_url(@job.job_queue.organisation, @job.job_queue, @job), notice: 'Job was successfully created.'
else
redirect_to job_queue_job_url(@job.job_queue, @job), notice: 'Job was successfully created.'
end
else
render action: 'duplicate_job'
end
else
render action: 'duplicate_job'
end
if check_required_fields(@job) && @job.save
@job.send_status_email
if current_user.is_admin?
redirect_to admin_organisation_job_queue_job_url(@job.job_queue.organisation, @job.job_queue, @job), notice: 'Job was successfully created.'
else
redirect_to job_queue_job_url(@job.job_queue, @job), notice: 'Job was successfully created.'
end
else
render action: 'duplicate_job'
end
if check_required_fields(@job) && @job.save
@job.send_status_email
redirect_to_job(@job)
else
render action: 'duplicate_job'
end
#....
private
def redirect_to_job(job)
if current_user.is_admin?
redirect_to admin_organisation_job_queue_job_url(job.job_queue.organisation, job.job_queue, @job), notice: 'Job was successfully created.'
else
redirect_to job_queue_job_url(job.job_queue, job), notice: 'Job was successfully created.'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment