aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/public_body.rb16
-rw-r--r--spec/models/public_body_spec.rb34
2 files changed, 38 insertions, 12 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 961fa3cbb..a18af8c69 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -282,19 +282,11 @@ class PublicBody < ActiveRecord::Base
# Guess home page from the request email, or use explicit override, or nil
# if not known.
def calculated_home_page
- # manual override for ones we calculate wrongly
- if self.home_page != ''
- return self.home_page
+ if home_page && !home_page.empty?
+ home_page[URI::regexp(%w(http https))] ? home_page : "http://#{home_page}"
+ elsif request_email_domain
+ "http://www.#{request_email_domain}"
end
-
- # extract the domain name from the FOI request email
- url = self.request_email_domain
- if url.nil?
- return nil
- end
-
- # add standard URL prefix
- return "http://www." + url
end
# Are all requests to this body under the Environmental Information Regulations?
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index db0de78b2..e30916dff 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -401,3 +401,37 @@ describe PublicBody, " when loading CSV files" do
PublicBody.count.should == original_count
end
end
+
+describe PublicBody do
+ describe "calculated home page" do
+ it "should return the home page verbatim if it's present" do
+ public_body = PublicBody.new
+ public_body.home_page = "http://www.example.com"
+ public_body.calculated_home_page.should == "http://www.example.com"
+ end
+
+ it "should return the home page based on the request email domain if it has one" do
+ public_body = PublicBody.new
+ public_body.stub!(:request_email_domain).and_return "public-authority.com"
+ public_body.calculated_home_page.should == "http://www.public-authority.com"
+ end
+
+ it "should return nil if there's no home page and the email domain can't be worked out" do
+ public_body = PublicBody.new
+ public_body.stub!(:request_email_domain).and_return nil
+ public_body.calculated_home_page.should be_nil
+ end
+
+ it "should ensure home page URLs start with http://" do
+ public_body = PublicBody.new
+ public_body.home_page = "example.com"
+ public_body.calculated_home_page.should == "http://example.com"
+ end
+
+ it "should not add http when https is present" do
+ public_body = PublicBody.new
+ public_body.home_page = "https://example.com"
+ public_body.calculated_home_page.should == "https://example.com"
+ end
+ end
+end