From d9815552bdc52d453b47cf3e57ea90f7602770ff Mon Sep 17 00:00:00 2001 From: Aurelio A. Heckert Date: Mon, 23 Nov 2009 18:41:03 -0300 Subject: [PATCH] More Beauty around web2.0conf code --- app/helpers/application_helper.rb | 24 +++++++++++++++++++----- app/models/google_maps.rb | 14 +++----------- app/views/content_viewer/view_page.rhtml | 8 +++----- config/web2.0.yml.dist | 2 ++ test/unit/application_helper_test.rb | 20 ++++++++++++++++++++ test/unit/google_maps_test.rb | 36 ++++++------------------------------ 6 files changed, 53 insertions(+), 51 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index df9c88e..60a63a4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -26,6 +26,17 @@ module ApplicationHelper include AccountHelper + def load_web2_conf + if File.exists?( RAILS_ROOT + '/config/web2.0.yml') + YAML.load_file( RAILS_ROOT + '/config/web2.0.yml' ) + else + {} + end + end + def web2_conf + @web_conf ||= load_web2_conf + end + # Displays context help. You can pass the content of the help message as the # first parameter or using template code inside a block passed to this # method. *Note*: the block is ignored if content is not @@ -500,18 +511,21 @@ module ApplicationHelper :host => 'www.gravatar.com', :protocol => 'http://', :only_path => false, - :controller => 'avatar.php' + :controller => 'avatar.php', + :d => web2_conf['gravatar'] ? web2_conf['gravatar']['default'] : nil }.merge(options) ) end - + def str_gravatar_url_for(email, options = {}) + default = web2_conf['gravatar'] ? web2_conf['gravatar']['default'] : nil url = 'http://www.gravatar.com/avatar.php?gravatar_id=' + Digest::MD5.hexdigest(email) - { :only_path => false }.merge(options).each { |k,v| + { + :only_path => false, + :d => default + }.merge(options).each { |k,v| url += ( '&%s=%s' % [ k,v ] ) } - # we can set the default imgage with this: - # :default => 'DOMAIN/images/icons-app/gravatar-minor.gif' url end diff --git a/app/models/google_maps.rb b/app/models/google_maps.rb index 4850abc..a6e0111 100644 --- a/app/models/google_maps.rb +++ b/app/models/google_maps.rb @@ -4,22 +4,14 @@ class GoogleMaps class << self + include ApplicationHelper + def erase_config @config = nil end - def config_file - File.join(RAILS_ROOT, 'config', 'web2.0.yml') - end - def config - if @config.nil? - if File.exists?(config_file) - yaml = YAML.load_file(config_file) - @config = yaml['googlemaps'] - end - end - + @config = web2_conf['googlemaps'] if @config.nil? @config ||= {} end diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index d92541b..c07f945 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -2,10 +2,8 @@ <% # AddThis Button - if block_given? and File.exists?( RAILS_ROOT + '/config/web2.0.yml') - opts = YAML.load_file( RAILS_ROOT + '/config/web2.0.yml' ) - if opts['addthis'] - opts = opts['addthis'] + if block_given? and web2_conf['addthis'] + opts = web2_conf['addthis'] %>
-<% end; end %> +<% end %>
<%= article_title(@page, :no_link => true) %> diff --git a/config/web2.0.yml.dist b/config/web2.0.yml.dist index 5584781..a2764c6 100644 --- a/config/web2.0.yml.dist +++ b/config/web2.0.yml.dist @@ -8,3 +8,5 @@ addthis: options: favorites, email, digg, delicious, technorati, slashdot, twitter, more googlemaps: key: +gravatar: + default: wavatar diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 3ca1162..307d8b6 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -8,6 +8,18 @@ class ApplicationHelperTest < Test::Unit::TestCase self.stubs(:session).returns({}) end + should 'retrieve conf from "web2.0" config file' do + yml = RAILS_ROOT + '/config/web2.0.yml' + conf = { + 'addthis'=>{'pub'=>'mylogin', 'options'=>'favorites, email'}, + 'gravatar'=>{'default'=>'wavatar'} + } + # does not work! + File.stubs(:exists?).with(yml).returns(true) + YAML.stubs(:load_file).with(yml).returns(conf) + assert_equal conf, web2_conf + end + should 'calculate correctly partial for object' do self.stubs(:params).returns({:controller => 'test'}) @@ -518,6 +530,14 @@ class ApplicationHelperTest < Test::Unit::TestCase assert_match(/Community nickname/, page_title) end + should 'generate a gravatar url' do + url = str_gravatar_url_for( 'rms@gnu.org', :size => 50 ) + assert_match(/^http:\/\/www\.gravatar\.com\/avatar\.php\?/, url) + assert_match(/(\?|&)gravatar_id=ed5214d4b49154ba0dc397a28ee90eb7(&|$)/, url) + assert_match(/(\?|&)d=wavatar(&|$)/, url) + assert_match(/(\?|&)size=50(&|$)/, url) + end + protected def url_for(args = {}) diff --git a/test/unit/google_maps_test.rb b/test/unit/google_maps_test.rb index 5a7aa1e..4264a21 100644 --- a/test/unit/google_maps_test.rb +++ b/test/unit/google_maps_test.rb @@ -2,45 +2,23 @@ require File.dirname(__FILE__) + '/../test_helper' class GoogleMapsTest < Test::Unit::TestCase - CONFIG_FILE = '/not/existing.yml' - def setup # force loading of config at every test GoogleMaps.erase_config - GoogleMaps.stubs(:config_file).returns(CONFIG_FILE) - end - - should 'retrieve key from "web2.0" config file' do - File.expects(:exists?).with(CONFIG_FILE).returns(true) - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'key' => 'MYKEY' }}) - assert_equal 'MYKEY', GoogleMaps.key end should 'enable when key is defined' do - File.expects(:exists?).with(CONFIG_FILE).returns(true) - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'key' => 'MYKEY' }}) + GoogleMaps.stubs(:config).returns({ 'key' => 'MYKEY' }) assert GoogleMaps.enabled? end - should 'disable if config file not present' do - File.expects(:exists?).with(CONFIG_FILE).returns(false) - assert !GoogleMaps.enabled? - end - should 'disable if key not defined' do - File.expects(:exists?).with(CONFIG_FILE).returns(true) - YAML.expects(:load_file).with(CONFIG_FILE).returns({}) + GoogleMaps.stubs(:config).returns({}) assert !GoogleMaps.enabled? end - - should 'not crash if config not informed' do - File.expects(:exists?).with(CONFIG_FILE).returns(true) - YAML.expects(:load_file).with(CONFIG_FILE).returns({}) - assert_equal '', GoogleMaps.key - end - should 'not crash if config file not found' do - GoogleMaps.expects(:config_file).returns('/not/present.yml') + should 'not crash if config not informed' do + GoogleMaps.stubs(:config).returns({}) assert_equal '', GoogleMaps.key end @@ -50,14 +28,12 @@ class GoogleMapsTest < Test::Unit::TestCase end should 'provide initial_zoom setting' do - File.expects(:exists?).with(CONFIG_FILE).returns(true) - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'initial_zoom' => 2}}) + GoogleMaps.stubs(:config).returns({'initial_zoom' => 2}) assert_equal 2, GoogleMaps.initial_zoom end should 'use 4 as default initial_zoom' do - File.expects(:exists?).with(CONFIG_FILE).returns(true) - YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { }}) + GoogleMaps.stubs(:config).returns({}) assert_equal 4, GoogleMaps.initial_zoom end -- libgit2 0.21.2