From 103f693a3381a14b85f84a8701a1bdd6f90c22d9 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 17:59:06 -0500 Subject: [PATCH 01/16] Add ArticlesController with related views, tests, and routes --- app/controllers/articles_controller.rb | 19 +++++++++++ app/helpers/articles_helper.rb | 2 ++ app/views/articles/create.html.erb | 2 ++ app/views/articles/destroy.html.erb | 2 ++ app/views/articles/display.html.erb | 2 ++ app/views/articles/edit.html.erb | 2 ++ app/views/articles/index.html.erb | 2 ++ app/views/articles/update.html.erb | 2 ++ config/routes.rb | 6 ++++ test/controllers/articles_controller_test.rb | 33 ++++++++++++++++++++ 10 files changed, 72 insertions(+) create mode 100644 app/controllers/articles_controller.rb create mode 100644 app/helpers/articles_helper.rb create mode 100644 app/views/articles/create.html.erb create mode 100644 app/views/articles/destroy.html.erb create mode 100644 app/views/articles/display.html.erb create mode 100644 app/views/articles/edit.html.erb create mode 100644 app/views/articles/index.html.erb create mode 100644 app/views/articles/update.html.erb create mode 100644 test/controllers/articles_controller_test.rb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 000000000..7804d55d0 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,19 @@ +class ArticlesController < ApplicationController + def index + end + + def create + end + + def display + end + + def edit + end + + def update + end + + def destroy + end +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 000000000..296827759 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/app/views/articles/create.html.erb b/app/views/articles/create.html.erb new file mode 100644 index 000000000..92bd1fa28 --- /dev/null +++ b/app/views/articles/create.html.erb @@ -0,0 +1,2 @@ +

Articles#create

+

Find me in app/views/articles/create.html.erb

diff --git a/app/views/articles/destroy.html.erb b/app/views/articles/destroy.html.erb new file mode 100644 index 000000000..02b852542 --- /dev/null +++ b/app/views/articles/destroy.html.erb @@ -0,0 +1,2 @@ +

Articles#destroy

+

Find me in app/views/articles/destroy.html.erb

diff --git a/app/views/articles/display.html.erb b/app/views/articles/display.html.erb new file mode 100644 index 000000000..c59787906 --- /dev/null +++ b/app/views/articles/display.html.erb @@ -0,0 +1,2 @@ +

Articles#display

+

Find me in app/views/articles/display.html.erb

diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 000000000..78998ca74 --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,2 @@ +

Articles#edit

+

Find me in app/views/articles/edit.html.erb

diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 000000000..ab9c55261 --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,2 @@ +

Articles#index

+

Find me in app/views/articles/index.html.erb

diff --git a/app/views/articles/update.html.erb b/app/views/articles/update.html.erb new file mode 100644 index 000000000..41b28c77c --- /dev/null +++ b/app/views/articles/update.html.erb @@ -0,0 +1,2 @@ +

Articles#update

+

Find me in app/views/articles/update.html.erb

