Commit 3d9b60463d251625908be57d129a55774653d426

Authored by AntonioTerceiro
1 parent 284128b1

ActionItem519: adding Environment#top_url

while I'm there, extracting the port detection for development mode into
Noosfero module

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2322 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/environment.rb
@@ -222,6 +222,14 @@ class Environment < ActiveRecord::Base @@ -222,6 +222,14 @@ class Environment < ActiveRecord::Base
222 end 222 end
223 end 223 end
224 224
  225 + def top_url
  226 + result = "http://#{default_hostname}"
  227 + if Noosfero.url_options.has_key?(:port)
  228 + result << ':' << Noosfero.url_options[:port].to_s
  229 + end
  230 + result
  231 + end
  232 +
225 def to_s 233 def to_s
226 self.name || '?' 234 self.name || '?'
227 end 235 end
app/models/profile.rb
@@ -257,12 +257,7 @@ class Profile &lt; ActiveRecord::Base @@ -257,12 +257,7 @@ class Profile &lt; ActiveRecord::Base
257 if self.domains.empty? 257 if self.domains.empty?
258 generate_url(:controller => 'content_viewer', :action => 'view_page', :page => []) 258 generate_url(:controller => 'content_viewer', :action => 'view_page', :page => [])
259 else 259 else
260 - options = { :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []}  
261 - # help developers by generating a suitable URL for development environment  
262 - if (ENV['RAILS_ENV'] == 'development')  
263 - options.merge!(development_url_options)  
264 - end  
265 - options 260 + Noosfero.url_options.merge({ :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []})
266 end 261 end
267 end 262 end
268 263
@@ -279,26 +274,7 @@ class Profile &lt; ActiveRecord::Base @@ -279,26 +274,7 @@ class Profile &lt; ActiveRecord::Base
279 end 274 end
280 275
281 def url_options 276 def url_options
282 - options = { :host => self.environment.default_hostname, :profile => self.identifier}  
283 -  
284 - # help developers by generating a suitable URL for development environment  
285 - if (ENV['RAILS_ENV'] == 'development')  
286 - options.merge!(development_url_options)  
287 - end  
288 -  
289 - options  
290 - end  
291 -  
292 - # FIXME couldn't think of a way to test this.  
293 - #  
294 - # Works (tested by hand) on Rails 2.0.2, with mongrel. Should work with  
295 - # webrick too.  
296 - def development_url_options # :nodoc:  
297 - if Object.const_defined?('OPTIONS')  
298 - { :port => OPTIONS[:port ]}  
299 - else  
300 - {}  
301 - end 277 + Noosfero.url_options.merge({ :host => self.environment.default_hostname, :profile => self.identifier})
302 end 278 end
303 279
304 # FIXME this can be SLOW 280 # FIXME this can be SLOW
lib/noosfero.rb
@@ -40,6 +40,27 @@ module Noosfero @@ -40,6 +40,27 @@ module Noosfero
40 @terminology = term 40 @terminology = term
41 end 41 end
42 42
  43 + def self.url_options
  44 + if ENV['RAILS_ENV'] == 'development'
  45 + development_url_options
  46 + else
  47 + {}
  48 + end
  49 + end
  50 +
  51 + # FIXME couldn't think of a way to test this.
  52 + #
  53 + # Works (tested by hand) on Rails 2.0.2, with mongrel. Should work with
  54 + # webrick too.
  55 + def self.development_url_options
  56 + if Object.const_defined?('OPTIONS')
  57 + { :port => OPTIONS[:port ]}
  58 + else
  59 + {}
  60 + end
  61 + end
  62 +
  63 +
43 end 64 end
44 65
45 require 'noosfero/constants' 66 require 'noosfero/constants'
test/unit/environment_test.rb
@@ -156,6 +156,21 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -156,6 +156,21 @@ class EnvironmentTest &lt; Test::Unit::TestCase
156 assert_equal 'localhost', env.default_hostname 156 assert_equal 'localhost', env.default_hostname
157 end 157 end
158 158
  159 + should 'provide default top URL' do
  160 + env = Environment.new
  161 + env.expects(:default_hostname).returns('www.lalala.net')
  162 + assert_equal 'http://www.lalala.net', env.top_url
  163 + end
  164 +
  165 + should 'include port in default top URL for development environment' do
  166 + env = Environment.new
  167 + env.expects(:default_hostname).returns('www.lalala.net')
  168 +
  169 + Noosfero.expects(:url_options).returns({ :port => 9999 }).at_least_once
  170 +
  171 + assert_equal 'http://www.lalala.net:9999', env.top_url
  172 + end
  173 +
159 should 'provide an approval_method setting' do 174 should 'provide an approval_method setting' do
160 env = Environment.new 175 env = Environment.new
161 176
test/unit/noosfero_test.rb
@@ -40,4 +40,10 @@ class NoosferoTest &lt; Test::Unit::TestCase @@ -40,4 +40,10 @@ class NoosferoTest &lt; Test::Unit::TestCase
40 assert_equal 'lalalalala', Noosfero.term('lalalalala') 40 assert_equal 'lalalalala', Noosfero.term('lalalalala')
41 end 41 end
42 42
  43 + should 'provide url options to identify development environment' do
  44 + ENV.expects(:[]).with('RAILS_ENV').returns('development')
  45 + Noosfero.expects(:development_url_options).returns({ :port => 9999 })
  46 + assert_equal({:port => 9999}, Noosfero.url_options)
  47 + end
  48 +
43 end 49 end
test/unit/profile_test.rb
@@ -281,16 +281,17 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -281,16 +281,17 @@ class ProfileTest &lt; Test::Unit::TestCase
281 should 'help developers by adding a suitable port to url options' do 281 should 'help developers by adding a suitable port to url options' do
282 profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) 282 profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id)
283 283
284 - ENV.expects(:[]).with('RAILS_ENV').returns('development')  
285 - profile.expects(:development_url_options).returns({ :port => 9999 }) 284 + Noosfero.expects(:url_options).returns({ :port => 9999 })
  285 +
286 ok('Profile#url_options must include port option when running in development mode') { profile.url_options[:port] == 9999 } 286 ok('Profile#url_options must include port option when running in development mode') { profile.url_options[:port] == 9999 }
287 end 287 end
288 288
289 should 'help developers by adding a suitable port to url options for own domain urls' do 289 should 'help developers by adding a suitable port to url options for own domain urls' do
290 profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) 290 profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id)
291 profile.domains << Domain.new(:name => 'micojones.net') 291 profile.domains << Domain.new(:name => 'micojones.net')
292 - ENV.expects(:[]).with('RAILS_ENV').returns('development')  
293 - profile.expects(:development_url_options).returns({ :port => 9999 }) 292 +
  293 + Noosfero.expects(:url_options).returns({ :port => 9999 })
  294 +
294 ok('Profile#url must include port options when running in developers mode') { profile.url[:port] == 9999 } 295 ok('Profile#url must include port options when running in developers mode') { profile.url[:port] == 9999 }
295 end 296 end
296 297