Class: Avo::Fields::Common::BadgeViewerComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/avo/fields/common/badge_viewer_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(value:, options:) ⇒ BadgeViewerComponent

Returns a new instance of BadgeViewerComponent.



4
5
6
7
8
9
10
11
12
13
14
# File 'app/components/avo/fields/common/badge_viewer_component.rb', line 4

def initialize(value:, options:)
  @value = value
  @options = options
  @backgrounds = {
    info: "bg-blue-500",
    success: "bg-green-500",
    danger: "bg-red-500",
    warning: "bg-yellow-500",
    neutral: "bg-gray-500"
  }
end

Instance Method Details

#classesObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/components/avo/fields/common/badge_viewer_component.rb', line 16

def classes
  background = :info

  @options.invert.each do |values, type|
    if [values].flatten.map { |value| value.to_s }.include? @value.to_s
      background = type.to_sym
      next
    end
  end

  classes = "whitespace-nowrap rounded-md uppercase px-2 py-1 text-xs font-bold block text-center truncate "

  classes += "#{@backgrounds[background]} text-white" if @backgrounds[background].present?

  classes
end