Module: Avo::Fields::Concerns::ItemLabels

Extended by:
ActiveSupport::Concern
Included in:
TagsField::IndexComponent, TagsField::ShowComponent
Defined in:
app/components/avo/fields/concerns/item_labels.rb

Instance Method Summary collapse

Instance Method Details

#label_from_item(item) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'app/components/avo/fields/concerns/item_labels.rb', line 15

def label_from_item(item)
  return item[:label] if item.is_a?(Hash) && item[:label].present?

  value = value_for_item item

  if suggestions_are_a_hash?
    return suggestions_by_id[value.to_s][:label] if suggestions_by_id[value.to_s].present?
  end
  value
end

#suggestions_are_a_hash?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'app/components/avo/fields/concerns/item_labels.rb', line 34

def suggestions_are_a_hash?
  return false if @field.suggestions.blank?

  @field.suggestions.first.is_a? Hash
end

#suggestions_by_idObject



26
27
28
29
30
31
32
# File 'app/components/avo/fields/concerns/item_labels.rb', line 26

def suggestions_by_id
  return {} unless suggestions_are_a_hash?

  @field.suggestions.map do |suggestion|
    [suggestion[:value].to_s, suggestion]
  end.to_h
end

#value_for_item(item) ⇒ Object



7
8
9
10
11
12
13
# File 'app/components/avo/fields/concerns/item_labels.rb', line 7

def value_for_item(item)
  if @field.acts_as_taggable_on.present?
    item["value"]
  else
    item
  end
end