Class: Super::Badge

Inherits:
Object
  • Object
show all
Defined in:
lib/super/badge.rb

Constant Summary collapse

STYLES =
{
  light: "bg-gray-100 text-black",
  dark: "bg-gray-900 text-white",
  red: "bg-red-700 text-white",
  yellow: "bg-yellow-400 text-black",
  green: "bg-green-700 text-white",
  blue: "bg-blue-700 text-white",
  purple: "bg-purple-800 text-white",
}

Instance Method Summary collapse

Constructor Details

#initialize(text, styles: nil) ⇒ Badge

Returns a new instance of Badge.



15
16
17
18
# File 'lib/super/badge.rb', line 15

def initialize(text, styles: nil)
  @text = text
  @requested_styles = Array(styles.presence).flatten
end

Instance Method Details

#stylesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/super/badge.rb', line 20

def styles
  return @styles if instance_variable_defined?(:@styles)
  @styles =
    if requested_styles.delete(:reset)
      []
    else
      COMMON_STYLES
    end

  if requested_styles.empty?
    @styles += [STYLES[:light]]
  else
    requested_styles.each do |style|
      @styles +=
        if STYLES.key?(style)
          [STYLES[style]]
        else
          [style]
        end
    end
  end

  @styles
end

#to_sObject



45
46
47
48
49
50
51
# File 'lib/super/badge.rb', line 45

def to_s
  ActionController::Base.helpers.(
    :span,
    @text,
    class: styles.join(" ")
  )
end