From 6e63af67e033e120956f6ad64beda094667c019c Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:06:04 -0800 Subject: [PATCH 01/18] add view to render articles --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index a125ef08..dd0827ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ # 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 + get "/articles", to: "articles#index" + root "articles#index" # Defines the root path route ("/") # root "posts#index" From d9f5a9e9a0f396791447841b0d58f1145b22f13c Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:06:11 -0800 Subject: [PATCH 02/18] create articles controller --- app/controllers/articles_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/controllers/articles_controller.rb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 00000000..e5904fd2 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,4 @@ +class ArticlesController < ApplicationController + def index + end +end From 598ed5eba6c84472f93926faec3f9cd2c65d9b9f Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:06:54 -0800 Subject: [PATCH 03/18] create article model --- app/models/article.rb | 2 ++ test/fixtures/articles.yml | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 app/models/article.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 00000000..b7a72b58 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,2 @@ +class Article < ApplicationRecord +end diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 00000000..364e9164 --- /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 18:59:05 + +two: + title: MyString + content: MyString + author: MyString + date: 2024-01-29 18:59:05 From 268e4e7233f90ade37952a236bb891f565499087 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:07:01 -0800 Subject: [PATCH 04/18] create article DB migration --- db/migrate/20240130025832_create_articles.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 db/migrate/20240130025832_create_articles.rb diff --git a/db/migrate/20240130025832_create_articles.rb b/db/migrate/20240130025832_create_articles.rb new file mode 100644 index 00000000..4df19bef --- /dev/null +++ b/db/migrate/20240130025832_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.datetime :date + + t.timestamps + end + end +end From 5a2f02687705d7af99d0faf6d6314738de9c4e9a Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:07:28 -0800 Subject: [PATCH 05/18] create article controller test file --- test/controllers/articles_controller_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 test/controllers/articles_controller_test.rb diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 00000000..0a82cbb1 --- /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 69ccce08cfb0d94e8680f2ef616b898f88514127 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:07:34 -0800 Subject: [PATCH 06/18] create articles helper module --- app/helpers/articles_helper.rb | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 app/helpers/articles_helper.rb diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 00000000..29682775 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end From db5345fc3b7c8656934fcc46c112f03115cf6cbb Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:07:55 -0800 Subject: [PATCH 07/18] add mock HTML content for articles view --- app/views/articles/index.html.erb | 1 + 1 file changed, 1 insertion(+) create mode 100644 app/views/articles/index.html.erb diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 00000000..a61abe3b --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1 @@ +

HELLO RAILS

\ No newline at end of file From 72f73f419eca275b3d8df2602417a3a37d9a4e74 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:08:53 -0800 Subject: [PATCH 08/18] create db schema file --- db/schema.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..673f8a4b --- /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_30_025832) do + create_table "articles", force: :cascade do |t| + t.string "title" + t.string "content" + t.string "author" + t.datetime "date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From 530e9738b916f0bc0bfab02e38f458b98f0ad0e5 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:57:45 -0800 Subject: [PATCH 09/18] implement class method search() --- app/models/article.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/article.rb b/app/models/article.rb index b7a72b58..155cc9b8 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,2 +1,5 @@ class Article < ApplicationRecord + def self.search( query_string ) + Article.where(["title LIKE ? OR content LIKE ?", "%#{query_string}%", "%#{query_string}%"]) + end end From af6bcb68ca8281badf9fad2bde275615ce6e7a6f Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:57:58 -0800 Subject: [PATCH 10/18] render all article titles for now --- app/views/articles/index.html.erb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index a61abe3b..badf9b49 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1 +1,9 @@ -

HELLO RAILS

\ No newline at end of file +

HELLO RAILS

+ +
    + <% @articles.each do |article| %> +
  • + <%= article.title %> +
  • + <% end %> +
\ No newline at end of file From e825ee4e15a1597fa1e421306225c0c9f39c96fa Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 19:58:17 -0800 Subject: [PATCH 11/18] try calling Article.search in the controller --- app/controllers/articles_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index e5904fd2..744fdae2 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,4 +1,8 @@ class ArticlesController < ApplicationController def index + @articles = Article.all + end + def search + @articles = Article.search end end From 1fea531ce8a6187d6f3861973ded81705ab12532 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:04:33 -0800 Subject: [PATCH 12/18] add route for search endpoint --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index dd0827ac..ce420ee8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ get "up" => "rails/health#show", as: :rails_health_check get "/articles", to: "articles#index" root "articles#index" + get "/search", to: "articles#search" # Defines the root path route ("/") # root "posts#index" From 3151397c989cc9f3670ac6b9a960123b2b2f574b Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:04:42 -0800 Subject: [PATCH 13/18] render results of mock query --- app/controllers/articles_controller.rb | 3 ++- app/views/articles/search.html.erb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 app/views/articles/search.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 744fdae2..4fa64758 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -3,6 +3,7 @@ def index @articles = Article.all end def search - @articles = Article.search + @mock_query = "BR" + @articles = Article.search(@mock_query) end end diff --git a/app/views/articles/search.html.erb b/app/views/articles/search.html.erb new file mode 100644 index 00000000..6b28550b --- /dev/null +++ b/app/views/articles/search.html.erb @@ -0,0 +1,9 @@ +

Some results for: <%= @mock_query %>

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

    Title: <%= article.title %>

    +

    Content: <%= article.content %>

    +
  • + <% end %> +
