Class: Yattho::Beta::Counter

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/beta/counter.rb

Overview

Use ‘Counter` to add a count to navigational elements and buttons.

Constant Summary collapse

DEFAULT_SCHEME =
:default
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "",
  :primary => "Counter--primary",
  :secondary => "Counter--secondary",
  # deprecated
  :gray => "Counter--primary",
  :light_gray => "Counter--secondary"
}.freeze
DEPRECATED_SCHEME_OPTIONS =
[:gray, :light_gray].freeze
SCHEME_OPTIONS =
(SCHEME_MAPPINGS.keys - DEPRECATED_SCHEME_OPTIONS).freeze

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(count: 0, scheme: DEFAULT_SCHEME, limit: 5_000, hide_if_zero: false, text: "", round: false, **system_arguments) ⇒ Counter

Returns a new instance of Counter.

Examples:

Default

<%= render(Yattho::Beta::Counter.new(count: 25)) %>

Schemes

<%= render(Yattho::Beta::Counter.new(count: 25, scheme: :primary)) %>
<%= render(Yattho::Beta::Counter.new(count: 25, scheme: :secondary)) %>

Parameters:

  • count (Integer, Float::INFINITY, nil) (defaults to: 0)

    The number to be displayed (e.x. # of issues, pull requests)

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    Color scheme. <%= one_of(Yattho::Beta::Counter::SCHEME_OPTIONS) %>

  • limit (Integer, nil) (defaults to: 5_000)

    Maximum value to display. Pass ‘nil` for no limit. (e.x. if `count` == 6,000 and `limit` == 5000, counter will display “5,000+”)

  • hide_if_zero (Boolean) (defaults to: false)

    If true, a ‘hidden` attribute is added to the counter if `count` is zero.

  • text (String) (defaults to: "")

    Text to display instead of count.

  • round (Boolean) (defaults to: false)

    Whether to apply our standard rounding logic to value.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/components/yattho/beta/counter.rb', line 41

def initialize(
  count: 0,
  scheme: DEFAULT_SCHEME,
  limit: 5_000,
  hide_if_zero: false,
  text: "",
  round: false,
  **system_arguments
)
  @count = count
  @limit = limit
  @hide_if_zero = hide_if_zero
  @text = text
  @round = round
  @system_arguments = deny_tag_argument(**system_arguments)

  @has_limit = !@limit.nil?
  @system_arguments[:title] = title
  @system_arguments[:tag] = :span
  @system_arguments[:classes] = class_names(
    "Counter",
    @system_arguments[:classes],
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_OPTIONS, scheme, DEFAULT_SCHEME,
                                      deprecated_values: DEPRECATED_SCHEME_OPTIONS)]
  )
  @system_arguments[:hidden] = true if count == 0 && hide_if_zero # rubocop:disable Style/NumericPredicate
end

Instance Method Details

#callObject



69
70
71
# File 'app/components/yattho/beta/counter.rb', line 69

def call
  render(Yattho::BaseComponent.new(**@system_arguments)) { value }
end