Commit 45660754c26da022ba43e521a53a33b2840de15a
1 parent
b1d79e89
Exists in
master
and in
29 other branches
ActionItem78: writing a module to help generating URL in TaskMailer
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@644 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
73 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
1 | +require 'noosfero' | ||
2 | + | ||
3 | +# This module defines utility methods for generating URL's in contexts where | ||
4 | +# one does not have a request (i.e. ActionMailer classes like TaskMailer). | ||
5 | +# | ||
6 | +# TODO: document the use of config/web.yml in a INSTALL document | ||
7 | +module Noosfero::URL | ||
8 | + | ||
9 | + class << self | ||
10 | + | ||
11 | + def config | ||
12 | + if @config.nil? | ||
13 | + config_file = File.join(RAILS_ROOT, 'config', 'web.yml') | ||
14 | + if File.exists?(config_file) | ||
15 | + @config = YAML::load_file(config_file) | ||
16 | + else | ||
17 | + @config = { | ||
18 | + 'path' => '', | ||
19 | + 'port' => 3000 | ||
20 | + } | ||
21 | + end | ||
22 | + end | ||
23 | + | ||
24 | + @config | ||
25 | + end | ||
26 | + end | ||
27 | + | ||
28 | + def port | ||
29 | + Noosfero::URL.config['port'] | ||
30 | + end | ||
31 | + | ||
32 | + def path | ||
33 | + Noosfero::URL.config['path'] | ||
34 | + end | ||
35 | + | ||
36 | +end |
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | + | ||
2 | +require File.dirname(__FILE__) + '/../test_helper' | ||
3 | + | ||
4 | +require 'noosfero/url' | ||
5 | + | ||
6 | +class NoosferoURLTest < Test::Unit::TestCase | ||
7 | + | ||
8 | + include Noosfero::URL | ||
9 | + | ||
10 | + def setup | ||
11 | + Noosfero::URL.instance_variable_set('@config', nil) | ||
12 | + end | ||
13 | + | ||
14 | + should 'read the config file' do | ||
15 | + file = "#{RAILS_ROOT}/config/web.yml" | ||
16 | + File.expects(:exists?).with(file).returns(true) | ||
17 | + YAML.expects(:load_file).with(file).returns('path' => '/mypath', 'port' => 9999) | ||
18 | + assert_equal({'path' => '/mypath', 'port' => 9999}, Noosfero::URL.config) | ||
19 | + end | ||
20 | + | ||
21 | + should 'fallback correcly' do | ||
22 | + file = "#{RAILS_ROOT}/config/web.yml" | ||
23 | + File.expects(:exists?).with(file).returns(false) | ||
24 | + assert_equal({'path' => '', 'port' => 3000}, Noosfero::URL.config) | ||
25 | + end | ||
26 | + | ||
27 | + should 'read the correct path' do | ||
28 | + Noosfero::URL.stubs(:config).returns('path' => '/mypath') | ||
29 | + assert_equal '/mypath', self.path | ||
30 | + end | ||
31 | + | ||
32 | + should 'read the correct port' do | ||
33 | + Noosfero::URL.stubs(:config).returns('port' => 9999) | ||
34 | + assert_equal 9999, self.port | ||
35 | + end | ||
36 | + | ||
37 | +end |