Skip to content

Instantly share code, notes, and snippets.

@AhmedNadar
Last active May 4, 2021 18:01
Show Gist options
  • Save AhmedNadar/a58f582d70f1e7b9c45ff84968df5e73 to your computer and use it in GitHub Desktop.
Save AhmedNadar/a58f582d70f1e7b9c45ff84968df5e73 to your computer and use it in GitHub Desktop.
Refactor nested IF statements in Ruby
# Before refactor
def set_company
if current_user
if current_user.company_id.present?
if current_user.companies.include?(current_user.company)
current_account = current_user.company
set_current_tenant(current_account)
else
set_current_tenant(nil)
end
else
set_current_tenant(nil)
end
else
set_current_tenant(nil)
end
end
# After refactor
def set_company
if current_user && current_user.company_id.present? && current_user.companies.include?(current_user.company)
current_account = current_user.company
set_current_tenant(current_account)
else
set_current_tenant(nil)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment