Class: Railsboot::BadgeComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/railsboot/badge_component.rb

Overview

ViewComponent: renders the badge block.

Instance Method Summary collapse

Constructor Details

#initialize(pill: false, color: DEFAULT_COLOR, **html_attributes) ⇒ BadgeComponent

Returns a new instance of BadgeComponent.

Parameters:

  • pill (Boolean) (defaults to: false)

    whether to render the badge as a rounded pill

  • color (String) (defaults to: DEFAULT_COLOR)

    Bootstrap color variant (one of COLORS)

  • html_attributes (Hash)

    additional HTML attributes applied to the rendered element

Options Hash (**html_attributes):

  • :class (String)

    additional CSS classes, merged with the component's own classes

  • :tag (Symbol)

    HTML tag to render (defaults to :span)



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/components/railsboot/badge_component.rb', line 9

def initialize(pill: false, color: DEFAULT_COLOR, **html_attributes)
  @pill = pill
  @color = fetch_or_raise(color, COLORS)
  @html_attributes = html_attributes

  @html_attributes[:tag] = :span
  @html_attributes[:class] = class_names(
    "badge",
    "bg-#{@color}",
    { "rounded-pill" => @pill },
    html_attributes.delete(:class)
  )
end

Instance Method Details

#callString

Renders the badge element wrapping the component content.

Returns:

  • (String)

    HTML-safe badge markup



25
26
27
28
29
# File 'app/components/railsboot/badge_component.rb', line 25

def call
  render Railsboot::BaseComponent.new(**@html_attributes) do
    content
  end
end