diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 00000000..9d0cc4d0 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,54 @@ +class ArticlesController < ApplicationController + def index + @articles = Article.all + + # run the search function if the search parameter exists + if params[:search] + @articles = Article.search(params[:search]) + end + end + + 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, status: :unprocessable_entity + 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 + + 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) + end +end 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 diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 00000000..284c0953 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,15 @@ +class Article < ApplicationRecord + validates :title, presence: true + validates :content, presence: true, length: { minimum: 1 } + # No need to validate author or date (both validated by the input fields) + + # function that returns all articles with title or content matching a search query + def self.search(query) + unless query.strip.empty? + where("lower(title) like :searchQuery or lower(content) like :searchQuery", searchQuery: "%#{query.downcase}%") + else + all + end + end + +end diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb new file mode 100644 index 00000000..498b08aa --- /dev/null +++ b/app/views/articles/_form.html.erb @@ -0,0 +1,45 @@ +<%= 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 :author %>
+ <%= form.text_field :author %>
+ <% article.errors.full_messages_for(:author).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 :date %>
+ <%= form.date_field :date %>
+ <% article.errors.full_messages_for(:date).each do |message| %> +
<%= message %>
+ <% end %> +
+ + +
+ + + +
<%= 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..808fb655 --- /dev/null +++ b/app/views/articles/edit.html.erb @@ -0,0 +1,3 @@ +

Edit Article

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

Encyclopedia

+ + +<%= form_tag "/articles", method: "GET" do %> + <%= text_field_tag :search, params[:search] %> <%= submit_tag "Search" %> +<% end %> + + +

Articles

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

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..3c551f16 --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,18 @@ + +

<%= @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 a125ef08..7eb4f422 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,5 +6,7 @@ get "up" => "rails/health#show", as: :rails_health_check # Defines the root path route ("/") - # root "posts#index" + root "articles#index" + + resources :articles end diff --git a/db/migrate/20240129061916_create_articles.rb b/db/migrate/20240129061916_create_articles.rb new file mode 100644 index 00000000..a048b0f9 --- /dev/null +++ b/db/migrate/20240129061916_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.text :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..dff6f3d2 --- /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_061916) do + create_table "articles", force: :cascade do |t| + t.string "title" + t.text "content" + t.text "author" + t.date "date" + + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end 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