diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..0d028ea 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/ratings_controller.rb b/app/controllers/ratings_controller.rb index 986ff39..2c2931c 100644 --- a/app/controllers/ratings_controller.rb +++ b/app/controllers/ratings_controller.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 13c6038..9aed109 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,6 +10,6 @@ class SessionsController < ApplicationController def destroy session[:user_id] = nil - redirect_to :root + redirect_to "/" end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8970d8b..33b18ea 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/concerns/rating_average.rb b/app/models/concerns/rating_average.rb index 72d756b..67c846e 100644 --- a/app/models/concerns/rating_average.rb +++ b/app/models/concerns/rating_average.rb @@ -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 diff --git a/app/models/rating.rb b/app/models/rating.rb index ac6bed0..7bd1295 100644 --- a/app/models/rating.rb +++ b/app/models/rating.rb @@ -1,7 +1,8 @@ class Rating < ApplicationRecord belongs_to :beer + belongs_to :user def to_s - "tekstiesitys" + "#{self.beer.name}: #{self.score}" end end diff --git a/app/models/user.rb b/app/models/user.rb index 379658a..6a04d45 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,5 @@ class User < ApplicationRecord + include RatingAverage + + has_many :ratings end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d5c21eb..68db69e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,10 +11,22 @@ +

+ <%= 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 %> +

<%= yield %> - <% if not session[:user_id].nil? %> -

<%= User.find(session[:user_id]).username %> signed in

- <% end %> <% if session[:last_rating].nil? %>

no ratings given

<% else %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 673fae2..e8d6817 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,8 +1,28 @@

<%= notice %>

-<%= render @user %> -
+

+ Username: + <%= @user.username %> +

+ +

+ Has made <%= pluralize(@user.ratings.count, "rating") %>, average rating <%= @user.average_rating %> +

+ +

+ ratings: +

+

+ + <%= link_to "Edit this user", edit_user_path(@user) %> | <%= link_to "Back to users", users_path %> diff --git a/config/routes.rb b/config/routes.rb index e191a75..328d85a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20230810071347_add_user_id_to_ratings.rb b/db/migrate/20230810071347_add_user_id_to_ratings.rb new file mode 100644 index 0000000..ca64184 --- /dev/null +++ b/db/migrate/20230810071347_add_user_id_to_ratings.rb @@ -0,0 +1,5 @@ +class AddUserIdToRatings < ActiveRecord::Migration[7.0] + def change + add_column :ratings, :user_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 87be6c1..cced3f6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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|