made a better ratings page

This commit is contained in:
Aaro Koivukangas
2023-12-18 08:04:09 +02:00
parent 1e5057accf
commit ff38f272dc
6 changed files with 77 additions and 5 deletions
+4
View File
@@ -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
+4
View File
@@ -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
+2 -2
View File
@@ -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
+2
View File
@@ -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
+8
View File
@@ -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