diff --git a/config/routes.rb b/config/routes.rb index a125ef085..7ea4f92ab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,10 @@ Rails.application.routes.draw do + get 'articles/index' + get 'articles/create' + get 'articles/display' + get 'articles/edit' + get 'articles/update' + get 'articles/destroy' # 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. diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 000000000..1661a35ac --- /dev/null +++ b/test/controllers/articles_controller_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +class ArticlesControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get articles_index_url + assert_response :success + end + + test "should get create" do + get articles_create_url + assert_response :success + end + + test "should get display" do + get articles_display_url + assert_response :success + end + + test "should get edit" do + get articles_edit_url + assert_response :success + end + + test "should get update" do + get articles_update_url + assert_response :success + end + + test "should get destroy" do + get articles_destroy_url + assert_response :success + end +end From 8696816e5608c45efb58e082cb59b3e178e7e8b1 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 18:42:35 -0500 Subject: [PATCH 02/16] Create Article model --- app/models/article.rb | 2 ++ db/migrate/20240129234034_create_articles.rb | 12 ++++++++++++ test/fixtures/articles.yml | 13 +++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 app/models/article.rb create mode 100644 db/migrate/20240129234034_create_articles.rb create mode 100644 test/fixtures/articles.yml diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 000000000..b7a72b589 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,2 @@ +class Article < ApplicationRecord +end diff --git a/db/migrate/20240129234034_create_articles.rb b/db/migrate/20240129234034_create_articles.rb new file mode 100644 index 000000000..f54e9966c --- /dev/null +++ b/db/migrate/20240129234034_create_articles.rb @@ -0,0 +1,12 @@ +class CreateArticles < ActiveRecord::Migration[7.1] + def change + create_table :articles do |t| + t.string :title + t.string :content + t.string :author + t.date :date + + t.timestamps + end + end +end diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 000000000..c807eea07 --- /dev/null +++ b/test/fixtures/articles.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + content: MyString + author: MyString + date: 2024-01-29 + +two: + title: MyString + content: MyString + author: MyString + date: 2024-01-29 From eb6689d9c472309dd45b8557c9205662b60949dc Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 18:54:26 -0500 Subject: [PATCH 03/16] Change 'content' column type to text in articles table --- .../20240129235228_change_content_to_text.rb | 6 +++++ db/schema.rb | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 db/migrate/20240129235228_change_content_to_text.rb create mode 100644 db/schema.rb diff --git a/db/migrate/20240129235228_change_content_to_text.rb b/db/migrate/20240129235228_change_content_to_text.rb new file mode 100644 index 000000000..d50685cfe --- /dev/null +++ b/db/migrate/20240129235228_change_content_to_text.rb @@ -0,0 +1,6 @@ +class ChangeContentToText < ActiveRecord::Migration[7.1] + def change + change_column :articles, + :content, :text + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..a8e37c8c1 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2024_01_29_235228) do + create_table "articles", force: :cascade do |t| + t.string "title" + t.text "content" + t.string "author" + t.date "date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From db4e546b836c0db6e3f9153489705bbafe441db3 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 19:20:56 -0500 Subject: [PATCH 04/16] Update articles page to display all articles --- app/controllers/articles_controller.rb | 1 + app/views/articles/index.html.erb | 12 ++++++++++-- config/routes.rb | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 7804d55d0..1a7e17ff8 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,5 +1,6 @@ class ArticlesController < ApplicationController def index + @articles = Article.all end def create diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index ab9c55261..cccc2ac3c 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,2 +1,10 @@ -

Articles#index

-

Find me in app/views/articles/index.html.erb

+

Articles

+
+ <% @articles.each do |article| %> +
+

<%= article.title %>

+ by <%= article.author %> on <%= article.date %> +

<%= article.content %>

+
+ <% end %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7ea4f92ab..2e606078a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - get 'articles/index' + get 'articles', to:"articles#index" get 'articles/create' get 'articles/display' get 'articles/edit' From 35f06688afba16fbebf117cccbddb5a506ed3526 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 19:38:17 -0500 Subject: [PATCH 05/16] implement individual article view --- app/controllers/articles_controller.rb | 3 ++- app/views/articles/display.html.erb | 2 -- app/views/articles/update.html.erb | 2 -- app/views/articles/view.html.erb | 4 ++++ config/routes.rb | 10 +++++----- 5 files changed, 11 insertions(+), 10 deletions(-) delete mode 100644 app/views/articles/display.html.erb delete mode 100644 app/views/articles/update.html.erb create mode 100644 app/views/articles/view.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 1a7e17ff8..0e522c8f2 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -6,7 +6,8 @@ def index def create end - def display + def view + @article = Article.find(params[:id]) end def edit diff --git a/app/views/articles/display.html.erb b/app/views/articles/display.html.erb deleted file mode 100644 index c59787906..000000000 --- a/app/views/articles/display.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -

Articles#display

-

Find me in app/views/articles/display.html.erb

diff --git a/app/views/articles/update.html.erb b/app/views/articles/update.html.erb deleted file mode 100644 index 41b28c77c..000000000 --- a/app/views/articles/update.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -

Articles#update

-

Find me in app/views/articles/update.html.erb

diff --git a/app/views/articles/view.html.erb b/app/views/articles/view.html.erb new file mode 100644 index 000000000..ba2fcd4bc --- /dev/null +++ b/app/views/articles/view.html.erb @@ -0,0 +1,4 @@ +

