From 3d9b60463d251625908be57d129a55774653d426 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 25 Jul 2008 21:47:47 +0000 Subject: [PATCH] ActionItem519: adding Environment#top_url --- app/models/environment.rb | 8 ++++++++ app/models/profile.rb | 28 ++-------------------------- lib/noosfero.rb | 21 +++++++++++++++++++++ test/unit/environment_test.rb | 15 +++++++++++++++ test/unit/noosfero_test.rb | 6 ++++++ test/unit/profile_test.rb | 9 +++++---- 6 files changed, 57 insertions(+), 30 deletions(-) diff --git a/app/models/environment.rb b/app/models/environment.rb index eecefc6..858fde9 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -222,6 +222,14 @@ class Environment < ActiveRecord::Base end end + def top_url + result = "http://#{default_hostname}" + if Noosfero.url_options.has_key?(:port) + result << ':' << Noosfero.url_options[:port].to_s + end + result + end + def to_s self.name || '?' end diff --git a/app/models/profile.rb b/app/models/profile.rb index 2929385..6c16530 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -257,12 +257,7 @@ class Profile < ActiveRecord::Base if self.domains.empty? generate_url(:controller => 'content_viewer', :action => 'view_page', :page => []) else - options = { :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []} - # help developers by generating a suitable URL for development environment - if (ENV['RAILS_ENV'] == 'development') - options.merge!(development_url_options) - end - options + Noosfero.url_options.merge({ :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []}) end end @@ -279,26 +274,7 @@ class Profile < ActiveRecord::Base end def url_options - options = { :host => self.environment.default_hostname, :profile => self.identifier} - - # help developers by generating a suitable URL for development environment - if (ENV['RAILS_ENV'] == 'development') - options.merge!(development_url_options) - end - - options - end - - # FIXME couldn't think of a way to test this. - # - # Works (tested by hand) on Rails 2.0.2, with mongrel. Should work with - # webrick too. - def development_url_options # :nodoc: - if Object.const_defined?('OPTIONS') - { :port => OPTIONS[:port ]} - else - {} - end + Noosfero.url_options.merge({ :host => self.environment.default_hostname, :profile => self.identifier}) end # FIXME this can be SLOW diff --git a/lib/noosfero.rb b/lib/noosfero.rb index 6120def..5a25118 100644 --- a/lib/noosfero.rb +++ b/lib/noosfero.rb @@ -40,6 +40,27 @@ module Noosfero @terminology = term end + def self.url_options + if ENV['RAILS_ENV'] == 'development' + development_url_options + else + {} + end + end + + # FIXME couldn't think of a way to test this. + # + # Works (tested by hand) on Rails 2.0.2, with mongrel. Should work with + # webrick too. + def self.development_url_options + if Object.const_defined?('OPTIONS') + { :port => OPTIONS[:port ]} + else + {} + end + end + + end require 'noosfero/constants' diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 29a54eb..2800544 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -156,6 +156,21 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal 'localhost', env.default_hostname end + should 'provide default top URL' do + env = Environment.new + env.expects(:default_hostname).returns('www.lalala.net') + assert_equal 'http://www.lalala.net', env.top_url + end + + should 'include port in default top URL for development environment' do + env = Environment.new + env.expects(:default_hostname).returns('www.lalala.net') + + Noosfero.expects(:url_options).returns({ :port => 9999 }).at_least_once + + assert_equal 'http://www.lalala.net:9999', env.top_url + end + should 'provide an approval_method setting' do env = Environment.new diff --git a/test/unit/noosfero_test.rb b/test/unit/noosfero_test.rb index 9d1e478..62ca33e 100644 --- a/test/unit/noosfero_test.rb +++ b/test/unit/noosfero_test.rb @@ -40,4 +40,10 @@ class NoosferoTest < Test::Unit::TestCase assert_equal 'lalalalala', Noosfero.term('lalalalala') end + should 'provide url options to identify development environment' do + ENV.expects(:[]).with('RAILS_ENV').returns('development') + Noosfero.expects(:development_url_options).returns({ :port => 9999 }) + assert_equal({:port => 9999}, Noosfero.url_options) + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index e425a3d..fe1fecf 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -281,16 +281,17 @@ class ProfileTest < Test::Unit::TestCase should 'help developers by adding a suitable port to url options' do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - ENV.expects(:[]).with('RAILS_ENV').returns('development') - profile.expects(:development_url_options).returns({ :port => 9999 }) + Noosfero.expects(:url_options).returns({ :port => 9999 }) + ok('Profile#url_options must include port option when running in development mode') { profile.url_options[:port] == 9999 } end should 'help developers by adding a suitable port to url options for own domain urls' do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) profile.domains << Domain.new(:name => 'micojones.net') - ENV.expects(:[]).with('RAILS_ENV').returns('development') - profile.expects(:development_url_options).returns({ :port => 9999 }) + + Noosfero.expects(:url_options).returns({ :port => 9999 }) + ok('Profile#url must include port options when running in developers mode') { profile.url[:port] == 9999 } end -- libgit2 0.21.2