From 8fcf78dab514469f8328c7c1a42d2b412728f289 Mon Sep 17 00:00:00 2001 From: Safa Date: Mon, 29 Jan 2024 22:30:57 -0500 Subject: [PATCH 1/2] Intialized database --- db/migrate/20240129221503_create_articles.rb | 12 ++++++++++ db/schema.rb | 24 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 db/migrate/20240129221503_create_articles.rb create mode 100644 db/schema.rb diff --git a/db/migrate/20240129221503_create_articles.rb b/db/migrate/20240129221503_create_articles.rb new file mode 100644 index 00000000..84add4e4 --- /dev/null +++ b/db/migrate/20240129221503_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 00000000..57249864 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,24 @@ +# 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_221503) 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 0080d069d5e6ddef625152c15d1c1bdb0e9896dc Mon Sep 17 00:00:00 2001 From: Safa Date: Mon, 29 Jan 2024 23:53:27 -0500 Subject: [PATCH 2/2] Implemented CRUD --- app/controllers/application_controller.rb | 1 + app/controllers/articles_controller.rb | 59 +++++++++++++++++++++++ app/helpers/articles_helper.rb | 2 + app/models/article.rb | 12 +++++ app/views/articles/_form.html.erb | 33 +++++++++++++ app/views/articles/edit.html.erb | 3 ++ app/views/articles/index.html.erb | 19 ++++++++ app/views/articles/new.html.erb | 3 ++ app/views/articles/show.html.erb | 12 +++++ config/routes.rb | 9 +++- db/schema.rb | 4 -- test/fixtures/posts.yml | 9 ++++ test/models/post_test.rb | 7 +++ 13 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 app/controllers/articles_controller.rb create mode 100644 app/helpers/articles_helper.rb create mode 100644 app/models/article.rb create mode 100644 app/views/articles/_form.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/new.html.erb create mode 100644 app/views/articles/show.html.erb create mode 100644 test/fixtures/posts.yml create mode 100644 test/models/post_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d12..fdd4b2f6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,3 @@ class ApplicationController < ActionController::Base + end diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 00000000..f994b8ec --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,59 @@ +class ArticlesController < ApplicationController + + def index + @articles = Article.all + end + + # Show a specific article + def show + @article = Article.find(params[:id]) + end + + def new + @article = Article.new + end + + def create + @article = Article.new(article_params) + if @article.save + redirect_to @article + else + render 'new' + end + end + + def destroy + @article = Article.find(params[:id]) + @article.destroy + redirect_to root_path, status: :see_other + end + + def edit + @article = Article.find(params[:id]) + end + + def update + @article = Article.find(params[:id]) + if @article.update(article_params) + flash[:notice] = "Article was updated" + redirect_to @article + else + flash[:notice] = "Article was not updated" + render 'edit' + end + end + + def search + if params[:query].present? + @articles = Article.search(params[:query]) + else + @articles = Article.none + end + end + + private + def article_params + params.require(:article).permit(:title, :content, :author, :date) + end + +end \ No newline at end of file diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 00000000..c0fa1c1c --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end \ No newline at end of file diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 00000000..792444ce --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,12 @@ +class Article < ApplicationRecord + + # We require Title and Content. Author and Date are not necessary. + + validates :title, presence: true + validates :content, presence: true + + + def self.search(query) + where("title LIKE ? OR content LIKE ?", "%#{query}%", "%#{query}%") + end +end diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 00000000..b5248668 --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,33 @@ +<%= form_with model: @article do |form| %> +
+ <%= form.label :title %> + <%= form.text_field :title %> + <% article.errors.full_messages_for(:title).each do |msg| %> + <% end %> +
+ +
+ <%= form.label :author %> + <%= form.text_field :author %> + <% article.errors.full_messages_for(:author).each do |msg| %> + <% end %> +
+ +
+ <%= form.label :content %> + <%= form.text_area :content %> + <% article.errors.full_messages_for(:content).each do |msg| %> + <% end %> +
+ +
+ <%= form.label :date %> + <%= form.date_field :date, value: Date.today %> +
+ +
+ <%= form.submit %> +
+<% end %> + + diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb new file mode 100644 index 00000000..9f2627be --- /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/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 00000000..6376f66f --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,19 @@ + +

Encyclopedia

+ +<%= form_tag "/search", method: "GET" do %> + <%= label_tag :search%> + <%=text_field_tag :search, params[:search]%> + <%= submit_tag "Search For Article" %> +<% end %> + + + +<%= link_to "Create A New Article", new_article_path %> + diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb new file mode 100644 index 00000000..35a13ce4 --- /dev/null +++ b/app/views/articles/new.html.erb @@ -0,0 +1,3 @@ +

Create A New Article

+ +<%= render "form", article: @article%> diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 00000000..55c6c184 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,12 @@ +

Showing Article

+

Title: <%= @article.title %>

+

Content: <%= @article.content %>

+

Author: <%= @article.author %>

+

Date: <%= @article.date %>

+ + + +<%= link_to "Home", root_path %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a125ef08..86b78b90 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,17 @@ Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + # Defines the root path route ("/") + root "articles#index" + + # Map to the controller action for CRUD execution + resources :articles + + get '/search', to: "articles#search" + # 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 diff --git a/db/schema.rb b/db/schema.rb index 57249864..0b23a4e4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -14,11 +14,7 @@ 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/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 00000000..4854ae74 --- /dev/null +++ b/test/fixtures/posts.yml @@ -0,0 +1,9 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + content: MyText + +two: + title: MyString + content: MyText diff --git a/test/models/post_test.rb b/test/models/post_test.rb new file mode 100644 index 00000000..ff155c49 --- /dev/null +++ b/test/models/post_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end