Commit bc0f3eea306176ba0eef4ed7374ea4e0df3a0e57

Authored by MoisesMachado
1 parent 0946557c

ActionItem198: require the www. in front of the domain

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/branches/0.11.x@2485 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/environment.rb
... ... @@ -220,6 +220,19 @@ class Environment < ActiveRecord::Base
220 220 end
221 221 end
222 222  
  223 + # Whether this environment should force having 'www.' in its domain name or
  224 + # not. Defauls to false.
  225 + #
  226 + # See also #default_hostname
  227 + def force_www
  228 + settings[:force_www] || false
  229 + end
  230 +
  231 + # Sets the value of #force_www. <tt>value</tt> must be a boolean.
  232 + def force_www=(value)
  233 + settings[:force_www] = value
  234 + end
  235 +
223 236 # #################################################
224 237 # Validations
225 238 # #################################################
... ... @@ -248,11 +261,16 @@ class Environment &lt; ActiveRecord::Base
248 261 Category.top_level_for(self)
249 262 end
250 263  
  264 + # Returns the hostname of the first domain associated to this environment.
  265 + #
  266 + # If #force_www is true, adds 'www.' at the beginning of the hostname. If the
  267 + # environment has not associated domains, returns 'localhost'.
251 268 def default_hostname
252 269 if self.domains(true).empty?
253 270 'localhost'
254 271 else
255   - self.domains.find(:first, :order => 'id').name
  272 + domain = self.domains.find(:first, :order => 'id').name
  273 + force_www ? ('www.' + domain) : domain
256 274 end
257 275 end
258 276  
... ...
test/unit/environment_test.rb
... ... @@ -172,6 +172,12 @@ class EnvironmentTest &lt; Test::Unit::TestCase
172 172 assert_equal 'localhost', env.default_hostname
173 173 end
174 174  
  175 + should 'add www when told to force www' do
  176 + env = Environment.create!(:name => 'test environment', :force_www => true)
  177 + env.domains << Domain.create(:name => 'example.com')
  178 + assert_equal 'www.example.com', env.default_hostname
  179 + end
  180 +
175 181 should 'provide default top URL' do
176 182 env = Environment.new
177 183 env.expects(:default_hostname).returns('www.lalala.net')
... ...