Commit e186c5d35a04651e046cc94944e82e8529509805

Authored by AntonioTerceiro
1 parent 45660754

ActionItem78: generating correct URL's



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@645 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/change_password.rb
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 class ChangePassword < Task 3 class ChangePassword < Task
4 4
  5 + include Noosfero::URL
  6 +
5 serialize :data, Hash 7 serialize :data, Hash
6 def data 8 def data
7 self[:data] ||= {} 9 self[:data] ||= {}
@@ -70,6 +72,7 @@ class ChangePassword &lt; Task @@ -70,6 +72,7 @@ class ChangePassword &lt; Task
70 def create_message 72 def create_message
71 hostname = self.requestor.environment.default_hostname 73 hostname = self.requestor.environment.default_hostname
72 code = self.code 74 code = self.code
  75 + port = self.port
73 76
74 lambda do 77 lambda do
75 _("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) 78 _("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)
lib/noosfero/url.rb
@@ -6,6 +6,8 @@ require &#39;noosfero&#39; @@ -6,6 +6,8 @@ require &#39;noosfero&#39;
6 # TODO: document the use of config/web.yml in a INSTALL document 6 # TODO: document the use of config/web.yml in a INSTALL document
7 module Noosfero::URL 7 module Noosfero::URL
8 8
  9 + include ActionController::UrlWriter
  10 +
9 class << self 11 class << self
10 12
11 def config 13 def config
@@ -14,15 +16,25 @@ module Noosfero::URL @@ -14,15 +16,25 @@ module Noosfero::URL
14 if File.exists?(config_file) 16 if File.exists?(config_file)
15 @config = YAML::load_file(config_file) 17 @config = YAML::load_file(config_file)
16 else 18 else
17 - @config = {  
18 - 'path' => '',  
19 - 'port' => 3000  
20 - } 19 + if ENV['RAILS_ENV'] == 'production'
  20 + @config = {
  21 + }
  22 + else
  23 + @config = {
  24 + 'path' => '',
  25 + 'port' => 3000
  26 + }
  27 + end
21 end 28 end
22 end 29 end
23 30
24 @config 31 @config
25 end 32 end
  33 +
  34 + def included(other_module)
  35 + other_module.send(:include, ActionController::UrlWriter)
  36 + end
  37 +
26 end 38 end
27 39
28 def port 40 def port
@@ -33,4 +45,18 @@ module Noosfero::URL @@ -33,4 +45,18 @@ module Noosfero::URL
33 Noosfero::URL.config['path'] 45 Noosfero::URL.config['path']
34 end 46 end
35 47
  48 + def url_for(options)
  49 + local_options = {}
  50 + local_options[:port] = self.port unless self.port.nil?
  51 +
  52 + url = super(local_options.merge(options))
  53 +
  54 + if self.path.blank?
  55 + url
  56 + else
  57 + url.sub(/(http:\/\/[^\/]+(:\d+)?)\//, "\\1#{self.path}/")
  58 + end
  59 + end
  60 +
  61 +
36 end 62 end
test/unit/noosfero_url_test.rb
@@ -34,4 +34,16 @@ class NoosferoURLTest &lt; Test::Unit::TestCase @@ -34,4 +34,16 @@ class NoosferoURLTest &lt; Test::Unit::TestCase
34 assert_equal 9999, self.port 34 assert_equal 9999, self.port
35 end 35 end
36 36
  37 + should 'add path when needed' do
  38 + self.stubs(:path).returns('/somepath')
  39 + self.stubs(:port).returns(nil)
  40 + assert_equal('http://example.com/somepath/', url_for(:host => 'example.com', :controller => 'home'))
  41 + end
  42 +
  43 + should 'not add path when it is not needed' do
  44 + self.stubs(:path).returns(nil)
  45 + self.stubs(:port).returns(nil)
  46 + assert_equal('http://example.com/', url_for(:host => 'example.com', :controller => 'home'))
  47 + end
  48 +
37 end 49 end