Class: IronAdmin::Dashboards::MetricCardComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/iron_admin/dashboards/metric_card_component.rb

Overview

Renders a metric card with name and formatted value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value:, format: :number, icon: nil, live: false) ⇒ MetricCardComponent

Returns a new instance of MetricCardComponent.

Parameters:

  • name (String, Symbol)

    Metric name

  • value (Numeric)

    Metric value

  • format (Symbol) (defaults to: :number)

    Format (:number, :currency, :percentage)

  • icon (String, nil) (defaults to: nil)

    Optional Heroicon name (e.g., "users", "currency-dollar")

  • live (Boolean) (defaults to: false)

    Whether to render a stable Turbo Stream target id



12
13
14
15
16
17
18
# File 'app/components/iron_admin/dashboards/metric_card_component.rb', line 12

def initialize(name:, value:, format: :number, icon: nil, live: false)
  @name = name
  @value = value
  @format = format
  @icon = icon
  @live = live
end

Instance Attribute Details

#iconString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Heroicon name.

Returns:

  • (String, nil)

    Heroicon name



50
51
52
# File 'app/components/iron_admin/dashboards/metric_card_component.rb', line 50

def icon
  @icon
end

Instance Method Details

#formatted_valueString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Value formatted according to format option.

Returns:

  • (String)

    Value formatted according to format option



22
23
24
25
26
27
28
# File 'app/components/iron_admin/dashboards/metric_card_component.rb', line 22

def formatted_value
  case @format
  when :currency then helpers.number_to_currency(@value)
  when :percentage then helpers.number_to_percentage(@value, precision: 1)
  else helpers.number_with_delimiter(@value)
  end
end

#labelString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Humanized metric label.

Returns:

  • (String)

    Humanized metric label



32
33
34
# File 'app/components/iron_admin/dashboards/metric_card_component.rb', line 32

def label
  @name.to_s.humanize
end

#live?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns True when the metric should expose a live target.

Returns:

  • (Boolean)

    True when the metric should expose a live target



38
39
40
# File 'app/components/iron_admin/dashboards/metric_card_component.rb', line 38

def live?
  @live
end

#live_target_idString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Stable Turbo Stream target id.

Returns:

  • (String)

    Stable Turbo Stream target id



44
45
46
# File 'app/components/iron_admin/dashboards/metric_card_component.rb', line 44

def live_target_id
  "metric_#{@name.to_s.parameterize(separator: "_")}"
end