Commit 3e8bfd50051a01c94c25214484e478ae53d92b27
1 parent
e069a353
Exists in
master
and in
28 other branches
application-helper: refactor gravatar logic
Showing
12 changed files
with
84 additions
and
63 deletions
Show diff stats
app/controllers/public/account_controller.rb
app/helpers/application_helper.rb
... | ... | @@ -38,6 +38,8 @@ module ApplicationHelper |
38 | 38 | |
39 | 39 | include LayoutHelper |
40 | 40 | |
41 | + include Noosfero::Gravatar | |
42 | + | |
41 | 43 | def locale |
42 | 44 | (@page && !@page.language.blank?) ? @page.language : FastGettext.locale |
43 | 45 | end |
... | ... | @@ -356,8 +358,7 @@ module ApplicationHelper |
356 | 358 | end |
357 | 359 | |
358 | 360 | def theme_path |
359 | - if ( respond_to?(:session) && session[:theme] ) || | |
360 | - ( @controller && @controller.session[:theme] ) | |
361 | + if session[:theme] | |
361 | 362 | '/user_themes/' + current_theme |
362 | 363 | else |
363 | 364 | '/designs/themes/' + current_theme |
... | ... | @@ -367,8 +368,7 @@ module ApplicationHelper |
367 | 368 | def current_theme |
368 | 369 | @current_theme ||= |
369 | 370 | begin |
370 | - if ( respond_to?(:session) && session[:theme] ) || | |
371 | - ( @controller && @controller.session[:theme] ) | |
371 | + if session[:theme] | |
372 | 372 | session[:theme] |
373 | 373 | else |
374 | 374 | # utility for developers: set the theme to 'random' in development mode and |
... | ... | @@ -591,28 +591,8 @@ module ApplicationHelper |
591 | 591 | :class => 'vcard'), :class => 'common-profile-list-block') |
592 | 592 | end |
593 | 593 | |
594 | - def gravatar_url_for(email, options = {}) | |
595 | - # Ta dando erro de roteamento | |
596 | - default = theme_option['gravatar'] || NOOSFERO_CONF['gravatar'] || nil | |
597 | - url_for( { :gravatar_id => Digest::MD5.hexdigest(email.to_s), | |
598 | - :host => 'www.gravatar.com', | |
599 | - :protocol => 'http://', | |
600 | - :only_path => false, | |
601 | - :controller => 'avatar.php', | |
602 | - :d => default | |
603 | - }.merge(options) ) | |
604 | - end | |
605 | - | |
606 | - def str_gravatar_url_for(email, options = {}) | |
607 | - default = theme_option['gravatar'] || NOOSFERO_CONF['gravatar'] || nil | |
608 | - "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.to_s)}?" + { | |
609 | - :only_path => false, | |
610 | - :d => default | |
611 | - }.merge(options).map{|k,v| '%s=%s' % [ k,v ] }.join('&') | |
612 | - end | |
613 | - | |
614 | - def gravatar_profile_url(email) | |
615 | - 'http://www.gravatar.com/'+ Digest::MD5.hexdigest(email.to_s) | |
594 | + def gravatar_default | |
595 | + (respond_to?(:theme_option) && theme_option.present? && theme_option['gravatar']) || NOOSFERO_CONF['gravatar'] | |
616 | 596 | end |
617 | 597 | |
618 | 598 | attr_reader :environment | ... | ... |
app/models/person.rb
... | ... | @@ -476,6 +476,13 @@ class Person < Profile |
476 | 476 | self.fields_privacy.nil? ? [] : self.fields_privacy.reject{ |k, v| v != 'public' }.keys.map(&:to_s) |
477 | 477 | end |
478 | 478 | |
479 | + include Noosfero::Gravatar | |
480 | + | |
481 | + def profile_custom_icon(gravatar_default=nil) | |
482 | + (self.image.present? && self.image.public_filename(:icon)) || | |
483 | + gravatar_profile_image_url(self.email, :size=>20, :d => gravatar_default) | |
484 | + end | |
485 | + | |
479 | 486 | protected |
480 | 487 | |
481 | 488 | def followed_by?(profile) | ... | ... |
app/models/profile.rb
... | ... | @@ -844,8 +844,10 @@ private :generate_url, :url_options |
844 | 844 | }[amount] || _("%s members") % amount |
845 | 845 | end |
846 | 846 | |
847 | - def profile_custom_icon | |
848 | - self.image.public_filename(:icon) unless self.image.blank? | |
847 | + include Noosfero::Gravatar | |
848 | + | |
849 | + def profile_custom_icon(gravatar_default=nil) | |
850 | + image.public_filename(:icon) if image.present? | |
849 | 851 | end |
850 | 852 | |
851 | 853 | def jid(options = {}) | ... | ... |
app/models/user.rb
... | ... | @@ -275,25 +275,21 @@ class User < ActiveRecord::Base |
275 | 275 | end |
276 | 276 | end |
277 | 277 | |
278 | - include ApplicationHelper | |
279 | - | |
280 | - def data_hash(controller=nil) | |
281 | - @controller ||= controller | |
278 | + def data_hash(gravatar_default = nil) | |
282 | 279 | friends_list = {} |
283 | 280 | enterprises = person.enterprises.map { |e| { 'name' => e.short_name, 'identifier' => e.identifier } } |
284 | 281 | self.person.friends.online.map do |person| |
285 | 282 | friends_list[person.identifier] = { |
286 | - 'avatar' => person.profile_custom_icon, | |
283 | + 'avatar' => person.profile_custom_icon(gravatar_default), | |
287 | 284 | 'name' => person.short_name, |
288 | 285 | 'jid' => person.full_jid, |
289 | 286 | 'status' => person.user.chat_status, |
290 | 287 | } |
291 | 288 | end |
292 | - avatar = self.person.profile_custom_icon | |
293 | - avatar = str_gravatar_url_for(self.email, :size=>20) if avatar.blank? | |
289 | + | |
294 | 290 | { |
295 | 291 | 'login' => self.login, |
296 | - 'avatar' => avatar, | |
292 | + 'avatar' => self.person.profile_custom_icon(gravatar_default), | |
297 | 293 | 'is_admin' => self.person.is_admin?, |
298 | 294 | 'since_month' => self.person.created_at.month, |
299 | 295 | 'since_year' => self.person.created_at.year, | ... | ... |
app/views/comment/_comment.rhtml
... | ... | @@ -13,11 +13,11 @@ |
13 | 13 | <% else %> |
14 | 14 | <% url_image, status_class = comment.author_id ? |
15 | 15 | [comment.removed_user_image, 'icon-user-removed'] : |
16 | - [str_gravatar_url_for( comment.email, :size => 50, :d=>404 ), 'icon-user-unknown'] %> | |
16 | + [gravatar_profile_image_url( comment.email, :size => 50, :d=>404 ), 'icon-user-unknown'] %> | |
17 | 17 | |
18 | 18 | <%= link_to( |
19 | 19 | image_tag(url_image, :onerror=>'gravatarCommentFailback(this)', |
20 | - 'data-gravatar'=>str_gravatar_url_for(comment.email, :size=>50)) + | |
20 | + 'data-gravatar'=>gravatar_profile_image_url(comment.email, :size=>50)) + | |
21 | 21 | content_tag('span', comment.author_name, :class => 'comment-info') + |
22 | 22 | content_tag('span', comment.message, |
23 | 23 | :class => 'comment-user-status ' + status_class), | ... | ... |
app/views/profile/_comment.rhtml
... | ... | @@ -16,11 +16,11 @@ |
16 | 16 | <% else %> |
17 | 17 | <% url_image, status_class = comment.author_id ? |
18 | 18 | [comment.removed_user_image, 'icon-user-removed'] : |
19 | - [str_gravatar_url_for( comment.email ), 'icon-user-unknown'] %> | |
19 | + [gravatar_profile_image_url( comment.email ), 'icon-user-unknown'] %> | |
20 | 20 | |
21 | 21 | <%= link_to( |
22 | 22 | image_tag(url_image, :onerror=>'gravatarCommentFailback(this)', |
23 | - 'data-gravatar'=>str_gravatar_url_for(comment.email)) + | |
23 | + 'data-gravatar'=>gravatar_profile_image_url(comment.email)) + | |
24 | 24 | content_tag('span', comment.author_name, :class => 'comment-info') + |
25 | 25 | content_tag('span', comment.message, |
26 | 26 | :class => 'comment-user-status comment-user-status-wall ' + status_class), | ... | ... |
app/views/search/_comment.rhtml
... | ... | @@ -8,7 +8,7 @@ |
8 | 8 | <%# unauthenticated user: display gravatar icon %> |
9 | 9 | <%= content_tag 'span', ' ', |
10 | 10 | :class => 'comment-picture', |
11 | - :style => 'background-image:url(%s)' % str_gravatar_url_for( comment.email, :size => 20 ) | |
11 | + :style => 'background-image:url(%s)' % gravatar_profile_image_url( comment.email, :size => 20 ) | |
12 | 12 | %> |
13 | 13 | <%- end %> |
14 | 14 | <strong><%= link_to(comment.title, comment.url) %></strong> | ... | ... |
... | ... | @@ -0,0 +1,11 @@ |
1 | +module Noosfero::Gravatar | |
2 | + def gravatar_profile_image_url(email, options = {}) | |
3 | + "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.to_s)}?" + { | |
4 | + :only_path => false, | |
5 | + }.merge(options).map{|k,v| '%s=%s' % [ k,v ] }.join('&') | |
6 | + end | |
7 | + | |
8 | + def gravatar_profile_url(email) | |
9 | + 'http://www.gravatar.com/'+ Digest::MD5.hexdigest(email.to_s) | |
10 | + end | |
11 | +end | ... | ... |
test/unit/application_helper_test.rb
... | ... | @@ -466,32 +466,19 @@ class ApplicationHelperTest < ActiveSupport::TestCase |
466 | 466 | assert_match(/Community nick/, page_title) |
467 | 467 | end |
468 | 468 | |
469 | - should 'generate a gravatar image url' do | |
470 | - stubs(:environment).returns(Environment.default) | |
471 | - @controller = ApplicationController.new | |
472 | - | |
469 | + should 'gravatar default parameter' do | |
470 | + profile = mock | |
471 | + profile.stubs(:theme).returns('some-theme') | |
472 | + stubs(:profile).returns(profile) | |
473 | 473 | with_constants :NOOSFERO_CONF => {'gravatar' => 'crazyvatar'} do |
474 | - url = str_gravatar_url_for( 'rms@gnu.org', :size => 50 ) | |
475 | - assert_match(/^http:\/\/www\.gravatar\.com\/avatar\.php\?/, url) | |
476 | - assert_match(/(\?|&)gravatar_id=ed5214d4b49154ba0dc397a28ee90eb7(&|$)/, url) | |
477 | - assert_match(/(\?|&)d=crazyvatar(&|$)/, url) | |
478 | - assert_match(/(\?|&)size=50(&|$)/, url) | |
474 | + assert_equal gravatar_default, 'crazyvatar' | |
479 | 475 | end |
480 | 476 | stubs(:theme_option).returns('gravatar' => 'nicevatar') |
481 | 477 | with_constants :NOOSFERO_CONF => {'gravatar' => 'crazyvatar'} do |
482 | - url = str_gravatar_url_for( 'rms@gnu.org', :size => 50 ) | |
483 | - assert_match(/^http:\/\/www\.gravatar\.com\/avatar\.php\?/, url) | |
484 | - assert_match(/(\?|&)gravatar_id=ed5214d4b49154ba0dc397a28ee90eb7(&|$)/, url) | |
485 | - assert_match(/(\?|&)d=nicevatar(&|$)/, url) | |
486 | - assert_match(/(\?|&)size=50(&|$)/, url) | |
478 | + assert_equal gravatar_default, 'nicevatar' | |
487 | 479 | end |
488 | 480 | end |
489 | 481 | |
490 | - should 'generate a gravatar profile url' do | |
491 | - url = gravatar_profile_url( 'rms@gnu.org' ) | |
492 | - assert_equal('http://www.gravatar.com/ed5214d4b49154ba0dc397a28ee90eb7', url) | |
493 | - end | |
494 | - | |
495 | 482 | should 'use theme passed via param when in development mode' do |
496 | 483 | stubs(:environment).returns(Environment.new(:theme => 'environment-theme')) |
497 | 484 | ENV.stubs(:[]).with('RAILS_ENV').returns('development') | ... | ... |
... | ... | @@ -0,0 +1,26 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class GravatarTest < ActiveSupport::TestCase | |
4 | + | |
5 | + def setup | |
6 | + @object = Object.new | |
7 | + @object.extend(Noosfero::Gravatar) | |
8 | + end | |
9 | + | |
10 | + should 'generate a gravatar image url' do | |
11 | + url = @object.gravatar_profile_image_url( 'rms@gnu.org', :size => 50, :d => 'crazyvatar' ) | |
12 | + assert_match(/^http:\/\/www\.gravatar\.com\/avatar\/ed5214d4b49154ba0dc397a28ee90eb7?/, url) | |
13 | + assert_match(/(\?|&)d=crazyvatar(&|$)/, url) | |
14 | + assert_match(/(\?|&)size=50(&|$)/, url) | |
15 | + | |
16 | + url = @object.gravatar_profile_image_url( 'rms@gnu.org', :size => 50, :d => 'nicevatar' ) | |
17 | + assert_match(/^http:\/\/www\.gravatar\.com\/avatar\/ed5214d4b49154ba0dc397a28ee90eb7?/, url) | |
18 | + assert_match(/(\?|&)d=nicevatar(&|$)/, url) | |
19 | + assert_match(/(\?|&)size=50(&|$)/, url) | |
20 | + end | |
21 | + | |
22 | + should 'generate a gravatar profile url' do | |
23 | + url = @object.gravatar_profile_url( 'rms@gnu.org' ) | |
24 | + assert_equal('http://www.gravatar.com/ed5214d4b49154ba0dc397a28ee90eb7', url) | |
25 | + end | |
26 | +end | ... | ... |
test/unit/user_test.rb
... | ... | @@ -336,10 +336,22 @@ class UserTest < ActiveSupport::TestCase |
336 | 336 | Person.any_instance.stubs(:is_admin?).returns(true) |
337 | 337 | Person.any_instance.stubs(:created_at).returns(DateTime.parse('16-08-2010')) |
338 | 338 | expected_hash = { |
339 | - 'login' => 'x_and_y', 'is_admin' => true, 'since_month' => 8, 'chat_enabled' => false, 'since_year' => 2010, 'email_domain' => nil, 'amount_of_friends' => 0, | |
339 | + 'login' => 'x_and_y', 'is_admin' => true, 'since_month' => 8, | |
340 | + 'chat_enabled' => false, 'since_year' => 2010, 'avatar' => | |
341 | + 'http://www.gravatar.com/avatar/a0517761d5125820c28d87860bc7c02e?only_path=false&d=&size=20', | |
342 | + 'email_domain' => nil, 'amount_of_friends' => 0, | |
340 | 343 | 'friends_list' => {}, 'enterprises' => [], |
341 | 344 | } |
342 | - assert_equal expected_hash, person.user.data_hash | |
345 | + | |
346 | + assert_equal expected_hash['login'], person.user.data_hash['login'] | |
347 | + assert_equal expected_hash['is_admin'], person.user.data_hash['is_admin'] | |
348 | + assert_equal expected_hash['since_month'], person.user.data_hash['since_month'] | |
349 | + assert_equal expected_hash['chat_enabled'], person.user.data_hash['chat_enabled'] | |
350 | + assert_equal expected_hash['since_year'], person.user.data_hash['since_year'] | |
351 | + assert_equal expected_hash['avatar'], person.user.data_hash['avatar'] | |
352 | + assert_equal expected_hash['email_domain'], person.user.data_hash['email_domain'] | |
353 | + assert_equal expected_hash['amount_of_friends'], person.user.data_hash['amount_of_friends'] | |
354 | + assert_equal expected_hash['friends_list'], person.user.data_hash['friends_list'] | |
343 | 355 | end |
344 | 356 | |
345 | 357 | should "data_hash return the friends_list information" do | ... | ... |