Class: PhlexKit::Stars

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/stars/stars.rb

Overview

Star rating (CUSTOM — ruby_ui has no rating component). Replaces the stars view helper: renders rating filled stars + the remainder empty, out of max. Presentational; attrs pass through via mix. Self-colours (amber) so it works anywhere, not just inside a review.

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(rating:, max: 5, **attrs) ⇒ Stars

Returns a new instance of Stars.



7
8
9
10
11
# File 'app/components/phlex_kit/stars/stars.rb', line 7

def initialize(rating:, max: 5, **attrs)
  @rating = rating.to_i.clamp(0, max)
  @max = max
  @attrs = attrs
end

Instance Method Details

#view_templateObject



13
14
15
16
17
# File 'app/components/phlex_kit/stars/stars.rb', line 13

def view_template
  span(**mix({ class: "pk-stars", title: "#{@rating}/#{@max}" }, @attrs)) do
    plain(("" * @rating) + ("" * (@max - @rating)))
  end
end