Last active
May 4, 2021 18:01
-
-
Save AhmedNadar/a58f582d70f1e7b9c45ff84968df5e73 to your computer and use it in GitHub Desktop.
Refactor nested IF statements in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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