annotate .svn/pristine/7a/7a017fdd06e31fbf6cfa542010617fca4f01cf26.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 require 'digest/md5'
Chris@909 2 require 'cgi'
Chris@909 3
Chris@909 4 module GravatarHelper
Chris@909 5
Chris@909 6 # These are the options that control the default behavior of the public
Chris@909 7 # methods. They can be overridden during the actual call to the helper,
Chris@909 8 # or you can set them in your environment.rb as such:
Chris@909 9 #
Chris@909 10 # # Allow racier gravatars
Chris@909 11 # GravatarHelper::DEFAULT_OPTIONS[:rating] = 'R'
Chris@909 12 #
Chris@909 13 DEFAULT_OPTIONS = {
Chris@909 14 # The URL of a default image to display if the given email address does
Chris@909 15 # not have a gravatar.
Chris@909 16 :default => nil,
Chris@909 17
Chris@909 18 # The default size in pixels for the gravatar image (they're square).
Chris@909 19 :size => 50,
Chris@909 20
Chris@909 21 # The maximum allowed MPAA rating for gravatars. This allows you to
Chris@909 22 # exclude gravatars that may be out of character for your site.
Chris@909 23 :rating => 'PG',
Chris@909 24
Chris@909 25 # The alt text to use in the img tag for the gravatar. Since it's a
Chris@909 26 # decorational picture, the alt text should be empty according to the
Chris@909 27 # XHTML specs.
Chris@909 28 :alt => '',
Chris@909 29
Chris@909 30 # The title text to use for the img tag for the gravatar.
Chris@909 31 :title => '',
Chris@909 32
Chris@909 33 # The class to assign to the img tag for the gravatar.
Chris@909 34 :class => 'gravatar',
Chris@909 35
Chris@909 36 # Whether or not to display the gravatars using HTTPS instead of HTTP
Chris@909 37 :ssl => false,
Chris@909 38 }
Chris@909 39
Chris@909 40 # The methods that will be made available to your views.
Chris@909 41 module PublicMethods
Chris@909 42
Chris@909 43 # Return the HTML img tag for the given user's gravatar. Presumes that
Chris@909 44 # the given user object will respond_to "email", and return the user's
Chris@909 45 # email address.
Chris@909 46 def gravatar_for(user, options={})
Chris@909 47 gravatar(user.email, options)
Chris@909 48 end
Chris@909 49
Chris@909 50 # Return the HTML img tag for the given email address's gravatar.
Chris@909 51 def gravatar(email, options={})
Chris@909 52 src = h(gravatar_url(email, options))
Chris@909 53 options = DEFAULT_OPTIONS.merge(options)
Chris@909 54 [:class, :alt, :size, :title].each { |opt| options[opt] = h(options[opt]) }
Chris@909 55 "<img class=\"#{options[:class]}\" alt=\"#{options[:alt]}\" title=\"#{options[:title]}\" width=\"#{options[:size]}\" height=\"#{options[:size]}\" src=\"#{src}\" />"
Chris@909 56 end
Chris@909 57
Chris@909 58 # Returns the base Gravatar URL for the given email hash. If ssl evaluates to true,
Chris@909 59 # a secure URL will be used instead. This is required when the gravatar is to be
Chris@909 60 # displayed on a HTTPS site.
Chris@909 61 def gravatar_api_url(hash, ssl=false)
Chris@909 62 if ssl
Chris@909 63 "https://secure.gravatar.com/avatar/#{hash}"
Chris@909 64 else
Chris@909 65 "http://www.gravatar.com/avatar/#{hash}"
Chris@909 66 end
Chris@909 67 end
Chris@909 68
Chris@909 69 # Return the gravatar URL for the given email address.
Chris@909 70 def gravatar_url(email, options={})
Chris@909 71 email_hash = Digest::MD5.hexdigest(email)
Chris@909 72 options = DEFAULT_OPTIONS.merge(options)
Chris@909 73 options[:default] = CGI::escape(options[:default]) unless options[:default].nil?
Chris@909 74 gravatar_api_url(email_hash, options.delete(:ssl)).tap do |url|
Chris@909 75 opts = []
Chris@909 76 [:rating, :size, :default].each do |opt|
Chris@909 77 unless options[opt].nil?
Chris@909 78 value = h(options[opt])
Chris@909 79 opts << [opt, value].join('=')
Chris@909 80 end
Chris@909 81 end
Chris@909 82 url << "?#{opts.join('&')}" unless opts.empty?
Chris@909 83 end
Chris@909 84 end
Chris@909 85
Chris@909 86 end
Chris@909 87
Chris@909 88 end