mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
upto 3.5
This commit is contained in:
@@ -1,2 +1,7 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
helper_method :current_user
|
||||
def current_user
|
||||
return nil if session[:user_id].nil?
|
||||
User.find session[:user_id]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@ class RatingsController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
Rating.create params.require(:rating).permit(:score, :beer_id)
|
||||
rating = Rating.create params.require(:rating).permit(:score, :beer_id)
|
||||
session[:last_rating] = "#{rating.beer.name} #{rating.score} points"
|
||||
|
||||
redirect_to ratings_path
|
||||
|
||||
@@ -10,6 +10,6 @@ class SessionsController < ApplicationController
|
||||
|
||||
def destroy
|
||||
session[:user_id] = nil
|
||||
redirect_to :root
|
||||
redirect_to "/"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,7 @@ class UsersController < ApplicationController
|
||||
|
||||
# GET /users/1 or /users/1.json
|
||||
def show
|
||||
@user = User.find current_user.id
|
||||
end
|
||||
|
||||
# GET /users/new
|
||||
|
||||
@@ -2,38 +2,29 @@ module RatingAverage
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def average_rating
|
||||
total_score = 0
|
||||
count = 0
|
||||
|
||||
beers = if defined? self.beers
|
||||
self.beers
|
||||
else
|
||||
self
|
||||
end
|
||||
|
||||
beers.each do |beer|
|
||||
count += beer.ratings.count
|
||||
beer.ratings.each do |rating|
|
||||
total_score += rating.score
|
||||
end
|
||||
if self.class == Beer
|
||||
ratings = [self]
|
||||
elsif self.class == Brewery
|
||||
# creates empty arrays for beers that have no ratings
|
||||
# + wraps the array in an array
|
||||
ratings = beers.map { |b| b.ratings }.reject(&:blank?)[0]
|
||||
elsif self.class == User
|
||||
ratings = self.ratings
|
||||
else
|
||||
raise "Wrong class type for calculating average, #{self.class}"
|
||||
end
|
||||
|
||||
return 0 unless count != 0
|
||||
|
||||
total_score / count
|
||||
return 0 unless ratings.count > 0
|
||||
ratings.map { |r| r.score }.sum / ratings.count.to_f
|
||||
end
|
||||
|
||||
def count_ratings
|
||||
beers = if defined? self.beers
|
||||
self.beers
|
||||
else
|
||||
self
|
||||
end
|
||||
|
||||
count = 0
|
||||
beers.each do |beer|
|
||||
count += beer.ratings.count
|
||||
if self.class == Beer or User
|
||||
return self.ratings.count
|
||||
elsif self.class == Brewery
|
||||
return self.beers.map { |b| b.ratings.count }.sum
|
||||
else
|
||||
raise "Wrong class type for counting ratings, #{self.class}"
|
||||
end
|
||||
count
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class Rating < ApplicationRecord
|
||||
belongs_to :beer
|
||||
belongs_to :user
|
||||
|
||||
def to_s
|
||||
"tekstiesitys"
|
||||
"#{self.beer.name}: #{self.score}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
class User < ApplicationRecord
|
||||
include RatingAverage
|
||||
|
||||
has_many :ratings
|
||||
end
|
||||
|
||||
@@ -11,10 +11,22 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p>
|
||||
<%= link_to "breweries", breweries_path %>
|
||||
<%= link_to "beers", beers_path %>
|
||||
<%= link_to "ratings", ratings_path %>
|
||||
<%= link_to "users", users_path %>
|
||||
|
|
||||
<% if current_user.nil? %>
|
||||
<%= link_to "Sign up", new_user_path %>
|
||||
<%= link_to "Sign in", new_session_path %>
|
||||
<% else %>
|
||||
<%= link_to current_user.username, user_path(current_user.id) %>
|
||||
<%= link_to "Rate a new beer", new_rating_path %>
|
||||
<%= link_to "signout", signout_path, data: { "turbo-method": :delete } %>
|
||||
<% end %>
|
||||
</p>
|
||||
<%= yield %>
|
||||
<% if not session[:user_id].nil? %>
|
||||
<p><%= User.find(session[:user_id]).username %> signed in</p>
|
||||
<% end %>
|
||||
<% if session[:last_rating].nil? %>
|
||||
<p>no ratings given</p>
|
||||
<% else %>
|
||||
|
||||
@@ -1,8 +1,28 @@
|
||||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<%= render @user %>
|
||||
|
||||
<div>
|
||||
<p>
|
||||
<strong>Username:</strong>
|
||||
<%= @user.username %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Has made <%= pluralize(@user.ratings.count, "rating") %>, average rating <%= @user.average_rating %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>ratings:</strong>
|
||||
<ul>
|
||||
<% @user.ratings.each do |r| %>
|
||||
<li>
|
||||
<%=r.beer.name %> <%= r.score %>
|
||||
<%= link_to "Delete", ratings_path(r.id), method: "delete" %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
|
||||
<%= link_to "Edit this user", edit_user_path(@user) %> |
|
||||
<%= link_to "Back to users", users_path %>
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ Rails.application.routes.draw do
|
||||
# root "articles#index"
|
||||
get '/', to: 'breweries#index'
|
||||
get 'signup', to: 'users#new'
|
||||
delete 'signout', to: 'sessions#destroy'
|
||||
resources :ratings, only: [:index, :new, :create, :destroy]
|
||||
resource :session, only: [:new, :create, :destroy]
|
||||
end
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddUserIdToRatings < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
add_column :ratings, :user_id, :integer
|
||||
end
|
||||
end
|
||||
Generated
+2
-1
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_08_08_143510) do
|
||||
ActiveRecord::Schema[7.0].define(version: 2023_08_10_071347) do
|
||||
create_table "beers", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "style"
|
||||
@@ -31,6 +31,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_08_143510) do
|
||||
t.integer "beer_id"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "user_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
|
||||
Reference in New Issue
Block a user