Class: Www::ReviewsIo::RatingBarComponent
- Inherits:
-
ApplicationComponent
- Object
- ViewComponent::Base
- ApplicationComponent
- Www::ReviewsIo::RatingBarComponent
- 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
-
#call ⇒ String
Renders the rating bar badge.
-
#initialize(value:, count:, link: nil, size_px: 11, rounding: :nearest) ⇒ RatingBarComponent
constructor
Initializes the rating bar component.
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.
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
#call ⇒ String
Renders the rating bar badge.
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 = 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([, 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 |