Skip to content

Instantly share code, notes, and snippets.

@emachnic
Created February 1, 2011 16:52
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 emachnic/806130 to your computer and use it in GitHub Desktop.
Save emachnic/806130 to your computer and use it in GitHub Desktop.
Refactoring 'if' statements in RubyFreelancers
/ Before refactoring
#freelancer_nav
- if freelancer_signed_in?
= link_to "Dashboard", '/freelancers/dashboard'
- if current_freelancer.profile.nil?
= link_to "New Profile", new_profile_path
- else
= link_to "My Profile", current_freelancer.profile
= link_to "Sign out", destroy_freelancer_session_path
- elsif admin_signed_in?
= link_to "Dashboard", admin_root_path
= link_to "Sign out", destroy_admin_session_path
- elsif employer_signed_in?
- if current_employer.profile.nil?
= link_to "New Profile", new_profile_path
- else
= link_to "Admin Login", new_admin_session_path
= link_to "Freelancer Login", new_freelancer_session_path
= link_to "be a freelancer", new_freelancer_registration_path
= link_to "be an employer", new_employer_registration_path
/ After refactoring
#freelancer_nav
- if current_user
= render :partial => "#{current_user_model}/nav"
- else
= link_to "Admin Login", new_admin_session_path
= link_to "Freelancer Login", new_freelancer_session_path
= link_to "be a freelancer", new_freelancer_registration_path
= link_to "be an employer", new_employer_registration_path
module ApplicationHelper
def current_user
if current_freelancer
return current_freelancer
elsif current_employer
return current_employer
elsif current_admin
return current_admin
else
return nil
end
end
def current_user_model
current_user_class = current_user.class.to_s
return current_user_class.downcase.pluralize
end
end
/ Admins nav partial
= link_to "Dashboard", admin_root_path
= link_to "Sign out", destroy_admin_session_path
/ Freelancers nav partial
= link_to "Dashboard", freelancer_root_path
- if current_freelancer.profile.nil?
= link_to "New Profile", new_profile_path
- else
= link_to "My Profile", current_freelancer.profile
= link_to "Sign out", destroy_freelancer_session_path
/ Employers nav partial
= link_to "Dashboard", employer_root_path
- if current_employer.profile.nil?
= link_to "New Profile", new_profile_path
- else
= link_to "My Profile", current_employer.profile
= link_to "Sign out", destroy_employer_session_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment