Sha256: cc1b40b95d60f26fa3d8117cdc49eb80134e32d0809ada5f343fcfba3ce27d7c

Contents?: true

Size: 731 Bytes

Versions: 6

Compression:

Stored size: 731 Bytes

Contents

# frozen_string_literal: true

module SolidusReviews
  module Spree
    module ProductDecorator
      def self.prepended(base)
        base.class_eval do
          has_many :reviews
        end
      end

      def stars
        avg_rating.try(:round) || 0
      end

      def recalculate_rating
        reviews_count = reviews.reload.default_approval_filter.count

        self.reviews_count = reviews_count
        self.avg_rating = if reviews_count > 0
                            '%.1f' % (reviews.default_approval_filter.sum(:rating).to_f / reviews_count)
                          else
                            0
                          end
        save
      end

      ::Spree::Product.prepend self
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_reviews-1.7.0 app/decorators/models/solidus_reviews/spree/product_decorator.rb
solidus_reviews-1.6.0 app/decorators/models/solidus_reviews/spree/product_decorator.rb
solidus_reviews-1.5.0 app/decorators/models/solidus_reviews/spree/product_decorator.rb
solidus_reviews-1.4.1 app/decorators/models/solidus_reviews/spree/product_decorator.rb
solidus_reviews-1.4.0 app/decorators/models/solidus_reviews/spree/product_decorator.rb
solidus_reviews-1.3.0 app/decorators/models/solidus_reviews/spree/product_decorator.rb