Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/directors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def create
if @director.save
format.html { redirect_to @director, notice: "Director was successfully created." }
format.json { render :show, status: :created, location: @director }
format.js
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @director.errors, status: :unprocessable_entity }
Expand Down
17 changes: 16 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class MoviesController < ApplicationController
before_action :set_movie, only: %i[ show edit update destroy ]
before_action :set_movie, only: %i[ show edit update destroy edit_field ]

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

# GET /movies/1 or /movies/1.json
Expand All @@ -19,6 +20,15 @@ def new
def edit
end

# GET /movies/:id/:field/edit
def edit_field
@movie_id = params.fetch(:id)
@field = params.fetch(:field)
respond_to do |format|
format.js
end
end

# POST /movies or /movies.json
def create
@movie = Movie.new(movie_params)
Expand All @@ -27,6 +37,7 @@ def create
if @movie.save
format.html { redirect_to @movie, notice: "Movie was successfully created." }
format.json { render :show, status: :created, location: @movie }
format.js
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @movie.errors, status: :unprocessable_entity }
Expand All @@ -40,19 +51,23 @@ def update
if @movie.update(movie_params)
format.html { redirect_to @movie, notice: "Movie was successfully updated." }
format.json { render :show, status: :ok, location: @movie }
format.js
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 }
format.js
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

import Rails from "@rails/ujs"
import Rails, { $ } from "@rails/ujs"; global.Rails = Rails;
import * as ActiveStorage from "@rails/activestorage"
import "channels"

Expand Down
15 changes: 15 additions & 0 deletions app/models/actor.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# == Schema Information
#
# Table name: actors
#
# id :bigint not null, primary key
# name :string
# bio :text
# dob :date
# image :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Actor < ApplicationRecord
has_many :characters, dependent: :destroy
has_many :movies, through: :characters

end
11 changes: 11 additions & 0 deletions app/models/character.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# == Schema Information
#
# Table name: characters
#
# id :bigint not null, primary key
# name :string
# actor_id :bigint not null
# movie_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Character < ApplicationRecord
belongs_to :actor
belongs_to :movie
Expand Down
13 changes: 13 additions & 0 deletions app/models/director.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# == Schema Information
#
# Table name: directors
#
# id :bigint not null, primary key
# name :string
# bio :text
# dob :date
# image :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Director < ApplicationRecord
has_many :movies
end
16 changes: 16 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# == Schema Information
#
# Table name: movies
#
# id :bigint not null, primary key
# title :string
# description :text
# duration :integer
# image :string
# year :integer
# director_id :bigint
# created_at :datetime not null
# updated_at :datetime not null
#
class Movie < ApplicationRecord
belongs_to :director
has_many :characters, dependent: :destroy
has_many :actors, through: :characters
end
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= render "shared/cdn_assets" %>

<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_pack_tag 'application' %>
</head>
Expand Down
16 changes: 16 additions & 0 deletions app/views/movies/_field_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<% if @field == "director"%>
<td>
<%= form_with(model: movie, local: false) do |form| %>
<%= form.collection_select :director_id, Director.order(:name),:id,:name,{}, onchange: 'Rails.fire(this.form, "submit")' %>
<% end %>
</td>

<% else %>
<td>
<%= form_with(model: movie, local: false) do |form| %>
<%= form.text_field @field %>
<% end %>
</td>

<% end %>
80 changes: 45 additions & 35 deletions app/views/movies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= form_with(model: movie) do |form| %>
<div>
<%= form_with(model: movie, local: false) do |form| %>
<% if movie.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(movie.errors.count, "error") %> prohibited this movie from being saved:</h2>
Expand All @@ -10,38 +11,47 @@
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>

<div class="field">
<%= form.label :duration %>
<%= form.number_field :duration %>
</div>

<div class="field">
<%= form.label :image %>
<%= form.text_field :image %>
</div>

<div class="field">
<%= form.label :year %>
<%= form.number_field :year %>
</div>

<div class="field">
<%= form.label :director_id %>
<%= form.text_field :director_id %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<tr>
<td>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
</td>

<td>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
</td>

<td>
<div class="field">
<%= form.label :duration %>
<%= form.number_field :duration %>
</div>
</td>


<td>
<div class="field">
<%= form.label :year %>
<%= form.number_field :year %>
</div>
</td>
<td>
<div class="field">
<%= form.label :director_name %>
<%= form.collection_select :director_id, Director.order(:name),:id,:name, include_blank: true %>
</div>
</td>

<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
</tr>
<% end %>
</div>
37 changes: 37 additions & 0 deletions app/views/movies/_table_row.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<tr id="<%= dom_id(movie) %>">

<td>
<div id="<%= dom_id(movie) %>_title">
<%= link_to movie.title, "/movies/#{movie.id}/title/edit", remote: true %>
</div>
</td>

<td>
<details>
<summary>Show description</summary>
<p>
<div id="<%= dom_id(movie) %>_description">
<%= link_to movie.description, "/movies/#{movie.id}/description/edit", remote: true %>
</div>
</p>
</details>
</td>
<td>
<div id="<%= dom_id(movie) %>_duration">
<%= link_to movie.duration, "/movies/#{movie.id}/duration/edit", remote: true %>
</div>
</td>
<td>
<div id="<%= dom_id(movie) %>_year">
<%= link_to movie.year, "/movies/#{movie.id}/year/edit", remote: true %>
</div>
</td>
<td>
<div id="<%= dom_id(movie) %>_director">
<%= link_to movie.director.name, "/movies/#{movie.id}/director/edit", remote: true %>
</div>
</td>
<td><%= link_to 'Destroy', movie, method: :delete, remote: true %>

</td>
</tr>
5 changes: 5 additions & 0 deletions app/views/movies/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var added_movie = $("<%= j(render 'movies/table_row', movie: @movie) %>");

$("#movies").before(added_movie);


3 changes: 3 additions & 0 deletions app/views/movies/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$("#<%= dom_id(@movie) %>").fadeOut(function() {
$(this).remove();
});
4 changes: 4 additions & 0 deletions app/views/movies/edit_field.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var updated_field = $("<%= j(render 'movies/field_form', movie: @movie, movie_id: @movie_id, field: @field ) %>");

$("#movie_<%= @movie_id %>_<%= @field %>").replaceWith(updated_field);

18 changes: 6 additions & 12 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@
</tr>
</thead>

<tbody>

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


<tbody id="movies">
<% @movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.description %></td>
<td><%= movie.duration %></td>
<td><%= movie.year %></td>
<td><%= movie.director_id %></td>
<td><%= link_to 'Show', movie %></td>
<td><%= link_to 'Edit', edit_movie_path(movie) %></td>
<td><%= link_to 'Destroy', movie, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<%= render 'table_row', movie: movie %>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Movie', new_movie_path %>
5 changes: 5 additions & 0 deletions app/views/movies/update.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var updated_movie = $("<%= j(render 'movies/table_row', movie: @movie) %>");

$("#movie_<%= @movie.id %>").replaceWith(updated_movie);


8 changes: 8 additions & 0 deletions app/views/shared/_cdn_assets.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<meta charset="utf-8">

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
resources :actors
resources :directors
resources :movies


get '/movies/:id/:field/edit', to: 'movies#edit_field'


end
13 changes: 12 additions & 1 deletion test/fixtures/actors.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# == Schema Information
#
# Table name: actors
#
# id :bigint not null, primary key
# name :string
# bio :text
# dob :date
# image :string
# created_at :datetime not null
# updated_at :datetime not null
#

one:
name: MyString
Expand Down
Loading