Skip to content

Commit

Permalink
part-3-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
YashPhatak committed Sep 7, 2024
1 parent 9af4bd9 commit 2b03239
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 12 deletions.
70 changes: 70 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class MoviesController < ApplicationController
before_action :set_movie, only: %i[ show edit update destroy ]

# GET /movies or /movies.json
def index
@movies = Movie.all
end

# GET /movies/1 or /movies/1.json
def show
end

# GET /movies/new
def new
@movie = Movie.new
end

# GET /movies/1/edit
def edit
end

# POST /movies or /movies.json
def create
@movie = Movie.new(movie_params)

respond_to do |format|
if @movie.save
format.html { redirect_to movie_url(@movie), notice: "Movie was successfully created." }
format.json { render :show, status: :created, location: @movie }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @movie.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /movies/1 or /movies/1.json
def update
respond_to do |format|
if @movie.update(movie_params)
format.html { redirect_to movie_url(@movie), notice: "Movie was successfully updated." }
format.json { render :show, status: :ok, location: @movie }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @movie.errors, status: :unprocessable_entity }
end
end
end

# DELETE /movies/1 or /movies/1.json
def destroy
@movie.destroy!

respond_to do |format|
format.html { redirect_to movies_url, notice: "Movie was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_movie
@movie = Movie.find(params[:id])
end

# Only allow a list of trusted parameters through.
def movie_params
params.require(:movie).permit(:title, :rating, :description, :release_date)
end
end
2 changes: 2 additions & 0 deletions app/helpers/movies_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MoviesHelper
end
37 changes: 37 additions & 0 deletions app/views/movies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= form_with(model: movie) do |form| %>
<% if movie.errors.any? %>
<div style="color: red">
<h2><%= pluralize(movie.errors.count, "error") %> prohibited this movie from being saved:</h2>

<ul>
<% movie.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :rating, style: "display: block" %>
<%= form.text_field :rating %>
</div>

<div>
<%= form.label :description, style: "display: block" %>
<%= form.text_field :description %>
</div>

<div>
<%= form.label :release_date, style: "display: block" %>
<%= form.text_field :release_date %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
22 changes: 22 additions & 0 deletions app/views/movies/_movie.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div id="<%= dom_id movie %>">
<p>
<strong>Title:</strong>
<%= movie.title %>
</p>

<p>
<strong>Rating:</strong>
<%= movie.rating %>
</p>

<p>
<strong>Description:</strong>
<%= movie.description %>
</p>

<p>
<strong>Release date:</strong>
<%= movie.release_date %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/movies/_movie.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! movie, :id, :title, :rating, :description, :release_date, :created_at, :updated_at
json.url movie_url(movie, format: :json)
12 changes: 12 additions & 0 deletions app/views/movies/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% content_for :title, "Editing movie" %>

<h1>Editing movie</h1>

<%= render "form", movie: @movie %>

<br>

<div>
<%= link_to "Show this movie", @movie %> |
<%= link_to "Back to movies", movies_path %>
</div>
16 changes: 16 additions & 0 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<p style="color: green"><%= notice %></p>

<% content_for :title, "Movies" %>

<h1>Movies</h1>

<div id="movies">
<% @movies.each do |movie| %>
<%= render movie %>
<p>
<%= link_to "Show this movie", movie %>
</p>
<% end %>
</div>

<%= link_to "New movie", new_movie_path %>
1 change: 1 addition & 0 deletions app/views/movies/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @movies, partial: "movies/movie", as: :movie
11 changes: 11 additions & 0 deletions app/views/movies/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% content_for :title, "New movie" %>

<h1>New movie</h1>

<%= render "form", movie: @movie %>

<br>

<div>
<%= link_to "Back to movies", movies_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/movies/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @movie %>

<div>
<%= link_to "Edit this movie", edit_movie_path(@movie) %> |
<%= link_to "Back to movies", movies_path %>

<%= button_to "Destroy this movie", @movie, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/movies/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "movies/movie", movie: @movie
14 changes: 2 additions & 12 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check

# Render dynamic PWA files from app/views/pwa/*
get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest

# Defines the root path route ("/")
# root "posts#index"
resources :movies
root :to => redirect('/movies')

Check failure on line 3 in config/routes.rb

View workflow job for this annotation

GitHub Actions / lint

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

Check failure on line 3 in config/routes.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end
48 changes: 48 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class MoviesControllerTest < ActionDispatch::IntegrationTest
setup do
@movie = movies(:one)
end

test "should get index" do
get movies_url
assert_response :success
end

test "should get new" do
get new_movie_url
assert_response :success
end

test "should create movie" do
assert_difference("Movie.count") do
post movies_url, params: { movie: { description: @movie.description, rating: @movie.rating, release_date: @movie.release_date, title: @movie.title } }
end

assert_redirected_to movie_url(Movie.last)
end

test "should show movie" do
get movie_url(@movie)
assert_response :success
end

test "should get edit" do
get edit_movie_url(@movie)
assert_response :success
end

test "should update movie" do
patch movie_url(@movie), params: { movie: { description: @movie.description, rating: @movie.rating, release_date: @movie.release_date, title: @movie.title } }
assert_redirected_to movie_url(@movie)
end

test "should destroy movie" do
assert_difference("Movie.count", -1) do
delete movie_url(@movie)
end

assert_redirected_to movies_url
end
end
47 changes: 47 additions & 0 deletions test/system/movies_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "application_system_test_case"

class MoviesTest < ApplicationSystemTestCase
setup do
@movie = movies(:one)
end

test "visiting the index" do
visit movies_url
assert_selector "h1", text: "Movies"
end

test "should create movie" do
visit movies_url
click_on "New movie"

fill_in "Description", with: @movie.description
fill_in "Rating", with: @movie.rating
fill_in "Release date", with: @movie.release_date
fill_in "Title", with: @movie.title
click_on "Create Movie"

assert_text "Movie was successfully created"
click_on "Back"
end

test "should update Movie" do
visit movie_url(@movie)
click_on "Edit this movie", match: :first

fill_in "Description", with: @movie.description
fill_in "Rating", with: @movie.rating
fill_in "Release date", with: @movie.release_date
fill_in "Title", with: @movie.title
click_on "Update Movie"

assert_text "Movie was successfully updated"
click_on "Back"
end

test "should destroy Movie" do
visit movie_url(@movie)
click_on "Destroy this movie", match: :first

assert_text "Movie was successfully destroyed"
end
end

0 comments on commit 2b03239

Please sign in to comment.