Sha256: e15c6ac6eeb1ee51aa5bd56f4d8873d20f3802cb644e8554f9a66ae0451ccd76

Contents?: true

Size: 1.05 KB

Versions: 212

Compression:

Stored size: 1.05 KB

Contents

require "digest"
require "erb"

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

      def initialize(id, **args, &block)
        args[:name] ||= "Avatar"

        super(id, **args, &block)

        hide_on [:edit, :new]

        @link_to_resource = args[:link_to_resource].present? ? args[:link_to_resource] : false
        @rounded = args[:rounded].nil? ? true : args[:rounded]
        @size = args[:size].present? ? args[:size].to_i : 32
        @default = args[:default].present? ? ERB::Util.url_encode(args[:default]).to_s : ""
      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

212 entries across 212 versions & 1 rubygems

Version Path
avo-2.33.3 lib/avo/fields/gravatar_field.rb
avo-2.33.3.pre.2 lib/avo/fields/gravatar_field.rb
avo-2.33.3.pre.1 lib/avo/fields/gravatar_field.rb
avo-2.33.2 lib/avo/fields/gravatar_field.rb
avo-2.33.1 lib/avo/fields/gravatar_field.rb
avo-2.33.0 lib/avo/fields/gravatar_field.rb
avo-2.32.6 lib/avo/fields/gravatar_field.rb
avo-2.32.5 lib/avo/fields/gravatar_field.rb
avo-2.32.4 lib/avo/fields/gravatar_field.rb
avo-2.32.3 lib/avo/fields/gravatar_field.rb
avo-2.32.2 lib/avo/fields/gravatar_field.rb
avo-2.32.1 lib/avo/fields/gravatar_field.rb
avo-2.32.0 lib/avo/fields/gravatar_field.rb
avo-2.31.0 lib/avo/fields/gravatar_field.rb
avo-2.30.2 lib/avo/fields/gravatar_field.rb
avo-2.30.1 lib/avo/fields/gravatar_field.rb
avo-2.30.1.pre4.pr1683 lib/avo/fields/gravatar_field.rb
avo-2.30.1.pre3.pr1683 lib/avo/fields/gravatar_field.rb
avo-2.30.1.pre2.pr1683 lib/avo/fields/gravatar_field.rb
avo-2.30.1.pre1.pr1683 lib/avo/fields/gravatar_field.rb