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 2  
3 3 class ChangePassword < Task
4 4  
  5 + include Noosfero::URL
  6 +
5 7 serialize :data, Hash
6 8 def data
7 9 self[:data] ||= {}
... ... @@ -70,6 +72,7 @@ class ChangePassword &lt; Task
70 72 def create_message
71 73 hostname = self.requestor.environment.default_hostname
72 74 code = self.code
  75 + port = self.port
73 76  
74 77 lambda do
75 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 # TODO: document the use of config/web.yml in a INSTALL document
7 7 module Noosfero::URL
8 8  
  9 + include ActionController::UrlWriter
  10 +
9 11 class << self
10 12  
11 13 def config
... ... @@ -14,15 +16,25 @@ module Noosfero::URL
14 16 if File.exists?(config_file)
15 17 @config = YAML::load_file(config_file)
16 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 28 end
22 29 end
23 30  
24 31 @config
25 32 end
  33 +
  34 + def included(other_module)
  35 + other_module.send(:include, ActionController::UrlWriter)
  36 + end
  37 +
26 38 end
27 39  
28 40 def port
... ... @@ -33,4 +45,18 @@ module Noosfero::URL
33 45 Noosfero::URL.config['path']
34 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 62 end
... ...
test/unit/noosfero_url_test.rb
... ... @@ -34,4 +34,16 @@ class NoosferoURLTest &lt; Test::Unit::TestCase
34 34 assert_equal 9999, self.port
35 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 49 end
... ...