mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
made a better ratings page
This commit is contained in:
@@ -12,4 +12,8 @@ class Beer < ApplicationRecord
|
||||
def to_s
|
||||
"#{name}, #{(Brewery.find_by_id brewery_id).name}"
|
||||
end
|
||||
|
||||
def self.top n
|
||||
sorted_by_rating_in_desc_order = Beer.all.sort_by{ |b| b.average_rating }.reverse.take(n)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,4 +25,8 @@ class Brewery < ApplicationRecord
|
||||
self.year = 2023
|
||||
puts "changed year to 2023"
|
||||
end
|
||||
|
||||
def self.top n
|
||||
sorted_by_rating_in_desc_order = Brewery.all.sort_by{ |b| b.average_rating }.reverse.take(n)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ module RatingAverage
|
||||
|
||||
def average_rating
|
||||
if instance_of?(Beer)
|
||||
ratings = [self]
|
||||
ratings = self.ratings
|
||||
elsif instance_of?(Brewery)
|
||||
# creates empty arrays for beers that have no ratings
|
||||
# + wraps the array in an array
|
||||
@@ -14,7 +14,7 @@ module RatingAverage
|
||||
raise "Wrong class type for calculating average, #{self.class}"
|
||||
end
|
||||
|
||||
return 0 unless ratings.count > 0
|
||||
return 0 unless ratings && ratings.count > 0
|
||||
|
||||
ratings.map(&:score).sum / ratings.count.to_f
|
||||
end
|
||||
|
||||
@@ -6,6 +6,8 @@ class Rating < ApplicationRecord
|
||||
less_than_or_equal_to: 50,
|
||||
only_integer: true }
|
||||
|
||||
scope :recent, -> { Rating.order(created_at: :desc) }
|
||||
|
||||
def to_s
|
||||
"#{beer.name}: #{score}"
|
||||
end
|
||||
|
||||
@@ -66,4 +66,12 @@ class User < ApplicationRecord
|
||||
end
|
||||
highest[:name]
|
||||
end
|
||||
|
||||
def self.top n
|
||||
sorted_by_rating_in_desc_order = User.all.sort_by{ |b| b.average_rating }.reverse.take(n)
|
||||
end
|
||||
|
||||
def self.most_active n
|
||||
sorted_by_rating_count_in_desc_order = User.all.sort_by{ |b| b.count_ratings }.reverse.take(n)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user