<%= @article.title %>

+

<%= @article.author %>

+

<%= @article.date %>

+

<%= @article.content %>

\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2e606078a..b2bc1eb59 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,10 @@ Rails.application.routes.draw do get 'articles', to:"articles#index" - get 'articles/create' - get 'articles/display' - get 'articles/edit' - get 'articles/update' - get 'articles/destroy' + # get 'articles/new', to:"articles#create" + get 'articles/:id', to:"articles#view" + # get 'articles/edit', to:"articles#edit" + # get 'articles/update', to:"articles#update" + # get 'articles/destroy', to:"articles#destroy" # 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. From 8d7cac902015cee05711f23a4014c8df4f11b690 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 19:46:22 -0500 Subject: [PATCH 06/16] Update article list view --- app/views/articles/index.html.erb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index cccc2ac3c..5e5f994dd 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,10 +1,10 @@

Articles

-
+
    <% @articles.each do |article| %> -
    -

    <%= article.title %>

    - by <%= article.author %> on <%= article.date %> -

    <%= article.content %>

    -
    +
  • + + <%= article.title %> + +
  • <% end %> -
\ No newline at end of file + \ No newline at end of file From cacb16e11d81b362abce804d7d6870676da5840d Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 20:10:54 -0500 Subject: [PATCH 07/16] Rename 'view' to 'show' in articles controller --- app/controllers/articles_controller.rb | 2 +- app/views/articles/{view.html.erb => show.html.erb} | 0 config/routes.rb | 9 +-------- 3 files changed, 2 insertions(+), 9 deletions(-) rename app/views/articles/{view.html.erb => show.html.erb} (100%) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 0e522c8f2..93f8a263c 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -6,7 +6,7 @@ def index def create end - def view + def show @article = Article.find(params[:id]) end diff --git a/app/views/articles/view.html.erb b/app/views/articles/show.html.erb similarity index 100% rename from app/views/articles/view.html.erb rename to app/views/articles/show.html.erb diff --git a/config/routes.rb b/config/routes.rb index b2bc1eb59..854f3b240 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,9 @@ Rails.application.routes.draw do get 'articles', to:"articles#index" # get 'articles/new', to:"articles#create" - get 'articles/:id', to:"articles#view" + get 'articles/:id', to:"articles#show" # get 'articles/edit', to:"articles#edit" # get 'articles/update', to:"articles#update" # get 'articles/destroy', to:"articles#destroy" - # 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 - - # Defines the root path route ("/") - # root "posts#index" end From a6f27e06089461c2bbda9ef181ba4065f7ef946d Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 20:15:50 -0500 Subject: [PATCH 08/16] Refactor routing for articles --- app/views/articles/index.html.erb | 2 +- config/routes.rb | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 5e5f994dd..e12375a39 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -2,7 +2,7 @@
    <% @articles.each do |article| %>
  • - + <%= article.title %>
  • diff --git a/config/routes.rb b/config/routes.rb index 854f3b240..6806a2561 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,4 @@ Rails.application.routes.draw do - get 'articles', to:"articles#index" - # get 'articles/new', to:"articles#create" - get 'articles/:id', to:"articles#show" - # get 'articles/edit', to:"articles#edit" - # get 'articles/update', to:"articles#update" - # get 'articles/destroy', to:"articles#destroy" - + root "articles#index" + resources :articles end From 62450d3542108c9f8ee0bfd4898cc706e5617f0f Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 21:18:10 -0500 Subject: [PATCH 09/16] Add new article creation feature --- app/controllers/articles_controller.rb | 15 ++++++++++++ app/models/article.rb | 10 ++++++++ app/views/articles/create.html.erb | 2 -- app/views/articles/new.html.erb | 32 ++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) delete mode 100644 app/views/articles/create.html.erb create mode 100644 app/views/articles/new.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 93f8a263c..c168e96ff 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -3,7 +3,17 @@ def index @articles = Article.all end + def new + @article = Article.new + end + def create + @article = Article.new(article_params) + if @article.save + redirect_to @article + else + render :new, status: :unprocessable_entity + end end def show @@ -18,4 +28,9 @@ def update def destroy end + + private + def article_params + params.require(:article).permit(:title, :content, :author, :date) + end end diff --git a/app/models/article.rb b/app/models/article.rb index b7a72b589..270d9c10f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,2 +1,12 @@ class Article < ApplicationRecord + before_validation :default_values + validates :title, :content, :author, :date, presence: true + + def default_values + unless self.author? + self.author = 'Anonymous' + end + self.date ||= Date.today + end + end diff --git a/app/views/articles/create.html.erb b/app/views/articles/create.html.erb deleted file mode 100644 index 92bd1fa28..000000000 --- a/app/views/articles/create.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -

    Articles#create

    -

    Find me in app/views/articles/create.html.erb

    diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 000000000..2e1dbadc2 --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,32 @@ +

    New Article

    +<%= form_with model: @article do |form| %> +
    + <%= form.label :title %>
    + <%= form.text_field :title %> + <%= @article.errors.full_messages_for(:title).each do |message| %> +
    <%= message %>
    + <% end %> +
    + +
    + <%= form.label :content %>
    + <%= form.text_area :content %> + <%= @article.errors.full_messages_for(:content).each do |message| %> +
    <%= message %>
    + <% end %> +
    + +
    + <%= form.label :author %>
    + <%= form.text_field :author %> +
    + +
    + <%= form.label :date %>
    + <%= form.text_field :date %> +
    + +
    + <%= form.submit %> +
    +<% end %> From 6e404099dd10880d5b90c0ead1d41d104b6a5ac1 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 21:31:21 -0500 Subject: [PATCH 10/16] add create and edit buttons and implement edit and update functions --- app/controllers/articles_controller.rb | 8 ++++++++ app/views/articles/index.html.erb | 3 ++- app/views/articles/show.html.erb | 4 +++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index c168e96ff..896cc72fd 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -21,9 +21,17 @@ def show end def edit + @article = Article.find(params[:id]) end def update + @article = Article.find(params[:id]) + + if @article.update(article_params) + redirect_to @article + else + render :edit, status: :unprocessable_entity + end end def destroy diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index e12375a39..9e3cf81ae 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -7,4 +7,5 @@ <% end %> -
