diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bc5f925..78ff31c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,7 +1,7 @@ # -*- encoding : utf-8 -*- require 'kramdown' require 'digest/md5' -require 'uri' +require 'cgi' module ApplicationHelper @@ -58,20 +58,19 @@ def gravatar_image(user, options = {}) return nil if user.nil? || user.email.nil? hash = Digest::MD5.hexdigest(user.email.downcase) - query = options.select { |k, v| [:size, :default].include?(k) } - .map { |k, v| "#{k}=#{URI.encode(v.to_s)}" } - .join('&') - src = "//www.gravatar.com/avatar/#{hash}?#{query}" - - img_options = { :alt => user.nickname } - if mouseover = options[:mouseover] - img_options[:mouseover] = mouseover - end + opts = options.dup + query = opts.select { |k, v| [:size, :d].include?(k) } + .map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" } + .join('&') + img_options = { + :src => "//www.gravatar.com/avatar/#{hash}?#{query}", + :alt => user.nickname + } if size = options[:size] img_options[:width] = size img_options[:height] = size end - image_tag(src, img_options) + tag 'img', img_options, false, false end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 5fd9273..d2568c4 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -114,5 +114,22 @@ def current_user end end + describe 'gravatar_image' do + it 'returns nil for user without email' do + u = create(:user, :email => nil) + result = gravatar_image(u) + expect(result).to be_nil + end + it 'works' do + u = create(:user, :nickname => 'Foo', :email => 'foo@bar.com') + options = { + :size => 25, + :d => 'http://test.com/default.jpg' + } + result = gravatar_image(u, options) + expected = 'Foo' + expect(result).to eq(expected) + end + end end