Module: Jav::Fields::Concerns::ItemLabels

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

Instance Method Summary collapse

Instance Method Details

#label_from_item(item) ⇒ Object



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

def label_from_item(item)
  value = value_for_item item

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

  value
end

#suggestions_are_a_hash?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'app/components/jav/fields/concerns/item_labels.rb', line 31

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

  @field.suggestions.first.is_a? Hash
end

#suggestions_by_idObject



23
24
25
26
27
28
29
# File 'app/components/jav/fields/concerns/item_labels.rb', line 23

def suggestions_by_id
  return {} unless suggestions_are_a_hash?

  @field.suggestions.index_by do |suggestion|
    suggestion[:value].to_s
  end
end

#value_for_item(item) ⇒ Object



7
8
9
10
11
12
13
# File 'app/components/jav/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