Commit d9815552bdc52d453b47cf3e57ea90f7602770ff

Authored by Aurelio A. Heckert
Committed by Antonio Terceiro
1 parent 73f9889f

More Beauty around web2.0conf code

- Remove unnecessary config file tests for GoogleMaps
- Also add the default gravatar configuration

(ActionItem1324)
app/helpers/application_helper.rb
@@ -26,6 +26,17 @@ module ApplicationHelper @@ -26,6 +26,17 @@ module ApplicationHelper
26 26
27 include AccountHelper 27 include AccountHelper
28 28
  29 + def load_web2_conf
  30 + if File.exists?( RAILS_ROOT + '/config/web2.0.yml')
  31 + YAML.load_file( RAILS_ROOT + '/config/web2.0.yml' )
  32 + else
  33 + {}
  34 + end
  35 + end
  36 + def web2_conf
  37 + @web_conf ||= load_web2_conf
  38 + end
  39 +
29 # Displays context help. You can pass the content of the help message as the 40 # Displays context help. You can pass the content of the help message as the
30 # first parameter or using template code inside a block passed to this 41 # first parameter or using template code inside a block passed to this
31 # method. *Note*: the block is ignored if <tt>content</tt> is not 42 # method. *Note*: the block is ignored if <tt>content</tt> is not
@@ -500,18 +511,21 @@ module ApplicationHelper @@ -500,18 +511,21 @@ module ApplicationHelper
500 :host => 'www.gravatar.com', 511 :host => 'www.gravatar.com',
501 :protocol => 'http://', 512 :protocol => 'http://',
502 :only_path => false, 513 :only_path => false,
503 - :controller => 'avatar.php' 514 + :controller => 'avatar.php',
  515 + :d => web2_conf['gravatar'] ? web2_conf['gravatar']['default'] : nil
504 }.merge(options) ) 516 }.merge(options) )
505 end 517 end
506 - 518 +
507 def str_gravatar_url_for(email, options = {}) 519 def str_gravatar_url_for(email, options = {})
  520 + default = web2_conf['gravatar'] ? web2_conf['gravatar']['default'] : nil
508 url = 'http://www.gravatar.com/avatar.php?gravatar_id=' + 521 url = 'http://www.gravatar.com/avatar.php?gravatar_id=' +
509 Digest::MD5.hexdigest(email) 522 Digest::MD5.hexdigest(email)
510 - { :only_path => false }.merge(options).each { |k,v| 523 + {
  524 + :only_path => false,
  525 + :d => default
  526 + }.merge(options).each { |k,v|
511 url += ( '&%s=%s' % [ k,v ] ) 527 url += ( '&%s=%s' % [ k,v ] )
512 } 528 }
513 - # we can set the default imgage with this:  
514 - # :default => 'DOMAIN/images/icons-app/gravatar-minor.gif'  
515 url 529 url
516 end 530 end
517 531
app/models/google_maps.rb
@@ -4,22 +4,14 @@ class GoogleMaps @@ -4,22 +4,14 @@ class GoogleMaps
4 4
5 class << self 5 class << self
6 6
  7 + include ApplicationHelper
  8 +
7 def erase_config 9 def erase_config
8 @config = nil 10 @config = nil
9 end 11 end
10 12
11 - def config_file  
12 - File.join(RAILS_ROOT, 'config', 'web2.0.yml')  
13 - end  
14 -  
15 def config 13 def config
16 - if @config.nil?  
17 - if File.exists?(config_file)  
18 - yaml = YAML.load_file(config_file)  
19 - @config = yaml['googlemaps']  
20 - end  
21 - end  
22 - 14 + @config = web2_conf['googlemaps'] if @config.nil?
23 @config ||= {} 15 @config ||= {}
24 end 16 end
25 17
app/views/content_viewer/view_page.rhtml
@@ -2,10 +2,8 @@ @@ -2,10 +2,8 @@
2 2
3 <% 3 <%
4 # AddThis Button 4 # AddThis Button
5 - if block_given? and File.exists?( RAILS_ROOT + '/config/web2.0.yml')  
6 - opts = YAML.load_file( RAILS_ROOT + '/config/web2.0.yml' )  
7 - if opts['addthis']  
8 - opts = opts['addthis'] 5 + if block_given? and web2_conf['addthis']
  6 + opts = web2_conf['addthis']
9 %> 7 %>
10 <div id="addThis"> 8 <div id="addThis">
11 <script type="text/javascript"> 9 <script type="text/javascript">
@@ -19,7 +17,7 @@ @@ -19,7 +17,7 @@
19 %></script> 17 %></script>
20 <a href="http://www.addthis.com/bookmark.php" id="bt_addThis" target="_blank" onmouseover="return addthis_open(this, '', '[URL]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="/images/bt-bookmark.gif" width="53" height="16" border="0" alt="" /></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script> 18 <a href="http://www.addthis.com/bookmark.php" id="bt_addThis" target="_blank" onmouseover="return addthis_open(this, '', '[URL]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="/images/bt-bookmark.gif" width="53" height="16" border="0" alt="" /></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>
21 </div> 19 </div>
22 -<% end; end %> 20 +<% end %>
23 21
24 <div> 22 <div>
25 <%= article_title(@page, :no_link => true) %> 23 <%= article_title(@page, :no_link => true) %>
config/web2.0.yml.dist
@@ -8,3 +8,5 @@ addthis: @@ -8,3 +8,5 @@ addthis:
8 options: favorites, email, digg, delicious, technorati, slashdot, twitter, more 8 options: favorites, email, digg, delicious, technorati, slashdot, twitter, more
9 googlemaps: 9 googlemaps:
10 key: <id google> 10 key: <id google>
  11 +gravatar:
  12 + default: wavatar
