Class: Www::ReviewsIo::RatingBarComponent

Inherits:
ApplicationComponent show all
Includes:
Concerns::ReviewsIoSupport
Defined in:
app/components/www/reviews_io/rating_bar_component.rb

Overview

ViewComponent: renders the rating bar block showing the average
Reviews.io star rating and review count, optionally linked to the
reviews page.

Instance Method Summary collapse

Methods inherited from ApplicationComponent

#cms_link, #fetch_or_fallback, #image_asset_tag, #image_tag, #number_to_currency, #number_with_delimiter, #post_path, #post_url, #strip_tags

Constructor Details

#initialize(value:, count:, link: nil, size_px: 11, rounding: :nearest) ⇒ RatingBarComponent

Initializes the rating bar component.

Parameters:

  • value (Numeric)

    average stars, 0..5 (clamped to that range)

  • count (Integer)

    review count shown below the stars

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

    optional URL for the reviews page; when
    present the whole badge is wrapped in a link

  • size_px (Integer) (defaults to: 11)

    font-size in px for the stars (capped at 16)

  • rounding (Symbol) (defaults to: :nearest)

    :floor or :nearest (half-star) for star
    icons



24
25
26
27
28
29
30
31
# File 'app/components/www/reviews_io/rating_bar_component.rb', line 24

def initialize(value:, count:, link: nil, size_px: 11, rounding: :nearest)
  super()
  @value = value.to_f.clamp(0.0, 5.0)
  @count = count.to_i
  @link = link
  @size_px = size_px
  @rounding = rounding
end

Instance Method Details

#callString

Renders the rating bar badge.

Returns:

  • (String)

    HTML for the badge, wrapped in a link when
    link was given



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/components/www/reviews_io/rating_bar_component.rb', line 37

def call
  top_row = tag.div(class: 'd-flex align-items-center gap-1 mb-0') do
    rating_number = tag.span(format('%.2f', @value), class: 'fw-bold', style: 'font-size: 1.25rem; line-height: 1.2;')
    stars = tag.span(class: 'rating-stars', style: "font-size:#{[@size_px, 16].min}px; letter-spacing: -0.1em;") do
      star_html(@value, icon_class: 'text-green', rounding: @rounding)
    end
    safe_join([rating_number, stars])
  end

  review_text = "Based on #{@count} #{@count == 1 ? 'review' : 'reviews'}"
  bottom_row = tag.span(review_text, class: 'text-body-secondary', style: 'font-size: 0.75rem; line-height: 1.2;')

  badge_content = tag.div(class: 'd-flex flex-column align-items-start') do
    safe_join([top_row, bottom_row])
  end

  if @link.present?
    helpers.link_to(@link, class: 'text-decoration-none text-reset') { badge_content }
  else
    badge_content
  end
end