\ No newline at end of file + +<%= link_to "Create New Article", new_article_path %> \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index ba2fcd4bc..214e724e7 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -1,4 +1,6 @@

<%= @article.title %>

<%= @article.author %>

<%= @article.date %>

-

<%= @article.content %>

\ No newline at end of file +

<%= @article.content %>

+ +<%= link_to "Edit", edit_article_path(@article) %> \ No newline at end of file From 4a951efc926a6ee732cb6b5d6bf2e73750057b0e Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 21:39:52 -0500 Subject: [PATCH 11/16] Extract article form into a partial and implement edit and create forms --- app/views/articles/_form.html.erb | 31 ++++++++++++++++++++++++++++++ app/views/articles/edit.html.erb | 4 ++-- app/views/articles/new.html.erb | 32 +------------------------------ 3 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 app/views/articles/_form.html.erb diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 000000000..a7a1c2d74 --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,31 @@ +<%= form_with model: @article do |form| %> +
+ <%= form.label :title %>
+ <%= form.text_field :title %> + <%= @article.errors.full_messages_for(:title).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.label :content %>
+ <%= form.text_area :content %> + <%= @article.errors.full_messages_for(:content).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.label :author %>
+ <%= form.text_field :author %> +
+ +
+ <%= form.label :date %>
+ <%= form.text_field :date %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb index 78998ca74..8e3d8ff69 100644 --- a/app/views/articles/edit.html.erb +++ b/app/views/articles/edit.html.erb @@ -1,2 +1,2 @@ -

Articles#edit

-

Find me in app/views/articles/edit.html.erb

+

Edit <%= @article.title %>

+<%= render "form", article: @article %> \ No newline at end of file diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb index 2e1dbadc2..024cdbe2c 100644 --- a/app/views/articles/new.html.erb +++ b/app/views/articles/new.html.erb @@ -1,32 +1,2 @@

New Article