\ No newline at end of file From f0f539d847acd39f9d3792587fbed702541e5e75 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:18:12 -0800 Subject: [PATCH 14/18] start with empty fixture --- test/fixtures/articles.yml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml index 364e9164..a9050390 100644 --- a/test/fixtures/articles.yml +++ b/test/fixtures/articles.yml @@ -1,13 +1 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -one: - title: MyString - content: MyString - author: MyString - date: 2024-01-29 18:59:05 - -two: - title: MyString - content: MyString - author: MyString - date: 2024-01-29 18:59:05 From 64fcc3f372fe72ecd10445008f503afdfec92c5f Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:21:13 -0800 Subject: [PATCH 15/18] add comment, fix formatting --- app/models/article.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index 155cc9b8..611abf01 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,5 +1,12 @@ class Article < ApplicationRecord - def self.search( query_string ) - Article.where(["title LIKE ? OR content LIKE ?", "%#{query_string}%", "%#{query_string}%"]) - end + def self.search( query_string ) + # perform SQL query to find articles where + # either its title OR contents contains + # the query_string as a substring + Article.where([ + "title LIKE ? OR content LIKE ?", + "%#{query_string}%", + "%#{query_string}%" + ]) + end end From 37e193917845d245db30d7994bbdf3883c1a448c Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:44:36 -0800 Subject: [PATCH 16/18] add search functionality in HTML page --- app/controllers/articles_controller.rb | 12 ++++++++++-- app/views/articles/index.html.erb | 10 ++++++++-- app/views/articles/search.html.erb | 2 +- config/routes.rb | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 4fa64758..eab2d045 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -1,9 +1,17 @@ class ArticlesController < ApplicationController + @@query_string = "" + def index @articles = Article.all end + def submit_query + @@query_string = params["query_string"] + print @@query_string + redirect_to "/search", query_string: params["query_string"] + end def search - @mock_query = "BR" - @articles = Article.search(@mock_query) + @query_string = @@query_string + print "SEARCH: " + @@query_string + @articles = Article.search(@@query_string) end end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index badf9b49..777d436f 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -1,4 +1,10 @@ -

HELLO RAILS

+

Shopify Internship assessment - Encyclopedia!!

+ +

Search for articles:

+
+ + +
    <% @articles.each do |article| %> @@ -6,4 +12,4 @@ <%= article.title %> <% end %> -
\ No newline at end of file + diff --git a/app/views/articles/search.html.erb b/app/views/articles/search.html.erb index 6b28550b..b2ed94e5 100644 --- a/app/views/articles/search.html.erb +++ b/app/views/articles/search.html.erb @@ -1,4 +1,4 @@ -

Some results for: <%= @mock_query %>

+

Some results for: "<%= @query_string %>"

    <% @articles.each do |article| %>
  • diff --git a/config/routes.rb b/config/routes.rb index ce420ee8..a77098e2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ get "/articles", to: "articles#index" root "articles#index" get "/search", to: "articles#search" + post "/submit_query", to: "articles#submit_query" # Defines the root path route ("/") # root "posts#index" From c3ffa4690db7f94e7e9b457b8b27150e5ca023d2 Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:48:06 -0800 Subject: [PATCH 17/18] create endpoint to view single article --- app/controllers/articles_controller.rb | 3 +++ app/views/articles/show.html.erb | 3 +++ config/routes.rb | 1 + 3 files changed, 7 insertions(+) create mode 100644 app/views/articles/show.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index eab2d045..b4b88d72 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -14,4 +14,7 @@ def search print "SEARCH: " + @@query_string @articles = Article.search(@@query_string) end + def show + @article = Article.find(params[":id"]) + end end diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 00000000..c96409a3 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,3 @@ +

    <%= @article.title %>

    + +

    <%= @article.body %>

    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a77098e2..18f4b290 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ root "articles#index" get "/search", to: "articles#search" post "/submit_query", to: "articles#submit_query" + get "/articles/:id", to: "articles#show" # Defines the root path route ("/") # root "posts#index" From 847e52e81a65d7bed615d8e1f1e5dad92d7945eb Mon Sep 17 00:00:00 2001 From: dingdongg Date: Mon, 29 Jan 2024 20:56:52 -0800 Subject: [PATCH 18/18] add functionality to update existing articles --- app/controllers/articles_controller.rb | 10 +++++++++- app/views/articles/show.html.erb | 9 ++++++++- config/routes.rb | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index b4b88d72..6b81fadf 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -15,6 +15,14 @@ def search @articles = Article.search(@@query_string) end def show - @article = Article.find(params[":id"]) + @article = Article.find(params[:id]) + end + def update + @article = Article.find(params[:id]) + @article.update( + title: params["new_article_title"], + content: params["new_article_contents"] + ) + redirect_to "/articles/" + params[:id] end end diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index c96409a3..7a292c4c 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -1,3 +1,10 @@

    <%= @article.title %>

    -

    <%= @article.body %>

    \ No newline at end of file +

    <%= @article.content %>

    + +

    Submit this form to update article:

    +
    + + + +
    diff --git a/config/routes.rb b/config/routes.rb index 18f4b290..77f5312d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ get "/search", to: "articles#search" post "/submit_query", to: "articles#submit_query" get "/articles/:id", to: "articles#show" + post "/articles/:id/update", to: "articles#update" # Defines the root path route ("/") # root "posts#index"