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