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