From f58cf28a446c696467deb7807a890d19eaecd103 Mon Sep 17 00:00:00 2001 From: Mohammad Ali Karimi Date: Sat, 29 Apr 2023 16:42:38 +0000 Subject: [PATCH 01/14] Added gem, draft resource generator and bundle install --- Gemfile | 2 + Gemfile.lock | 24 +++++ app/controllers/movies_controller.rb | 58 +++++++++++ app/models/movie.rb | 2 + app/views/movies/index.html.erb | 116 +++++++++++++++++++++ app/views/movies/show.html.erb | 101 ++++++++++++++++++ config/routes.rb | 19 ++++ db/migrate/20230429163502_create_movies.rb | 11 ++ db/schema.rb | 10 +- test/fixtures/movies.yml | 11 ++ test/models/movie_test.rb | 7 ++ 11 files changed, 360 insertions(+), 1 deletion(-) create mode 100644 app/controllers/movies_controller.rb create mode 100644 app/models/movie.rb create mode 100644 app/views/movies/index.html.erb create mode 100644 app/views/movies/show.html.erb create mode 100644 db/migrate/20230429163502_create_movies.rb create mode 100644 test/fixtures/movies.yml create mode 100644 test/models/movie_test.rb diff --git a/Gemfile b/Gemfile index 3d7ed82..ce03a34 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ ruby '3.0.3' gem "grade_runner", github: "firstdraft/grade_runner" +gem "draft_generators", github: "firstdraft/draft_generators" + # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.1' # Use postgresql as the database for Active Record diff --git a/Gemfile.lock b/Gemfile.lock index fe0ec3b..a484f54 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,11 @@ +GIT + remote: https://github.com/firstdraft/draft_generators.git + revision: 23069b562615e42e04c3343e330846594ce51fdb + specs: + draft_generators (0.0.3) + devise + indefinite_article + GIT remote: https://github.com/firstdraft/grade_runner.git revision: e19ab1764a5a4e0a89731fcab76e352d60240ddf @@ -90,6 +98,7 @@ GEM ast (2.4.2) awesome_print (1.9.2) backport (1.2.0) + bcrypt (3.1.18) benchmark (0.2.0) better_errors (2.9.1) coderay (>= 1.0.0) @@ -130,6 +139,12 @@ GEM rexml crass (1.0.6) debug_inspector (1.1.0) + devise (4.9.2) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) diff-lcs (1.5.0) diffy (3.4.2) e2mmap (0.1.0) @@ -157,6 +172,8 @@ GEM image_processing (1.12.2) mini_magick (>= 4.9.5, < 5) ruby-vips (>= 2.0.17, < 3) + indefinite_article (0.2.5) + activesupport jaro_winkler (1.5.4) jbuilder (2.11.5) actionview (>= 5.0.0) @@ -193,6 +210,7 @@ GEM faraday (>= 1, < 3) sawyer (~> 0.9) oj (3.10.18) + orm_adapter (0.5.0) parallel (1.22.1) parser (3.1.1.0) ast (~> 2.4.1) @@ -256,6 +274,9 @@ GEM ffi (~> 1.0) rchardet (1.8.0) regexp_parser (2.2.1) + responders (3.1.0) + actionpack (>= 5.2) + railties (>= 5.2) reverse_markdown (2.1.1) nokogiri rexml (3.2.5) @@ -375,6 +396,8 @@ GEM tzinfo (>= 1.0.0) unicode-display_width (2.1.0) uniform_notifier (1.16.0) + warden (1.2.9) + rack (>= 2.0.9) web-console (4.2.0) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -417,6 +440,7 @@ DEPENDENCIES byebug capybara (>= 3.26) carrierwave + draft_generators! factory_bot_rails grade_runner! htmlbeautifier diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb new file mode 100644 index 0000000..e8c70fe --- /dev/null +++ b/app/controllers/movies_controller.rb @@ -0,0 +1,58 @@ +class MoviesController < ApplicationController + def index + matching_movies = Movie.all + + @list_of_movies = matching_movies.order({ :created_at => :desc }) + + render({ :template => "movies/index.html.erb" }) + end + + def show + the_id = params.fetch("path_id") + + matching_movies = Movie.where({ :id => the_id }) + + @the_movie = matching_movies.at(0) + + render({ :template => "movies/show.html.erb" }) + end + + def create + the_movie = Movie.new + the_movie.title = params.fetch("query_title") + the_movie.description = params.fetch("query_description") + the_movie.released = params.fetch("query_released", false) + + if the_movie.valid? + the_movie.save + redirect_to("/movies", { :notice => "Movie created successfully." }) + else + redirect_to("/movies", { :alert => the_movie.errors.full_messages.to_sentence }) + end + end + + def update + the_id = params.fetch("path_id") + the_movie = Movie.where({ :id => the_id }).at(0) + + the_movie.title = params.fetch("query_title") + the_movie.description = params.fetch("query_description") + the_movie.released = params.fetch("query_released", false) + + if the_movie.valid? + the_movie.save + redirect_to("/movies/#{the_movie.id}", { :notice => "Movie updated successfully."} ) + else + redirect_to("/movies/#{the_movie.id}", { :alert => the_movie.errors.full_messages.to_sentence }) + end + end + + def destroy + the_id = params.fetch("path_id") + the_movie = Movie.where({ :id => the_id }).at(0) + + the_movie.destroy + + redirect_to("/movies", { :notice => "Movie deleted successfully."} ) + end +end diff --git a/app/models/movie.rb b/app/models/movie.rb new file mode 100644 index 0000000..dc614df --- /dev/null +++ b/app/models/movie.rb @@ -0,0 +1,2 @@ +class Movie < ApplicationRecord +end diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb new file mode 100644 index 0000000..1fb5985 --- /dev/null +++ b/app/views/movies/index.html.erb @@ -0,0 +1,116 @@ +
+
+

