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 Sep 29, 2023
1 parent 8aa79ac commit de37074
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/models/public_body/calculated_home_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,22 @@ def calculated_home_page

private

# Guess home page from the request email, or use explicit override, or nil
# if not known.
# Ensure known home page has a full URL or guess 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

# Ensure the home page has the HTTP protocol at the start of the URL
def ensure_home_page_protocol
return unless home_page.present?
home_page[URI.regexp(%w(http https))] ? home_page : "https://#{home_page}"
end

# Guess the home page from the request address email domain.
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 de37074

Please sign in to comment.