diff --git a/app/models/change_password.rb b/app/models/change_password.rb index 6b4eabd..7de3275 100644 --- a/app/models/change_password.rb +++ b/app/models/change_password.rb @@ -2,6 +2,8 @@ class ChangePassword < Task + include Noosfero::URL + serialize :data, Hash def data self[:data] ||= {} @@ -70,6 +72,7 @@ class ChangePassword < Task def create_message hostname = self.requestor.environment.default_hostname code = self.code + port = self.port lambda do _("In order to change your password, please visit the following address:\n\n%s") % url_for(:host => hostname, :controller => 'account', :action => 'new_password', :code => code) diff --git a/lib/noosfero/url.rb b/lib/noosfero/url.rb index d366812..2c33227 100644 --- a/lib/noosfero/url.rb +++ b/lib/noosfero/url.rb @@ -6,6 +6,8 @@ require 'noosfero' # TODO: document the use of config/web.yml in a INSTALL document module Noosfero::URL + include ActionController::UrlWriter + class << self def config @@ -14,15 +16,25 @@ module Noosfero::URL if File.exists?(config_file) @config = YAML::load_file(config_file) else - @config = { - 'path' => '', - 'port' => 3000 - } + if ENV['RAILS_ENV'] == 'production' + @config = { + } + else + @config = { + 'path' => '', + 'port' => 3000 + } + end end end @config end + + def included(other_module) + other_module.send(:include, ActionController::UrlWriter) + end + end def port @@ -33,4 +45,18 @@ module Noosfero::URL Noosfero::URL.config['path'] end + def url_for(options) + local_options = {} + local_options[:port] = self.port unless self.port.nil? + + url = super(local_options.merge(options)) + + if self.path.blank? + url + else + url.sub(/(http:\/\/[^\/]+(:\d+)?)\//, "\\1#{self.path}/") + end + end + + end diff --git a/test/unit/noosfero_url_test.rb b/test/unit/noosfero_url_test.rb index 044a1aa..2d208fd 100644 --- a/test/unit/noosfero_url_test.rb +++ b/test/unit/noosfero_url_test.rb @@ -34,4 +34,16 @@ class NoosferoURLTest < Test::Unit::TestCase assert_equal 9999, self.port end + should 'add path when needed' do + self.stubs(:path).returns('/somepath') + self.stubs(:port).returns(nil) + assert_equal('http://example.com/somepath/', url_for(:host => 'example.com', :controller => 'home')) + end + + should 'not add path when it is not needed' do + self.stubs(:path).returns(nil) + self.stubs(:port).returns(nil) + assert_equal('http://example.com/', url_for(:host => 'example.com', :controller => 'home')) + end + end -- libgit2 0.21.2