Class: Www::ReviewsIo::RatingBarComponent

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

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

value: average stars 0..5
count: review count
link: optional URL for reviews page
size_px: optional font-size in px for stars (default 10)
rounding: :floor or :nearest (half-star) for star icons



13
14
15
16
17
18
19
20
# File 'app/components/www/reviews_io/rating_bar_component.rb', line 13

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

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/www/reviews_io/rating_bar_component.rb', line 22

def call
  top_row = tag.div(class: 'd-flex align-items-center gap-1 mb-0') do
    rating_number = tag.span(sprintf('%.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-muted', 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