Skip to content

Commit

Permalink
Refactor PublicBody::CalculatedHomePage#calculated_home_page
Browse files Browse the repository at this point in the history
Extract each piece of logic to own method to improve clarity.
  • Loading branch information
garethrees committed Jul 24, 2023
1 parent 1ed39c6 commit 1e1243c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/models/public_body/calculated_home_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ def calculated_home_page
# Guess home page from the request email, or use explicit override, or nil
# if not known.
def calculated_home_page!
if home_page && !home_page.empty?
home_page[URI.regexp(%w(http https))] ? home_page : "https://#{home_page}"
elsif request_email_domain
return if excluded_calculated_home_page_domain?(request_email_domain)
"https://www.#{request_email_domain}"
end
ensure_home_page_protocol || guess_home_page
end

def ensure_home_page_protocol
return unless home_page.present?
home_page[URI.regexp(%w(http https))] ? home_page : "https://#{home_page}"
end

def guess_home_page
return unless request_email_domain
return if excluded_calculated_home_page_domain?(request_email_domain)
"https://www.#{request_email_domain}"
end

def excluded_calculated_home_page_domain?(domain)
Expand Down

0 comments on commit 1e1243c

Please sign in to comment.