Commit 945fe846a14dd1e1dc3a5b23c097224dbe471e50
Exists in
master
and in
4 other branches
Merge pull request #2007 from Partugal/patch-custom-gravatar
Allow using a custom Gravatar URL. Default is still set to the Gravatar.com service, but allows override for a custom gravatar-ish service.
Showing
5 changed files
with
40 additions
and
4 deletions
Show diff stats
app/decorators/commit_decorator.rb
@@ -76,7 +76,7 @@ class CommitDecorator < ApplicationDecorator | @@ -76,7 +76,7 @@ class CommitDecorator < ApplicationDecorator | ||
76 | source_name = send "#{options[:source]}_name".to_sym | 76 | source_name = send "#{options[:source]}_name".to_sym |
77 | source_email = send "#{options[:source]}_email".to_sym | 77 | source_email = send "#{options[:source]}_email".to_sym |
78 | text = if options[:avatar] | 78 | text = if options[:avatar] |
79 | - avatar = h.image_tag h.gravatar_icon(source_email, options[:size]), class: "avatar #{"s#{options[:size]}" if options[:size]}", width: options[:size] | 79 | + avatar = h.image_tag h.gravatar_icon(source_email, options[:size]), class: "avatar #{"s#{options[:size]}" if options[:size]}", width: options[:size], alt: "" |
80 | %Q{#{avatar} <span class="commit-#{options[:source]}-name">#{source_name}</span>} | 80 | %Q{#{avatar} <span class="commit-#{options[:source]}-name">#{source_name}</span>} |
81 | else | 81 | else |
82 | source_name | 82 | source_name |
app/helpers/application_helper.rb
1 | require 'digest/md5' | 1 | require 'digest/md5' |
2 | +require 'uri' | ||
2 | 3 | ||
3 | module ApplicationHelper | 4 | module ApplicationHelper |
4 | 5 | ||
@@ -30,13 +31,15 @@ module ApplicationHelper | @@ -30,13 +31,15 @@ module ApplicationHelper | ||
30 | args.any? { |v| v.to_s.downcase == action_name } | 31 | args.any? { |v| v.to_s.downcase == action_name } |
31 | end | 32 | end |
32 | 33 | ||
33 | - def gravatar_icon(user_email = '', size = 40) | 34 | + def gravatar_icon(user_email = '', size = nil) |
35 | + size = 40 if size.nil? || size <= 0 | ||
36 | + | ||
34 | if Gitlab.config.disable_gravatar? || user_email.blank? | 37 | if Gitlab.config.disable_gravatar? || user_email.blank? |
35 | 'no_avatar.png' | 38 | 'no_avatar.png' |
36 | else | 39 | else |
37 | - gravatar_prefix = request.ssl? ? "https://secure" : "http://www" | 40 | + gravatar_url = request.ssl? ? Gitlab.config.gravatar_ssl_url : Gitlab.config.gravatar_url |
38 | user_email.strip! | 41 | user_email.strip! |
39 | - "#{gravatar_prefix}.gravatar.com/avatar/#{Digest::MD5.hexdigest(user_email.downcase)}?s=#{size}&d=mm" | 42 | + sprintf(gravatar_url, {:hash => Digest::MD5.hexdigest(user_email.downcase), :email => URI.escape(user_email), :size => size}) |
40 | end | 43 | end |
41 | end | 44 | end |
42 | 45 |
config/gitlab.yml.example
@@ -24,6 +24,8 @@ app: | @@ -24,6 +24,8 @@ app: | ||
24 | # backup_path: "/vol/backups" # default: Rails.root + backups/ | 24 | # backup_path: "/vol/backups" # default: Rails.root + backups/ |
25 | # backup_keep_time: 604800 # default: 0 (forever) (in seconds) | 25 | # backup_keep_time: 604800 # default: 0 (forever) (in seconds) |
26 | # disable_gravatar: true # default: false - Disable user avatars from Gravatar.com | 26 | # disable_gravatar: true # default: false - Disable user avatars from Gravatar.com |
27 | + # gravatar_url: "http://" # default: http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=mm | ||
28 | + # gravatar_ssl_url: "https://" # default: https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=mm | ||
27 | 29 | ||
28 | 30 | ||
29 | # | 31 | # |
config/initializers/1_settings.rb
@@ -145,5 +145,14 @@ class Settings < Settingslogic | @@ -145,5 +145,14 @@ class Settings < Settingslogic | ||
145 | def disable_gravatar? | 145 | def disable_gravatar? |
146 | app['disable_gravatar'] || false | 146 | app['disable_gravatar'] || false |
147 | end | 147 | end |
148 | + | ||
149 | + def gravatar_url | ||
150 | + app['gravatar_url'] || 'http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=mm' | ||
151 | + end | ||
152 | + | ||
153 | + def gravatar_ssl_url | ||
154 | + app['gravatar_ssl_url'] || 'https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=mm' | ||
155 | + end | ||
156 | + | ||
148 | end | 157 | end |
149 | end | 158 | end |
spec/helpers/application_helper_spec.rb
@@ -51,14 +51,36 @@ describe ApplicationHelper do | @@ -51,14 +51,36 @@ describe ApplicationHelper do | ||
51 | gravatar_icon('').should == 'no_avatar.png' | 51 | gravatar_icon('').should == 'no_avatar.png' |
52 | end | 52 | end |
53 | 53 | ||
54 | + it "should return default gravatar url" do | ||
55 | + stub!(:request).and_return(double(:ssl? => false)) | ||
56 | + gravatar_icon(user_email).should match('http://www.gravatar.com/avatar/b58c6f14d292556214bd64909bcdb118') | ||
57 | + end | ||
58 | + | ||
54 | it "should use SSL when appropriate" do | 59 | it "should use SSL when appropriate" do |
55 | stub!(:request).and_return(double(:ssl? => true)) | 60 | stub!(:request).and_return(double(:ssl? => true)) |
56 | gravatar_icon(user_email).should match('https://secure.gravatar.com') | 61 | gravatar_icon(user_email).should match('https://secure.gravatar.com') |
57 | end | 62 | end |
58 | 63 | ||
64 | + it "should return custom gravatar path when gravatar_url is set" do | ||
65 | + stub!(:request).and_return(double(:ssl? => false)) | ||
66 | + Gitlab.config.stub(:gravatar_url).and_return('http://example.local/?s=%{size}&hash=%{hash}') | ||
67 | + gravatar_icon(user_email, 20).should == 'http://example.local/?s=20&hash=b58c6f14d292556214bd64909bcdb118' | ||
68 | + end | ||
69 | + | ||
59 | it "should accept a custom size" do | 70 | it "should accept a custom size" do |
60 | stub!(:request).and_return(double(:ssl? => false)) | 71 | stub!(:request).and_return(double(:ssl? => false)) |
61 | gravatar_icon(user_email, 64).should match(/\?s=64/) | 72 | gravatar_icon(user_email, 64).should match(/\?s=64/) |
62 | end | 73 | end |
74 | + | ||
75 | + it "should use default size when size is wrong" do | ||
76 | + stub!(:request).and_return(double(:ssl? => false)) | ||
77 | + gravatar_icon(user_email, nil).should match(/\?s=40/) | ||
78 | + end | ||
79 | + | ||
80 | + it "should be case insensitive" do | ||
81 | + stub!(:request).and_return(double(:ssl? => false)) | ||
82 | + gravatar_icon(user_email).should == gravatar_icon(user_email.upcase + " ") | ||
83 | + end | ||
84 | + | ||
63 | end | 85 | end |
64 | end | 86 | end |