test/unit/application_helper_test.rb
@@ -8,6 +8,18 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase @@ -8,6 +8,18 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase
8 self.stubs(:session).returns({}) 8 self.stubs(:session).returns({})
9 end 9 end
10 10
  11 + should 'retrieve conf from "web2.0" config file' do
  12 + yml = RAILS_ROOT + '/config/web2.0.yml'
  13 + conf = {
  14 + 'addthis'=>{'pub'=>'mylogin', 'options'=>'favorites, email'},
  15 + 'gravatar'=>{'default'=>'wavatar'}
  16 + }
  17 + # does not work!
  18 + File.stubs(:exists?).with(yml).returns(true)
  19 + YAML.stubs(:load_file).with(yml).returns(conf)
  20 + assert_equal conf, web2_conf
  21 + end
  22 +
11 should 'calculate correctly partial for object' do 23 should 'calculate correctly partial for object' do
12 self.stubs(:params).returns({:controller => 'test'}) 24 self.stubs(:params).returns({:controller => 'test'})
13 25
@@ -518,6 +530,14 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase @@ -518,6 +530,14 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase
518 assert_match(/Community nickname/, page_title) 530 assert_match(/Community nickname/, page_title)
519 end 531 end
520 532
  533 + should 'generate a gravatar url' do
  534 + url = str_gravatar_url_for( 'rms@gnu.org', :size => 50 )
  535 + assert_match(/^http:\/\/www\.gravatar\.com\/avatar\.php\?/, url)
  536 + assert_match(/(\?|&)gravatar_id=ed5214d4b49154ba0dc397a28ee90eb7(&|$)/, url)
  537 + assert_match(/(\?|&)d=wavatar(&|$)/, url)
  538 + assert_match(/(\?|&)size=50(&|$)/, url)
  539 + end
  540 +
521 protected 541 protected
522 542
523 def url_for(args = {}) 543 def url_for(args = {})
test/unit/google_maps_test.rb
@@ -2,45 +2,23 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39; @@ -2,45 +2,23 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39;
2 2
3 class GoogleMapsTest < Test::Unit::TestCase 3 class GoogleMapsTest < Test::Unit::TestCase
4 4
5 - CONFIG_FILE = '/not/existing.yml'  
6 -  
7 def setup 5 def setup
8 # force loading of config at every test 6 # force loading of config at every test
9 GoogleMaps.erase_config 7 GoogleMaps.erase_config
10 - GoogleMaps.stubs(:config_file).returns(CONFIG_FILE)  
11 - end  
12 -  
13 - should 'retrieve key from "web2.0" config file' do  
14 - File.expects(:exists?).with(CONFIG_FILE).returns(true)  
15 - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'key' => 'MYKEY' }})  
16 - assert_equal 'MYKEY', GoogleMaps.key  
17 end 8 end
18 9
19 should 'enable when key is defined' do 10 should 'enable when key is defined' do
20 - File.expects(:exists?).with(CONFIG_FILE).returns(true)  
21 - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'key' => 'MYKEY' }}) 11 + GoogleMaps.stubs(:config).returns({ 'key' => 'MYKEY' })
22 assert GoogleMaps.enabled? 12 assert GoogleMaps.enabled?
23 end 13 end
24 14
25 - should 'disable if config file not present' do  
26 - File.expects(:exists?).with(CONFIG_FILE).returns(false)  
27 - assert !GoogleMaps.enabled?  
28 - end  
29 -  
30 should 'disable if key not defined' do 15 should 'disable if key not defined' do
31 - File.expects(:exists?).with(CONFIG_FILE).returns(true)  
32 - YAML.expects(:load_file).with(CONFIG_FILE).returns({}) 16 + GoogleMaps.stubs(:config).returns({})
33 assert !GoogleMaps.enabled? 17 assert !GoogleMaps.enabled?
34 end 18 end
35 -  
36 - should 'not crash if config not informed' do  
37 - File.expects(:exists?).with(CONFIG_FILE).returns(true)  
38 - YAML.expects(:load_file).with(CONFIG_FILE).returns({})  
39 - assert_equal '', GoogleMaps.key  
40 - end  
41 19
42 - should 'not crash if config file not found' do  
43 - GoogleMaps.expects(:config_file).returns('/not/present.yml') 20 + should 'not crash if config not informed' do
  21 + GoogleMaps.stubs(:config).returns({})
44 assert_equal '', GoogleMaps.key 22 assert_equal '', GoogleMaps.key
45 end 23 end
46 24
@@ -50,14 +28,12 @@ class GoogleMapsTest &lt; Test::Unit::TestCase @@ -50,14 +28,12 @@ class GoogleMapsTest &lt; Test::Unit::TestCase
50 end 28 end
51 29
52 should 'provide initial_zoom setting' do 30 should 'provide initial_zoom setting' do
53 - File.expects(:exists?).with(CONFIG_FILE).returns(true)  
54 - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'initial_zoom' => 2}}) 31 + GoogleMaps.stubs(:config).returns({'initial_zoom' => 2})
55 assert_equal 2, GoogleMaps.initial_zoom 32 assert_equal 2, GoogleMaps.initial_zoom
56 end 33 end
57 34
58 should 'use 4 as default initial_zoom' do 35 should 'use 4 as default initial_zoom' do
59 - File.expects(:exists?).with(CONFIG_FILE).returns(true)  
60 - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { }}) 36 + GoogleMaps.stubs(:config).returns({})
61 assert_equal 4, GoogleMaps.initial_zoom 37 assert_equal 4, GoogleMaps.initial_zoom
62 end 38 end
63 39