-<%= form_with model: @article do |form| %> -
- <%= form.label :title %>
- <%= form.text_field :title %> - <%= @article.errors.full_messages_for(:title).each do |message| %> -
<%= message %>
- <% end %> -
- -
- <%= form.label :content %>
- <%= form.text_area :content %> - <%= @article.errors.full_messages_for(:content).each do |message| %> -
<%= message %>
- <% end %> -
- -
- <%= form.label :author %>
- <%= form.text_field :author %> -
- -
- <%= form.label :date %>
- <%= form.text_field :date %> -
- -
- <%= form.submit %> -
-<% end %> +<%= render "form", article: @article %> \ No newline at end of file From bd793afba9eba537cc1a7d0c981c7e647f310842 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 21:54:42 -0500 Subject: [PATCH 12/16] implement destroy --- app/controllers/articles_controller.rb | 4 ++++ app/views/articles/show.html.erb | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 896cc72fd..e85895ea0 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -35,6 +35,10 @@ def update end def destroy + @article = Article.find(params[:id]) + @article.destroy + + redirect_to root_path, status: :see_other end private diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 214e724e7..e5dc541cc 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -3,4 +3,8 @@

<%= @article.date %>

<%= @article.content %>

-<%= link_to "Edit", edit_article_path(@article) %> \ No newline at end of file +<%= link_to "Edit", edit_article_path(@article) %>
+<%= link_to "Destroy", article_path(@article), data: { + turbo_method: :delete, + turbo_confirm: "Are you sure?" +} %> \ No newline at end of file From 8c7add360284e7068e9d1fea1a560ae426d3a14d Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 22:17:16 -0500 Subject: [PATCH 13/16] Add search functionality to articles --- app/controllers/articles_controller.rb | 6 +++++- app/models/article.rb | 4 ++++ app/views/articles/index.html.erb | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e85895ea0..75b3f3e24 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,6 +1,10 @@ class ArticlesController < ApplicationController def index - @articles = Article.all + @articles = if params[:search] + Article.search(params[:search]) + else + Article.all + end end def new diff --git a/app/models/article.rb b/app/models/article.rb index 270d9c10f..e87c57b95 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -9,4 +9,8 @@ def default_values self.date ||= Date.today end + def self.search(search) + where("title LIKE ? or content LIKE ?", "%#{search}%", "%#{search}") + end + end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 9e3cf81ae..f6c887299 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,4 +1,8 @@

Articles

