From 9cd466534a362f64fba32ce58e73ab2ccf32c8b2 Mon Sep 17 00:00:00 2001 From: sarabah Date: Sat, 27 Jan 2024 16:42:40 -0800 Subject: [PATCH 01/12] feat: create articles controller and set application home page --- app/controllers/articles_controller.rb | 4 ++++ app/helpers/articles_helper.rb | 2 ++ app/views/articles/index.html.erb | 2 ++ config/routes.rb | 3 ++- test/controllers/articles_controller_test.rb | 7 +++++++ 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/controllers/articles_controller.rb create mode 100644 app/helpers/articles_helper.rb create mode 100644 app/views/articles/index.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..e5904fd2e --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,4 @@ +class ArticlesController < ApplicationController + def index + 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/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 000000000..0c90ed3de --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,2 @@ +

Hello Rails

+ diff --git a/config/routes.rb b/config/routes.rb index a125ef085..296a3911f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,5 +6,6 @@ get "up" => "rails/health#show", as: :rails_health_check # Defines the root path route ("/") - # root "posts#index" + root "articles#index" + get "/articles", to: "articles#index" end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 000000000..0a82cbb19 --- /dev/null +++ b/test/controllers/articles_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ArticlesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From 6824d335ebc7eed33c825e953d140c010b2dd20e Mon Sep 17 00:00:00 2001 From: sarabah Date: Sat, 27 Jan 2024 16:53:31 -0800 Subject: [PATCH 02/12] feat: generated Article model and db migration --- app/models/article.rb | 2 ++ db/migrate/20240128005115_create_articles.rb | 12 ++++++++++ db/schema.rb | 23 ++++++++++++++++++++ test/fixtures/articles.yml | 13 +++++++++++ 4 files changed, 50 insertions(+) create mode 100644 app/models/article.rb create mode 100644 db/migrate/20240128005115_create_articles.rb create mode 100644 db/schema.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/20240128005115_create_articles.rb b/db/migrate/20240128005115_create_articles.rb new file mode 100644 index 000000000..84add4e46 --- /dev/null +++ b/db/migrate/20240128005115_create_articles.rb @@ -0,0 +1,12 @@ +class CreateArticles < ActiveRecord::Migration[7.1] + def change + create_table :articles do |t| + t.string :title + t.text :content + t.string :author + t.date :date + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..0577feeb0 --- /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_28_005115) 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 diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 000000000..031844fff --- /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: MyText + author: MyString + date: 2024-01-27 + +two: + title: MyString + content: MyText + author: MyString + date: 2024-01-27 From 2c61e3b28ee9429abdc2c4b085b0caa0b0525105 Mon Sep 17 00:00:00 2001 From: sarabah Date: Sat, 27 Jan 2024 17:20:25 -0800 Subject: [PATCH 03/12] feat: read and display a single article --- app/controllers/articles_controller.rb | 6 ++++++ app/views/articles/index.html.erb | 11 ++++++++++- app/views/articles/show.html.erb | 8 ++++++++ config/routes.rb | 4 +++- 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 app/views/articles/show.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e5904fd2e..f2fc70f0a 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,4 +1,10 @@ class ArticlesController < ApplicationController def index + @articles = Article.all + end + + # show a single article + def show + @article = Article.find(params[:id]) end end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 0c90ed3de..851441996 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,2 +1,11 @@ -

Hello Rails

+

Articles

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

<%= @article.title %>

+ +

<%= @article.content %>

+ +

By: <%= @article.author %>

+ +

<%= @article.date %>

