Skip to content

Commit

Permalink
Adding support for average rating
Browse files Browse the repository at this point in the history
  • Loading branch information
sohomdutta2772 committed Nov 16, 2024
1 parent 8c25008 commit 41d63af
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ class Item < ApplicationRecord
validates :name, presence: true
validates :description, presence: true
validates :price, presence: true, numericality: { greater_than: 0 }

def average_rating
ratings.average(:score).to_f.round(2)
end
end
1 change: 1 addition & 0 deletions app/views/item/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<p><strong>Price:</strong> <%= number_to_currency(item.price) %></p>
<p><strong>Seller:</strong> <%= item.user.username %></p>
<p><strong>Reviews:</strong> <%= item.ratings.count %></p>
<p><strong>Average Rating:</strong> <%= item.average_rating %> / 5</p>
</div>
</li>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions app/views/item/show_listing.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<p><strong>Description:</strong> <%= @item.description %></p>
<p><strong>Price:</strong> <%= number_to_currency(@item.price) %></p>
<p><strong>Seller:</strong> <%= @item.user.username %></p>
<p><strong>Average Rating:</strong> <%= @item.average_rating %> / 5</p>

<% if session[:user] %>
<%= form_with url: create_order_path(@item), local: true do |form| %>
Expand Down

0 comments on commit 41d63af

Please sign in to comment.