Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chenqian-rails-intern-assessment-CRUD-implementations #293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class ArticlesController < ApplicationController
# View all articles
def index
@articles = Article.all
end

# View a single article
def show
@article = Article.find(params[:id])
end

# View the new article form
def new
@article = Article.new
end

# View the edit article form
def edit
@article = Article.find(params[:id])
end

# Create a new article
def create
@article = Article.create(article_params)
@article.save
redirect_to root_path
end

# Update the article
def update
@article = Article.find(params[:id])
@article.update(article_params)
redirect_to root_path
end

# Delete the article
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to root_path, notice: "Article deleted successfully."
end

# Search for articles
def search
key = "%#{params[:key]}%"
@articles = Article.search(params[:key])
render 'search'
end

private
def article_params
params.require(:article).permit(:title, :content, :author, :date)
end
end

2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ArticlesHelper
end
7 changes: 7 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Article < ApplicationRecord
# Search for articles by title or content, based on given test cases
# We can easily add more fields to search by adding more OR conditions based on future requirements
def self.search(key)
where("title LIKE ? OR content LIKE ?", "%#{key}%", "%#{key}%")
end
end
29 changes: 29 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row justify-content-center">
<h4 class="mt-4 mb-4 display-4">Update Article</h4>
</div>
<div class="row justify-content-center">
<%= form_with(model: @article, local: true) do |form| %>
<div class="form-group">
<%= form.label :Title %>
<%= form.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label :Content %>
<%= form.text_area :content, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label :Author %>
<%= form.text_area :author, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label :Date %>
<%= form.text_area :date, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.submit class: 'form-control btn-primary' %>
</div>
<% end %>
</div>
</div>
50 changes: 50 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row justify-content-center">
<h4 class="mt-4 display-4">Articles Home</h4>
</div>
<%= link_to 'Create Article', new_article_path %>
<body>
<form action="/search">
<input placeholder="Search" name="key" type="text">
<button type="submit">Search</button>
</form>
<%= yield %>
</body>

<% if @articles.present? %>
<div class="row justify-content-center">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Content</th>
<th scope="col">Author</th>
<th scope="col">Date</th>
<th scope="col" colspan="3">Action</th>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.content %></td>
<td><%= article.author %></td>
<td><%= article.date %></td>
<td><%= link_to 'Show', article_path(article.id) %></td>
<td><%= link_to 'Edit', edit_article_path(article.id) %></td>
<td><%= link_to 'Delete',
article_path(article.id),
data: { turbo_method: :delete, turbo_confirm: "Are you sure to delete this article?" } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="p-3 mb-2 bg-light text-dark">
<span class="text-info font-weight-bold">Welcome! </span>
<span class="text-info">No articles available. Please create one.</span>
</div>
<% end %>
</div>
34 changes: 34 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row justify-content-center">
<h4 class="mt-4 mb-4 display-4">New Article</h4>
</div>
<div class="row justify-content-center">
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<div class="form-group">
<%= form.label :title %>
<%= form.text_field :title, class: 'form-control' %>
</div>

<div class="form-group">
<%= form.label :content %>
<%= form.text_area :content, class: 'form-control' %>
</div>

<div class="Author">
<%= form.label :author %>
<%= form.text_area :author, class: 'form-control' %>
</div>

<div class="Date">
<div class="field">
<%= form.label :date %>
<%= form.date_select :date, {class: 'form-control'} %>
</div>

<div class="form-group">
<%= form.submit class: 'form-control btn-primary' %>
</div>
<% end %>
</div>
</div>
29 changes: 29 additions & 0 deletions app/views/articles/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<h1>Search Results</h1>
<% if @articles.present? && @articles.any? %>
<div class="row justify-content-center">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Content</th>
<th scope="col">Author</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.content %></td>
<td><%= article.author %></td>
<td><%= article.date %></td>
<% end %>
</tbody>
</table>
</div>
<% else %>
<p>No results found for "<%= params[:key] %>".</p>
<% end %>

<%= link_to "Back to Home", root_path %>
28 changes: 28 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row justify-content-center">
<h4 class="mt-4 mb-4 display-4">Show details of article <%= @article.id %></h4>
</div>
<div class="row justify-content-center">
<table class="table">
<tbody>
<tr>
<th>Title:</th>
<td><%= @article.title %></td>
</tr>
<tr>
<th>Content:</th>
<td><%= @article.content %></td>
</tr>
<tr>
<th>Author:</th>
<td><%= @article.author %></td>
</tr>
<tr>
<th>Date:</th>
<td><%= @article.date %></td>
</tr>
</tbody>
</table>
</div>
</div>
15 changes: 10 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Define the routes for the articles controller
get 'articles/index'
get 'articles/show'
get 'articles/create'
get 'articles/update'
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"
# Define the root URL
root 'articles#index'
# Define the resources for articles
resources :articles
end
12 changes: 12 additions & 0 deletions db/migrate/20240130033254_create_articles.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get articles_index_url
assert_response :success
end

test "should get show" do
get articles_show_url
assert_response :success
end

test "should get new" do
get articles_new_url
assert_response :success
end

test "should get edit" do
get articles_edit_url
assert_response :success
end
end
13 changes: 13 additions & 0 deletions test/fixtures/articles.yml
Original file line number Diff line number Diff line change
@@ -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-29

two:
title: MyString
content: MyText
author: MyString
date: 2024-01-29
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all

# Add more helper methods to be used by all tests here...
# Ensure each test starts with a clean database
setup do
Article.delete_all
end
end
end