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
5 changes: 5 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ def show
# GET /movies/new
def new
@movie = Movie.new

end

# GET /movies/1/edit
def edit
render template:"movies/edit.js.erb"
format.js
end

# POST /movies or /movies.json
Expand All @@ -27,6 +30,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 @@ -53,6 +57,7 @@ def 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
13 changes: 13 additions & 0 deletions app/models/actor.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# == Schema Information
#
# Table name: actors
#
# id :bigint not null, primary key
# bio :text
# dob :date
# image :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Actor < ApplicationRecord
has_many :characters, dependent: :destroy
end
21 changes: 21 additions & 0 deletions app/models/character.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# == Schema Information
#
# Table name: characters
#
# id :bigint not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# actor_id :bigint not null
# movie_id :bigint not null
#
# Indexes
#
# index_characters_on_actor_id (actor_id)
# index_characters_on_movie_id (movie_id)
#
# Foreign Keys
#
# fk_rails_... (actor_id => actors.id)
# fk_rails_... (movie_id => movies.id)
#
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
# bio :text
# dob :date
# image :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Director < ApplicationRecord
has_many :movies
end
23 changes: 23 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# == Schema Information
#
# Table name: movies
#
# id :bigint not null, primary key
# description :text
# duration :integer
# image :string
# title :string
# year :integer
# created_at :datetime not null
# updated_at :datetime not null
# director_id :bigint
#
# Indexes
#
# index_movies_on_director_id (director_id)
#
# Foreign Keys
#
# fk_rails_... (director_id => directors.id)
#
class Movie < ApplicationRecord
belongs_to :director
has_many :characters, dependent: :destroy
end
5 changes: 5 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
</ul>
</nav>
<%= yield %>

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

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

</body>
</html>
10 changes: 5 additions & 5 deletions app/views/movies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with(model: movie) do |form| %>
<%= 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 @@ -11,22 +11,22 @@
</div>
<% end %>

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

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

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

<div class="field">
<div class="field" >
<%= form.label :image %>
<%= form.text_field :image %>
</div>
Expand Down
9 changes: 9 additions & 0 deletions app/views/movies/_movie.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<tr id="<%= dom_id(movie)%>">
<td><%= link_to movie.title, edit_movie_path(movie), remote:true %></td>
<td><%= movie.year %></td>
<td><%= Director.find_by(id: movie.director_id).name %> </td>
<td><%= movie.description %></td>
<td><%= movie.duration %></td>
<td><%= link_to 'Delete', 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 @@
console.log("New Movie Created");

$("#movies_table").prepend("<%= j(render 'movies/movie', movie: @movie)%>");

$("#new_movie_form").trigger("reset");
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)%>").remove();

console.log("Deleted");
3 changes: 3 additions & 0 deletions app/views/movies/edit.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log("Edit Success!")

$("#<%= dom_id(@movie)%>").prepend("<%= j(render "form") %>);
64 changes: 51 additions & 13 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,68 @@
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Duration</th>
<th>Year</th>
<th>Director</th>
<th>Description</th>
<th>Duration</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.description %></td>
<td><%= movie.duration %></td>
<tbody id="new_movie_form">
<tr>
<%= form_with(model: Movie.new, local:false) do |form| %>
<td class="field">
<%= form.text_field :title %>
</td>

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

<td class="field">
<%= form.collection_select :director_id, Director.order(:name), :id, :name, {} %>
</td>

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

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

<td class="actions">
<button class="btn btn-light">
<%= form.submit "Create Movie" %>
</button>
</td>
<% end %>
</tr>
</tbody>

<tbody id="movies_table">
<% @movies.order(title: :asc).each do |movie| %>
<tr id="<%= dom_id(movie)%>">
<td><%= link_to movie.title, edit_movie_path(movie), remote:true %></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>
<td><%= Director.find_by(id: movie.director_id).name %> </td>

<td>
<details>
<summary> Show Description </summary>
<p> <%= movie.description %> </p>
<p> <%= link_to 'Edit', edit_movie_path(movie) , remote:true %> </p>
</details>
</td>

<td><%= movie.duration %></td>
<td><%= link_to 'Delete', movie, method: :delete, remote:true %>
</td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Movie', new_movie_path %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# == Route Map
#

Rails.application.routes.draw do
root "movies#index"

Expand Down
59 changes: 59 additions & 0 deletions lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# NOTE: only doing this in development as some production environments (Heroku)
# NOTE: are sensitive to local FS writes, and besides -- it's just not proper
# NOTE: to have a dev-mode tool do its thing in production.
if Rails.env.development?
require 'annotate'
task :set_annotation_options do
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults(
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'true', #was false
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'false',
'exclude_fixtures' => 'false',
'exclude_factories' => 'false',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
)
end

Annotate.load_tasks
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
# bio :text
# dob :date
# image :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#

one:
name: MyString
Expand Down
22 changes: 21 additions & 1 deletion test/fixtures/characters.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# == Schema Information
#
# Table name: characters
#
# id :bigint not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# actor_id :bigint not null
# movie_id :bigint not null
#
# Indexes
#
# index_characters_on_actor_id (actor_id)
# index_characters_on_movie_id (movie_id)
#
# Foreign Keys
#
# fk_rails_... (actor_id => actors.id)
# fk_rails_... (movie_id => movies.id)
#

one:
name: MyString
Expand Down
Loading