diff --git a/app/models/environment.rb b/app/models/environment.rb index d3a13a6..65c31f7 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -220,6 +220,19 @@ class Environment < ActiveRecord::Base end end + # Whether this environment should force having 'www.' in its domain name or + # not. Defauls to false. + # + # See also #default_hostname + def force_www + settings[:force_www] || false + end + + # Sets the value of #force_www. value must be a boolean. + def force_www=(value) + settings[:force_www] = value + end + # ################################################# # Validations # ################################################# @@ -248,11 +261,16 @@ class Environment < ActiveRecord::Base Category.top_level_for(self) end + # Returns the hostname of the first domain associated to this environment. + # + # If #force_www is true, adds 'www.' at the beginning of the hostname. If the + # environment has not associated domains, returns 'localhost'. def default_hostname if self.domains(true).empty? 'localhost' else - self.domains.find(:first, :order => 'id').name + domain = self.domains.find(:first, :order => 'id').name + force_www ? ('www.' + domain) : domain end end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 8753a74..8e0cb88 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -172,6 +172,12 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal 'localhost', env.default_hostname end + should 'add www when told to force www' do + env = Environment.create!(:name => 'test environment', :force_www => true) + env.domains << Domain.create(:name => 'example.com') + assert_equal 'www.example.com', env.default_hostname + end + should 'provide default top URL' do env = Environment.new env.expects(:default_hostname).returns('www.lalala.net') -- libgit2 0.21.2