+ diff --git a/config/routes.rb b/config/routes.rb index 296a3911f..e327deb02 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,5 +7,7 @@ # Defines the root path route ("/") root "articles#index" - get "/articles", to: "articles#index" + + # resourceful routing - maps all conventional routes for Articles + resources :articles end From 76acfd79f4d9787dabada6a034e91ec1aad9aec0 Mon Sep 17 00:00:00 2001 From: sarabah Date: Sat, 27 Jan 2024 20:49:58 -0800 Subject: [PATCH 04/12] feat: ability to create a new article with strong params and validation --- app/controllers/articles_controller.rb | 20 +++++++++++++ app/models/article.rb | 4 +++ app/views/articles/index.html.erb | 6 ++-- app/views/articles/new.html.erb | 39 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/views/articles/new.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index f2fc70f0a..50c5d3594 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -7,4 +7,24 @@ def index def show @article = Article.find(params[:id]) end + + # create a new article + 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 + + 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..ad6078bb5 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,2 +1,6 @@ class Article < ApplicationRecord + validates :title, presence: true, length: {minimum: 1 } + validates :content, presence: true, length: { minimum: 10 } + validates :author, presence: true, length: { minimum: 1 } + validates :date, presence: true end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 851441996..7b3e4e8e6 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -3,9 +3,9 @@ + +<%= link_to "New Article", new_article_path %> \ No newline at end of file diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 000000000..41f844eec --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,39 @@ +

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 %> + <% @article.errors.full_messages_for(:author).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.label :date %>
+ <%= form.date_select :date %> + <% @article.errors.full_messages_for(:date).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file From 8bde8cdf3acf396113cbd0dcf824510cef357197 Mon Sep 17 00:00:00 2001 From: sarabah Date: Sat, 27 Jan 2024 21:33:19 -0800 Subject: [PATCH 05/12] feat: ability to edit an article + refactord to use partial form --- app/controllers/articles_controller.rb | 14 ++++++++++ app/views/articles/_form.html.erb | 37 +++++++++++++++++++++++++ app/views/articles/edit.html.erb | 3 ++ app/views/articles/new.html.erb | 38 +------------------------- app/views/articles/show.html.erb | 4 +++ 5 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 app/views/articles/_form.html.erb create mode 100644 app/views/articles/edit.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 50c5d3594..1fc2dd00d 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -23,6 +23,20 @@ def create end 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 + private def article_params params.require(:article).permit(:title, :content, :author, :date) diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 000000000..33134e073 --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,37 @@ +<%= 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 %> + <% article.errors.full_messages_for(:author).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.label :date %>
+ <%= form.date_select :date %> + <% article.errors.full_messages_for(:date).each do |message| %> +
<%= message %>
+ <% end %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 000000000..e5ca19800 --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,3 @@ +

Edit Article

+ +<%= 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 41f844eec..1daeb7f04 100644 --- a/app/views/articles/new.html.erb +++ b/app/views/articles/new.html.erb @@ -1,39 +1,3 @@

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 %> - <% @article.errors.full_messages_for(:author).each do |message| %> -
<%= message %>
- <% end %> -
- -
- <%= form.label :date %>
- <%= form.date_select :date %> - <% @article.errors.full_messages_for(:date).each do |message| %> -
<%= message %>
- <% end %> -
- -
- <%= form.submit %> -
-<% end %> \ No newline at end of file +<%= render "form", article: @article %> \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 12a7b0bce..4988a53b7 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -6,3 +6,7 @@

<%= @article.date %>

