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, style: nil, styles: nil) ⇒ Badge

Returns a new instance of Badge.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/super/badge.rb', line 15

def initialize(text, style: nil, styles: nil)
  @text = text
  if styles.present?
    Super::Useful::Deprecation["0.22"].deprecation_warning("styles:", "use `style:` with a single style")
  end
  @requested_styles = Array(styles.presence).flatten + Array(style.presence).flatten
  if @requested_styles.any? { |s| s.is_a?(String) }
    Super::Useful::Deprecation["0.22"].warn("Super::Badge.new(text, style:) accepts exactly one Symbol style from this list #{STYLES.keys.inspect}")
  end
  if @requested_styles.size != 1
    Super::Useful::Deprecation["0.22"].warn("Super::Badge.new(text, style:) accepts exactly one style, but it received #{@requested_styles.size}")
  end
end

Instance Method Details

#stylesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/super/badge.rb', line 29

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



54
55
56
57
58
59
60
# File 'lib/super/badge.rb', line 54

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