+ List of all movies +

+
+
+ +
+ +
+
+

+ Add a new movie +

+ +
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+ + +
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + <% @list_of_movies.each do |a_movie| %> + + + + + + + + + + + + + + + <% end %> +
+ ID + + Title + + Description + + Released + + Created at + + Updated at + +
+ <%= a_movie.id %> + + <%= a_movie.title %> + + <%= a_movie.description %> + + <%= a_movie.released %> + + <%= time_ago_in_words(a_movie.created_at) %> ago + + <%= time_ago_in_words(a_movie.updated_at) %> ago + + + Show details + +
+
+
+ +
diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb new file mode 100644 index 0000000..a5d4003 --- /dev/null +++ b/app/views/movies/show.html.erb @@ -0,0 +1,101 @@ +
+
+

+ Movie #<%= @the_movie.id %> details +

+ +
+
+ + Go back + +
+ +
+ + Delete movie + +
+
+ +
+
+ Title +
+
+ <%= @the_movie.title %> +
+ +
+ Description +
+
+ <%= @the_movie.description %> +
+ +
+ Released +
+
+ <%= @the_movie.released %> +
+ +
+ Created at +
+
+ <%= time_ago_in_words(@the_movie.created_at) %> ago +
+ +
+ Updated at +
+
+ <%= time_ago_in_words(@the_movie.updated_at) %> ago +
+
+
+
+ +
+ + +
+
+

+ Edit movie +

+ +
+
+ + + +
+ +
+ + + +
+ +
+ > + + +
+ + +
+
+
+ +
diff --git a/config/routes.rb b/config/routes.rb index c06383a..f2a03a7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,22 @@ Rails.application.routes.draw do + # Routes for the Movie resource: + + # CREATE + post("/insert_movie", { :controller => "movies", :action => "create" }) + + # READ + get("/movies", { :controller => "movies", :action => "index" }) + + get("/movies/:path_id", { :controller => "movies", :action => "show" }) + + # UPDATE + + post("/modify_movie/:path_id", { :controller => "movies", :action => "update" }) + + # DELETE + get("/delete_movie/:path_id", { :controller => "movies", :action => "destroy" }) + + #------------------------------ + # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20230429163502_create_movies.rb b/db/migrate/20230429163502_create_movies.rb new file mode 100644 index 0000000..65fc80f --- /dev/null +++ b/db/migrate/20230429163502_create_movies.rb @@ -0,0 +1,11 @@ +class CreateMovies < ActiveRecord::Migration[6.1] + def change + create_table :movies do |t| + t.string :title + t.text :description + t.boolean :released + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4603022..f22b36f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 2023_04_29_163502) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "movies", force: :cascade do |t| + t.string "title" + t.text "description" + t.boolean "released" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml new file mode 100644 index 0000000..5e5594a --- /dev/null +++ b/test/fixtures/movies.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + description: MyText + released: false + +two: + title: MyString + description: MyText + released: false diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb new file mode 100644 index 0000000..c81cd39 --- /dev/null +++ b/test/models/movie_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class MovieTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 1a26c5b67500c7a9009bb31d59c7480e7961848a Mon Sep 17 00:00:00 2001 From: Mohammad Ali Karimi Date: Sat, 29 Apr 2023 18:18:55 +0000 Subject: [PATCH 02/14] Added authenticity token --- app/views/movies/index.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb index 1fb5985..1cc0c0c 100644 --- a/app/views/movies/index.html.erb +++ b/app/views/movies/index.html.erb @@ -15,6 +15,7 @@
+
diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index 83d4244..c809c91 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -13,7 +13,7 @@
- Delete movie + Delete Movie