+
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
+ From fea16b45501b6d290e5b56c8c31c490690d02bc1 Mon Sep 17 00:00:00 2001 From: sarabah Date: Sat, 27 Jan 2024 21:38:41 -0800 Subject: [PATCH 06/12] feat: ability to destroy article --- app/controllers/articles_controller.rb | 7 +++++++ app/views/articles/show.html.erb | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 1fc2dd00d..5edcb7153 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -37,6 +37,13 @@ def update end end + def destroy + @article = Article.find(params[:id]) + @article.destroy + + redirect_to root_path, status: :see_other + end + private def article_params params.require(:article).permit(:title, :content, :author, :date) diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 4988a53b7..48ef7f637 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -8,5 +8,8 @@
  • <%= 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 f8a3853bc7ff878d286595b08901e9b16fc915d1 Mon Sep 17 00:00:00 2001 From: sarabah Date: Mon, 29 Jan 2024 17:35:51 -0800 Subject: [PATCH 07/12] feat: integrated bootstrap --- .github/workflows/tests.yml | 2 +- .ruby-version | 2 +- Gemfile | 2 +- Gemfile.lock | 4 ++-- app/controllers/articles_controller.rb | 3 +++ app/views/layouts/application.html.erb | 15 ++++++++++----- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 29d07fe5e..cd032554a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: 3.2.3 + ruby-version: 3.3.0 bundler-cache: true - name: Run tests diff --git a/.ruby-version b/.ruby-version index b347b11ea..15a279981 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.2.3 +3.3.0 diff --git a/Gemfile b/Gemfile index 7abd44d7a..28697d2a0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "https://rubygems.org" -ruby "3.2.3" +ruby "3.3.0" # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.1.2" diff --git a/Gemfile.lock b/Gemfile.lock index ac898c77c..b897baea6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -238,7 +238,7 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) - webdrivers (5.3.1) + webdrivers (5.3.0) nokogiri (~> 1.6) rubyzip (>= 1.3.0) selenium-webdriver (~> 4.0, < 4.11) @@ -276,7 +276,7 @@ DEPENDENCIES webdrivers RUBY VERSION - ruby 3.2.3p157 + ruby 3.3.0p0 BUNDLED WITH 2.5.5 diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 5edcb7153..7556e9abf 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -13,6 +13,7 @@ def new @article = Article.new end + # post a new article def create @article = Article.new(article_params) @@ -27,6 +28,7 @@ def edit @article = Article.find(params[:id]) end + # updates existing article def update @article = Article.find(params[:id]) @@ -37,6 +39,7 @@ def update end end + # destroys an article def destroy @article = Article.find(params[:id]) @article.destroy diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 812bfb90f..939b4c587 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,16 +1,21 @@ - - + + + + EngInternAssessmentRails - + <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> <%= javascript_importmap_tags %> - +
    +
    <%= yield %> +
    + - + \ No newline at end of file From 5b1c2402d5a0c8f1ca085cd18c937d5ce5f5f7c5 Mon Sep 17 00:00:00 2001 From: sarabah Date: Mon, 29 Jan 2024 17:50:41 -0800 Subject: [PATCH 08/12] feat: add navbar using bootstrap --- .gitignore | 1 + app/views/home/_navbar.html.erb | 22 ++++++++++++++++++++++ app/views/layouts/application.html.erb | 1 + 3 files changed, 24 insertions(+) create mode 100644 app/views/home/_navbar.html.erb diff --git a/.gitignore b/.gitignore index 5fb66c9f0..c41a2d39a 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ # Ignore master key for decrypting credentials and more. /config/master.key +.DS_Store diff --git a/app/views/home/_navbar.html.erb b/app/views/home/_navbar.html.erb new file mode 100644 index 000000000..d5cc1eae1 --- /dev/null +++ b/app/views/home/_navbar.html.erb @@ -0,0 +1,22 @@ + \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 939b4c587..03dae657e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -12,6 +12,7 @@ <%= javascript_importmap_tags %> + <%= render 'home/navbar'%>

    <%= yield %> From 112d20e2ce2ed181692ca96a1b9e8a185f711d71 Mon Sep 17 00:00:00 2001 From: sarabah Date: Mon, 29 Jan 2024 18:58:10 -0800 Subject: [PATCH 09/12] feat: implement search functionality --- app/controllers/articles_controller.rb | 6 ++++++ app/views/articles/search.html.erb | 11 +++++++++++ app/views/articles/show.html.erb | 1 + app/views/home/_navbar.html.erb | 9 +++++---- config/routes.rb | 3 +++ 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 app/views/articles/search.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 7556e9abf..3e53f0867 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -3,6 +3,12 @@ def index @articles = Article.all end + # searches for an article + def search + @articles = Article.where("title LIKE ? OR content LIKE ? OR author LIKE ?", + "%" + params[:q] + "%", "%" + params[:q] + "%", "%" + params[:q] + "%") + end + # show a single article def show @article = Article.find(params[:id]) diff --git a/app/views/articles/search.html.erb b/app/views/articles/search.html.erb new file mode 100644 index 000000000..acb53c445 --- /dev/null +++ b/app/views/articles/search.html.erb @@ -0,0 +1,11 @@ +

    Search Results

    + +<% if @articles.present? %> +
      + <% @articles.each do |article| %> +
    • <%= link_to article.title, article_path(article) %>
    • + <% end %> +
    +<% else %> +

    No articles found. Please try another search.

    +<% end %> \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 48ef7f637..2d88f3267 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -12,4 +12,5 @@ turbo_method: :delete, turbo_confirm: "Are you sure?" } %> +
  • <%= link_to "Home", "/" %>
  • \ No newline at end of file diff --git a/app/views/home/_navbar.html.erb b/app/views/home/_navbar.html.erb index d5cc1eae1..149b167c7 100644 --- a/app/views/home/_navbar.html.erb +++ b/app/views/home/_navbar.html.erb @@ -1,7 +1,8 @@