Class: Www::ReviewsIo::ProductRatingComponent

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

Overview

Displays product rating from Reviews.io data stored in the database.
This replaces the old API-based rating lookup with a database query.

Usage:
<%= render Www::ReviewsIo::ProductRatingComponent.new(skus: ['SKU-001']) %>
<%= render Www::ReviewsIo::ProductRatingComponent.new(skus: ['SKU-001'], link: '#reviews') %>
<%= render Www::ReviewsIo::ProductRatingComponent.new(skus: ['SKU-001', 'SKU-002']) %>
<%= render Www::ReviewsIo::ProductRatingComponent.new(product_line: 'towel-warmer-classic-infinity') %>

Expansion options:

  • include_variants: true - expand each SKU to include all item variants (default: true)
  • include_pl_siblings: true - expand to include all items in same product line (default: false)

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(skus: nil, product_line: nil, include_variants: true, include_pl_siblings: false, link: nil, size_px: 12, rounding: :floor, min_rating: 3) ⇒ ProductRatingComponent

Builds a rating display from Reviews.io data for the given SKUs or product line.

Parameters:

  • skus (Array<String>, String, nil) (defaults to: nil)

    SKU(s) to look up ratings for; nil when using product_line

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

    product line slug; expands to all items in that line

  • include_variants (Boolean) (defaults to: true)

    expand each SKU to include all item variants

  • include_pl_siblings (Boolean) (defaults to: false)

    expand to include all items in the same product line

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

    optional URL the rating bar links to

  • size_px (Integer) (defaults to: 12)

    star size in pixels

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

    rounding mode passed to the rating bar (e.g. :floor)

  • min_rating (Numeric) (defaults to: 3)

    minimum average rating required to render



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/www/reviews_io/product_rating_component.rb', line 32

def initialize(
  skus: nil,
  product_line: nil,
  include_variants: true,
  include_pl_siblings: false,
  link: nil,
  size_px: 12,
  rounding: :floor,
  min_rating: 3
)
  super()
  @skus = Array(skus).flatten.compact_blank if skus.present?
  @product_line = product_line
  @include_variants = include_variants
  @include_pl_siblings = include_pl_siblings
  @link = link
  @size_px = size_px
  @rounding = rounding
  @min_rating = min_rating
end

Instance Method Details

#callString

Renders the rating bar with the aggregated Reviews.io data.

Returns:

  • (String)

    rendered HTML for the rating bar

See Also:



64
65
66
67
68
69
70
71
72
# File 'app/components/www/reviews_io/product_rating_component.rb', line 64

def call
  render Www::ReviewsIo::RatingBarComponent.new(
    value: average_rating.to_f,
    count: total_reviews,
    link: @link,
    size_px: @size_px,
    rounding: @rounding
  )
end

#render?Boolean

Whether the component should render.

Returns:

  • (Boolean)

    true when SKUs resolved and the average rating and review count pass thresholds



56
57
58
# File 'app/components/www/reviews_io/product_rating_component.rb', line 56

def render?
  resolved_skus.any? && average_rating.to_f > @min_rating && total_reviews.positive?
end