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.53.0 lib/avo/fields/gravatar_field.rb
avo-2.52.0 lib/avo/fields/gravatar_field.rb
avo-2.49.0 lib/avo/fields/gravatar_field.rb
avo-2.48.0 lib/avo/fields/gravatar_field.rb
avo-2.47.0 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta8 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta6 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta7 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta5 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta2 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta4 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta3 lib/avo/fields/gravatar_field.rb
avo-3.0.0.beta1 lib/avo/fields/gravatar_field.rb
avo-3.0.1.beta1 lib/avo/fields/gravatar_field.rb
avo-3.0.0.pre17 lib/avo/fields/gravatar_field.rb
avo-3.0.0.pre18 lib/avo/fields/gravatar_field.rb
avo-3.0.0.pre14 lib/avo/fields/gravatar_field.rb
avo-3.0.0.pre16 lib/avo/fields/gravatar_field.rb
avo-3.0.0.pre15 lib/avo/fields/gravatar_field.rb
avo-3.0.0.pre13 lib/avo/fields/gravatar_field.rb