Module: SolidusReviews::Spree::ProductDecorator

Defined in:
app/decorators/models/solidus_reviews/spree/product_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
10
# File 'app/decorators/models/solidus_reviews/spree/product_decorator.rb', line 6

def self.prepended(base)
  base.class_eval do
    has_many :reviews
  end
end

Instance Method Details

#recalculate_ratingObject



16
17
18
19
20
21
22
23
24
25
26
# File 'app/decorators/models/solidus_reviews/spree/product_decorator.rb', line 16

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

#starsObject



12
13
14
# File 'app/decorators/models/solidus_reviews/spree/product_decorator.rb', line 12

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