mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
32 lines
794 B
Ruby
32 lines
794 B
Ruby
module RatingAverage
|
|
extend ActiveSupport::Concern
|
|
|
|
def average_rating
|
|
if instance_of?(Beer)
|
|
ratings = [self]
|
|
elsif instance_of?(Brewery)
|
|
# creates empty arrays for beers that have no ratings
|
|
# + wraps the array in an array
|
|
ratings = beers.map(&:b.ratings).reject(&:blank?)[0]
|
|
elsif instance_of?(User)
|
|
ratings = self.ratings
|
|
else
|
|
raise "Wrong class type for calculating average, #{self.class}"
|
|
end
|
|
|
|
return 0 unless ratings.count > 0
|
|
|
|
ratings.map(&:r.score).sum / ratings.count.to_f
|
|
end
|
|
|
|
def count_ratings
|
|
if instance_of?(Beer) || User
|
|
ratings.count
|
|
elsif instance_of?(Brewery)
|
|
beers.map(&:ratings.count).sum
|
|
else
|
|
raise "Wrong class type for counting ratings, #{self.class}"
|
|
end
|
|
end
|
|
end
|