Sha256: 817c5a8b84f76c08134b0d1a5128fd3eb98dd6ee17652895e48a40e85706921f

Contents?: true

Size: 993 Bytes

Versions: 39

Compression:

Stored size: 993 Bytes

Contents

module Avo
  module Fields
    module Concerns
      module ItemLabels
        extend ActiveSupport::Concern

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

        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

        def suggestions_by_id
          return {} unless suggestions_are_a_hash?

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

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

          @field.suggestions.first.is_a? Hash
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
avo-3.15.1 app/components/avo/fields/concerns/item_labels.rb
avo-3.15.0 app/components/avo/fields/concerns/item_labels.rb
avo-3.14.5 app/components/avo/fields/concerns/item_labels.rb
avo-3.14.4 app/components/avo/fields/concerns/item_labels.rb
avo-3.14.3 app/components/avo/fields/concerns/item_labels.rb
avo-3.14.2 app/components/avo/fields/concerns/item_labels.rb
avo-3.14.1 app/components/avo/fields/concerns/item_labels.rb
avo-3.14.0 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.7 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.6 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.5 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.4 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.3 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.2 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.1 app/components/avo/fields/concerns/item_labels.rb
avo-3.13.0 app/components/avo/fields/concerns/item_labels.rb
avo-3.12.0 app/components/avo/fields/concerns/item_labels.rb
avo-3.11.10 app/components/avo/fields/concerns/item_labels.rb
avo-3.11.9 app/components/avo/fields/concerns/item_labels.rb
avo-3.11.8 app/components/avo/fields/concerns/item_labels.rb