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 def to_s
"#{name}, #{(Brewery.find_by_id brewery_id).name}" "#{name}, #{(Brewery.find_by_id brewery_id).name}"
end end
def self.top n
sorted_by_rating_in_desc_order = Beer.all.sort_by{ |b| b.average_rating }.reverse.take(n)
end
end end
+4
View File
@@ -25,4 +25,8 @@ class Brewery < ApplicationRecord
self.year = 2023 self.year = 2023
puts "changed year to 2023" puts "changed year to 2023"
end end
def self.top n
sorted_by_rating_in_desc_order = Brewery.all.sort_by{ |b| b.average_rating }.reverse.take(n)
end
end end
+2 -2
View File
@@ -3,7 +3,7 @@ module RatingAverage
def average_rating def average_rating
if instance_of?(Beer) if instance_of?(Beer)
ratings = [self] ratings = self.ratings
elsif instance_of?(Brewery) elsif instance_of?(Brewery)
# creates empty arrays for beers that have no ratings # creates empty arrays for beers that have no ratings
# + wraps the array in an array # + wraps the array in an array
@@ -14,7 +14,7 @@ module RatingAverage
raise "Wrong class type for calculating average, #{self.class}" raise "Wrong class type for calculating average, #{self.class}"
end end
return 0 unless ratings.count > 0 return 0 unless ratings && ratings.count > 0
ratings.map(&:score).sum / ratings.count.to_f ratings.map(&:score).sum / ratings.count.to_f
end end
+2
View File
@@ -6,6 +6,8 @@ class Rating < ApplicationRecord
less_than_or_equal_to: 50, less_than_or_equal_to: 50,
only_integer: true } only_integer: true }
scope :recent, -> { Rating.order(created_at: :desc) }
def to_s def to_s
"#{beer.name}: #{score}" "#{beer.name}: #{score}"
end end
+8
View File
@@ -66,4 +66,12 @@ class User < ApplicationRecord
end end
highest[:name] highest[:name]
end 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 end
+57 -3
View File
@@ -1,15 +1,69 @@
<h2>List of ratings</h2> <h2>List of ratings</h2>
<p>There is <%= pluralize @ratings.count, "rating" %></p> <p>There is <%= pluralize @ratings.count, "rating" %></p>
<ul>
<% @ratings.each do |rating| %> <div class="container text-center">
<div class="row">
<div class="col">
<h2>Best beer</h2>
<div class="row">
<table class="col">
<tr>
<th><strong>Beer</strong></th>
<th><strong>Rating</strong></th>
</tr>
<% Beer.top(3).each do |beer| %>
<tr>
<td><%= beer.name %></td>
<td><%= beer.average_rating %></td>
</tr>
<% end %>
</table>
</div>
</div>
<div class="col">
<h2>Best brewery</h2>
<div class="row">
<table class="col">
<tr>
<th><strong>Brewery</strong></th>
<th><strong>Rating</strong></th>
</tr>
<% Brewery.top(3).each do |brewery| %>
<tr>
<td><%= brewery.name %></td>
<td><%= brewery.average_rating %></td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
</div>
<h2>Most active users</h2>
<ol>
<% User.most_active(3).each do |user| %>
<li> <li>
<%= render rating %> <%= user.username %>, <%= pluralize user.ratings.count, "rating" %>
</li> </li>
<% end %> <% end %>
</ol>
<h2>Recent ratings</h2>
<ul>
<% Rating.recent.each do |rating| %>
<li>
<%= rating.beer.name %>, <%= rating.beer.count_ratings %>, <%= rating.created_at %>
</li>
<% end%>
</ul> </ul>
<p> <p>
<%= link_to "New rating", new_rating_path, class:"btn btn-primary" %> <%= link_to "New rating", new_rating_path, class:"btn btn-primary" %>
</p> </p>