Class: Jav::Fields::GravatarField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/jav/fields/gravatar_field.rb

Defined Under Namespace

Classes: IndexComponent, ShowComponent

Instance Attribute Summary collapse

Attributes inherited from BaseField

#action, #as_avatar, #as_description, #as_label, #autocomplete, #block, #computable, #computed, #computed_value, #format_using, #help, #id, #in_sidebar, #index_text_align, #model, #null_values, #nullable, #panel_name, #readonly, #required, #resource, #sortable, #stacked, #user, #view, #visible

Attributes included from Concerns::IsDisabled

#disabled

Attributes included from Concerns::HasHTMLAttributes

#html

Attributes included from FieldExtensions::VisibleInDifferentViews

#show_on_edit, #show_on_index, #show_on_new, #show_on_show

Instance Method Summary collapse

Methods inherited from BaseField

#assign_value, #component_for_view, #custom?, #custom_name?, #database_id, #default_name, #fill_field, #has_own_panel?, #hidden_in_reflection?, #hydrate, #model_errors, #name, #placeholder, #plural_name, #resolve_attribute, #to_permitted_param, #translation_key, #type, #updatable, #update_using, #value, #view_component_name, #visible?, #visible_in_reflection?

Methods included from FieldExtensions::HasFieldName

#field_name, #field_name=

Methods included from Concerns::HasDefault

#computed_default_value

Methods included from Concerns::IsDisabled

#is_disabled?

Methods included from Concerns::IsReadonly

#is_readonly?

Methods included from Concerns::IsRequired

#is_required?

Methods included from Concerns::HasHTMLAttributes

#get_html

Methods included from FieldExtensions::VisibleInDifferentViews

#except_on, #hide_on, #only_on, #show_on, #show_on_create, #show_on_update, #visible_on?

Methods included from Concerns::IsResourceItem

#is_field?, #is_main_panel?, #is_panel?, #is_row?, #is_sidebar?, #is_tab?, #is_tab_group?, #is_tool?

Constructor Details

#initialize(id, **args, &block) ⇒ GravatarField

Returns a new instance of GravatarField.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jav/fields/gravatar_field.rb', line 9

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

  super

  hide_on %i[edit new]

  @link_to_resource = args[:link_to_resource].presence || 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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/jav/fields/gravatar_field.rb', line 7

def default
  @default
end

Returns the value of attribute link_to_resource.



7
8
9
# File 'lib/jav/fields/gravatar_field.rb', line 7

def link_to_resource
  @link_to_resource
end

#roundedObject (readonly)

Returns the value of attribute rounded.



7
8
9
# File 'lib/jav/fields/gravatar_field.rb', line 7

def rounded
  @rounded
end

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/jav/fields/gravatar_field.rb', line 7

def size
  @size
end

Instance Method Details

#md5Object



22
23
24
25
26
# File 'lib/jav/fields/gravatar_field.rb', line 22

def md5
  return if value.blank?

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

#to_imageObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/jav/fields/gravatar_field.rb', line 28

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