Sha256: 0ec22f61c6dc1d8f078bbc4b6c59c0067b3cf9053ba717cfc825cfe6781f649b

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'digest'
require 'erb'

module Avo
  module Fields
    class GravatarField < BaseField
      attr_reader :size
      attr_reader :rounded
      attr_reader :default

      def initialize(name, **args, &block)
        @defaults = {
          partial_name: 'gravatar-field',
          name: 'Avatar',
          id: args[:id].present? ? args[:id] : 'email',
        }

        super(name, **args, &block)

        hide_on [:edit, :new]

        @rounded = args[:rounded].present? ? args[:rounded] : true
        @size = args[:size].present? ? args[:size].to_i : 40
        @default = args[:default].present? ? ERB::Util.url_encode(args[:default]).to_s : ''
        @link_to_resource = args[:link_to_resource].present? ? args[:link_to_resource] : false
      end

      def md5
        return if value.blank?

        Digest::MD5.hexdigest(value.strip.downcase)
      end

      def to_image
        options = {
          default: '',
          size: 340,
        }

        query = options.map { |key, value| "#{key}=#{value}" }.join('&')

        URI::HTTPS.build(host: "www.gravatar.com", path: "/avatar/#{md5}", query: query).to_s
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
avo-0.5.0.beta9 lib/avo/fields/gravatar_field.rb
avo-0.5.0.beta8 lib/avo/fields/gravatar_field.rb