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
|
||||
|
||||
@@ -1,15 +1,69 @@
|
||||
<h2>List of ratings</h2>
|
||||
<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>
|
||||
<%= render rating %>
|
||||
<%= user.username %>, <%= pluralize user.ratings.count, "rating" %>
|
||||
</li>
|
||||
<% 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>
|
||||
|
||||
|
||||
<p>
|
||||
<%= link_to "New rating", new_rating_path, class:"btn btn-primary" %>
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user