From c3fbb70cffc65381e05f44e4af0cfd80ee7078a5 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Tue, 26 Sep 2023 13:00:02 -0500 Subject: [PATCH] Complete Lesson 25 --- app/controllers/movies_controller.rb | 76 ++++++++++++++-------------- app/helpers/reviews_helper.rb | 7 +++ app/models/movie.rb | 3 ++ app/views/movies/index.html.erb | 3 ++ app/views/movies/show.html.erb | 1 + 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index e849cd4..fd98483 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,42 +1,42 @@ class MoviesController < ApplicationController - def index - @movies = Movie.released - end - def show - @movie = Movie.find(params[:id]) - end - def edit - @movie = Movie.find(params[:id]) - end - def update - @movie = Movie.find(params[:id]) - if @movie.update(movie_params) - redirect_to @movie, notice: "Movie successfully updated!" - else - render :edit, status: :unprocessable_entity - end - end - def new - @movie = Movie.new - end - def create - @movie = Movie.new(movie_params) - if @movie.save - redirect_to @movie, notice: "Movie successfully created!" - else - render :new, status: :unprocessable_entity - end - end - def destroy - @movie = Movie.find(params[:id]) - if @movie.destroy - redirect_to movies_url, status: :see_other, alert: "Movie successfully destroyed!" - end - end + def index + @movies = Movie.released + end + def show + @movie = Movie.find(params[:id]) + end + def edit + @movie = Movie.find(params[:id]) + end + def update + @movie = Movie.find(params[:id]) + if @movie.update(movie_params) + redirect_to @movie, notice: "Movie successfully updated!" + else + render :edit, status: :unprocessable_entity + end + end + def new + @movie = Movie.new + end + def create + @movie = Movie.new(movie_params) + if @movie.save + redirect_to @movie, notice: "Movie successfully created!" + else + render :new, status: :unprocessable_entity + end + end + def destroy + @movie = Movie.find(params[:id]) + if @movie.destroy + redirect_to movies_url, status: :see_other, alert: "Movie successfully destroyed!" + end + end - private + private - def movie_params - params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name) - end + def movie_params + params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name) + end end diff --git a/app/helpers/reviews_helper.rb b/app/helpers/reviews_helper.rb index 682b7b1..b46201d 100644 --- a/app/helpers/reviews_helper.rb +++ b/app/helpers/reviews_helper.rb @@ -1,2 +1,9 @@ module ReviewsHelper + def average_stars(movie) + if movie.average_stars.zero? + content_tag(:strong, "No reviews") + else + pluralize(number_with_precision(movie.average_stars, precision: 1) , "star") + end + end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 27d0c05..e8ccd90 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -17,4 +17,7 @@ class Movie < ApplicationRecord def self.released where("released_on < ?", Time.now).order(released_on: :desc) end + def average_stars + reviews.average(:stars) || 0.0 + end end diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb index b03bf33..7fa639f 100644 --- a/app/views/movies/index.html.erb +++ b/app/views/movies/index.html.erb @@ -12,6 +12,9 @@

<%= total_gross(movie) %>

+ + <%= average_stars(movie) %> +

<%= truncate(movie.description, length: 150, separator: ' ') %>

diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index ce6b53a..d5e0508 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -8,6 +8,7 @@ <%= year_of(@movie) %> • <%= @movie.rating %>
+ <%= average_stars(@movie) %> <%= link_to pluralize(@movie.reviews.size, "review"), movie_reviews_path(@movie) %>