From 1e1243cea7018022eb4f303b8f982764b4331fbe Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 24 Jul 2023 12:54:17 +0100 Subject: [PATCH] Refactor PublicBody::CalculatedHomePage#calculated_home_page Extract each piece of logic to own method to improve clarity. --- app/models/public_body/calculated_home_page.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models/public_body/calculated_home_page.rb b/app/models/public_body/calculated_home_page.rb index 9e41cab9208..ae1b13a2c14 100644 --- a/app/models/public_body/calculated_home_page.rb +++ b/app/models/public_body/calculated_home_page.rb @@ -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)