Sha256: 89a3ad5a537a00b2b2ef184ceef7f1d90fb4bd446941d28de3eae74adcf19180

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Gravatarify::Helper
  # so that it's possible to access that build_gravatar_url method
  include Gravatarify::Base
  
  # to simplify things a bit and have a neat-o naming
  alias_method :gravatar_url, :build_gravatar_url
  
  # Helper method for HAML to return a neat hash to be used as attributes in an image tag.
  #
  # Now it's as simple as doing something like:
  #
  #    %img{ gravatar_attrs(@user.mail, :size => 20) }/
  #
  def gravatar_attrs(email, options = {})
    url_options = options.reject { |key, value| !Gravatarify::GRAVATAR_OPTIONS.include?(key) }
    options = options.delete_if { |key, value| Gravatarify::GRAVATAR_OPTIONS.include?(key) }
    options[:alt] ||= Gravatarify::Base.get_smart_email_from(email) # use email as :alt attribute
    options[:width] = options[:height] = (url_options[:size] || Gravatarify::GRAVATAR_DEFAULT_SIZE) # customize size
    options[:src] = email.respond_to?(:gravatar_url) ? email.gravatar_url(url_options) : build_gravatar_url(email, url_options)
    options
  end
  
  # Takes care of creating an <tt><img/></tt>-tag based on a gravatar url, it no longer
  # makes use of any Rails helper, so is totally useable in any other library.
  def gravatar_tag(email, options = {})
    html_attrs = gravatar_attrs(email, options).map do |key,value|
      escaped = defined?(Rack::Utils) ? Rack::Utils.escape_html(value) : CGI.escapeHTML(value)
      "#{key}=\"#{escaped}\""
    end.sort.join(" ")
    "<img #{html_attrs} />"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gravatarify-1.2.0 lib/gravatarify/helper.rb