+<%= form_with(url: articles_path, method: "get") do %> + <%= text_field_tag :search, params[:search] %> + <%= submit_tag "Search" %> +<% end %>
    <% @articles.each do |article| %>
  • From aac966bacc2ac950f09fec4d44040e024c382392 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 22:46:21 -0500 Subject: [PATCH 14/16] fixed search --- app/models/article.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/article.rb b/app/models/article.rb index e87c57b95..2bb385cbd 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -10,7 +10,7 @@ def default_values end def self.search(search) - where("title LIKE ? or content LIKE ?", "%#{search}%", "%#{search}") + where("title LIKE ? or content LIKE ?", "%#{search}%", "%#{search}%") end end From 2a94ed24d79edc9cdfda2079d38a5efb36995e7c Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 22:47:48 -0500 Subject: [PATCH 15/16] setup db prior to running tests --- test/controllers/articles_controller_test.rb | 33 -------------------- test/models/article_test.rb | 5 +++ 2 files changed, 5 insertions(+), 33 deletions(-) delete mode 100644 test/controllers/articles_controller_test.rb diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb deleted file mode 100644 index 1661a35ac..000000000 --- a/test/controllers/articles_controller_test.rb +++ /dev/null @@ -1,33 +0,0 @@ -require "test_helper" - -class ArticlesControllerTest < ActionDispatch::IntegrationTest - test "should get index" do - get articles_index_url - assert_response :success - end - - test "should get create" do - get articles_create_url - assert_response :success - end - - test "should get display" do - get articles_display_url - assert_response :success - end - - test "should get edit" do - get articles_edit_url - assert_response :success - end - - test "should get update" do - get articles_update_url - assert_response :success - end - - test "should get destroy" do - get articles_destroy_url - assert_response :success - end -end diff --git a/test/models/article_test.rb b/test/models/article_test.rb index 0d55b70ea..d880d78cd 100644 --- a/test/models/article_test.rb +++ b/test/models/article_test.rb @@ -1,6 +1,11 @@ require 'test_helper' class ArticleTest < ActiveSupport::TestCase + + setup do + Article.delete_all + end + test 'starts with no articles' do assert_equal 0, Article.count end From 293f2bcfb1e70a62f7baf58a2de53773f596dc55 Mon Sep 17 00:00:00 2001 From: Ben Grass Date: Mon, 29 Jan 2024 23:22:32 -0500 Subject: [PATCH 16/16] Add detailed comments to ArticlesController, routes, and Article model --- app/controllers/articles_controller.rb | 65 ++++++++++++++++++++++++-- app/models/article.rb | 13 ++++-- config/routes.rb | 4 +- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 75b3f3e24..512c64644 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,52 +1,107 @@ class ArticlesController < ApplicationController + + # get / or /articles. + # Fetches articles based on search parameters and assigns them to an instance variable. + # + # @return [void] def index - @articles = if params[:search] + @articles = if params[:search] # search if search there's a search parameter Article.search(params[:search]) else + # return all articles Article.all end end + # get /articles/new. + # Assigns a new instance of Article to an instance variable. + # + # @return [void] def new @article = Article.new end + # post /articles. + # Creates a new article. + # + # @return [void] def create @article = Article.new(article_params) + + # redirect to article if saved successfully if @article.save redirect_to @article else + # if there's an error return to new post page with unprocessable entity status render :new, status: :unprocessable_entity end end + # get /articles/:id. + # Fetches and assigns an article to an instance variable. + # + # @param :id [Integer] article ID + # + # @return [void] def show @article = Article.find(params[:id]) end + # get /articles/:id/edit. + # Fetches and assigns an article to an instance variable for editing. + # + # @param :id [Integer] The ID of the article to edit. + # + # @return [void] def edit @article = Article.find(params[:id]) end + # put or patch /articles/:id. + # Update an article. + # + # Updates an article with the specified ID using the given parameters. + # If the update is successful, redirects to the updated article. + # If the update fails, renders the edit template with an error status. + # + # @param :id [Integer] The ID of the article to update. + # @param :article_params [ActionController::Parameters] The parameters to update the article from form input. + # + # @return [void] def update @article = Article.find(params[:id]) + # if update is completed, redirect if @article.update(article_params) redirect_to @article else + # if there's a processing error, return to edit with unprocessable entity code. render :edit, status: :unprocessable_entity end end + # delete /articles/:id. + # Destroys an article and redirects to the root path. + # + # @param :id [Integer] ID of article to delete. + # + # @return [void] def destroy @article = Article.find(params[:id]) @article.destroy - redirect_to root_path, status: :see_other + redirect_to root_path, status: :see_other # redirect to root path with error code 303 end private - def article_params - params.require(:article).permit(:title, :content, :author, :date) - end + + # Strong parameters method for filtering and permitting article parameters. + # + # @return [ActionController::Parameters] parameters object with the filtered and permitted parameters. + # + def article_params + # filters parameters based on type so data works with model + params.require(:article).permit(:title, :content, :author, :date) + + end end diff --git a/app/models/article.rb b/app/models/article.rb index 2bb385cbd..9b0200630 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,14 +1,19 @@ class Article < ApplicationRecord - before_validation :default_values - validates :title, :content, :author, :date, presence: true + before_validation :default_values # check if default values are required before validation + validates :title, :content, :author, :date, presence: true # require all fields + # Sets author to 'Anonymous' if it is not already set def default_values unless self.author? - self.author = 'Anonymous' + self.author = 'Anonymous' # set author to 'Anonymous' if not set end - self.date ||= Date.today + self.date ||= Date.today # if date is nil set to Date.today end + # Searches for articles that have a matching title or content. + # + # @param search [String] The search query. + # @return [ActiveRecord::Relation
    ] An ActiveRecord relation of articles that match the search query. def self.search(search) where("title LIKE ? or content LIKE ?", "%#{search}%", "%#{search}%") end diff --git a/config/routes.rb b/config/routes.rb index 6806a2561..45b861a76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ Rails.application.routes.draw do - root "articles#index" - resources :articles + root "articles#index" # set root to direct to index method of articles_controller + resources :articles # sets up routes for articles automatically end