From 81c3ad5ae6127922c75b5fe6dd70d2b0c0e3e9cb Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Wed, 2 Feb 2011 10:15:05 -0300 Subject: [PATCH] Check if environments table exists before try to load Environment.default_hostname --- config/initializers/exception_notification.rb | 2 +- lib/noosfero.rb | 4 ++++ test/unit/noosfero_test.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/config/initializers/exception_notification.rb b/config/initializers/exception_notification.rb index 7ba2fab..dc404d0 100644 --- a/config/initializers/exception_notification.rb +++ b/config/initializers/exception_notification.rb @@ -1,6 +1,6 @@ unless NOOSFERO_CONF['exception_recipients'].blank? require 'exception_notification.rb' - ExceptionNotifier.sender_address = "noreply@#{Environment.default.default_hostname}" + ExceptionNotifier.sender_address = "noreply@#{Noosfero.default_hostname}" ExceptionNotifier.email_prefix = "[Noosfero ERROR] " ExceptionNotifier.exception_recipients = NOOSFERO_CONF['exception_recipients'] ActionController::Base.send :include, ExceptionNotifiable diff --git a/lib/noosfero.rb b/lib/noosfero.rb index 20e85fc..0bc1ec2 100644 --- a/lib/noosfero.rb +++ b/lib/noosfero.rb @@ -39,6 +39,10 @@ module Noosfero '[a-z0-9][a-z0-9~.]*([_-][a-z0-9~.]+)*' end + def self.default_hostname + Environment.table_exists? && Environment.default ? Environment.default.default_hostname : 'localhost' + end + private def self.controllers_in_directory(dir) diff --git a/test/unit/noosfero_test.rb b/test/unit/noosfero_test.rb index 9a01db7..5504afe 100644 --- a/test/unit/noosfero_test.rb +++ b/test/unit/noosfero_test.rb @@ -69,4 +69,19 @@ class NoosferoTest < Test::Unit::TestCase end end + should "use default hostname of default environment as hostname of Noosfero instance" do + Environment.default.domains << Domain.new(:name => 'thisisdefaulthostname.com', :is_default => true) + assert_equal 'thisisdefaulthostname.com', Noosfero.default_hostname + end + + should "use 'localhost' as default hostname of Noosfero instance when has no environments in database" do + Environment.stubs(:default).returns(nil) + assert_equal 'localhost', Noosfero.default_hostname + end + + should "use 'localhost' as default hostname of Noosfero instance when environments table doesn't exists" do + Environment.stubs(:table_exists?).returns(false) + assert_equal 'localhost', Noosfero.default_hostname + end + end -- libgit2 0.21.2