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

Implement CRUD operations #292

Open
wants to merge 16 commits 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
107 changes: 107 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
class ArticlesController < ApplicationController

# get / or /articles.
# Fetches articles based on search parameters and assigns them to an instance variable.
#
# @return [void]
def index
@articles = if params[:search] # search if search there's a search parameter
Article.search(params[:search])
else
# return all articles
Article.all
end
end

# get /articles/new.
# Assigns a new instance of Article to an instance variable.
#
# @return [void]
def new
@article = Article.new
end

# post /articles.
# Creates a new article.
#
# @return [void]
def create
@article = Article.new(article_params)

# redirect to article if saved successfully
if @article.save
redirect_to @article
else
# if there's an error return to new post page with unprocessable entity status
render :new, status: :unprocessable_entity
end
end

# get /articles/:id.
# Fetches and assigns an article to an instance variable.
#
# @param :id [Integer] article ID
#
# @return [void]
def show
@article = Article.find(params[:id])
end

# get /articles/:id/edit.
# Fetches and assigns an article to an instance variable for editing.
#
# @param :id [Integer] The ID of the article to edit.
#
# @return [void]
def edit
@article = Article.find(params[:id])
end

# put or patch /articles/:id.
# Update an article.
#
# Updates an article with the specified ID using the given parameters.
# If the update is successful, redirects to the updated article.
# If the update fails, renders the edit template with an error status.
#
# @param :id [Integer] The ID of the article to update.
# @param :article_params [ActionController::Parameters] The parameters to update the article from form input.
#
# @return [void]
def update
@article = Article.find(params[:id])

# if update is completed, redirect
if @article.update(article_params)
redirect_to @article
else
# if there's a processing error, return to edit with unprocessable entity code.
render :edit, status: :unprocessable_entity
end
end

# delete /articles/:id.
# Destroys an article and redirects to the root path.
#
# @param :id [Integer] ID of article to delete.
#
# @return [void]
def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to root_path, status: :see_other # redirect to root path with error code 303
end

private

# Strong parameters method for filtering and permitting article parameters.
#
# @return [ActionController::Parameters] parameters object with the filtered and permitted parameters.
#
def article_params
# filters parameters based on type so data works with model
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
21 changes: 21 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Article < ApplicationRecord
before_validation :default_values # check if default values are required before validation
validates :title, :content, :author, :date, presence: true # require all fields

# Sets author to 'Anonymous' if it is not already set
def default_values
unless self.author?
self.author = 'Anonymous' # set author to 'Anonymous' if not set
end
self.date ||= Date.today # if date is nil set to Date.today
end

# Searches for articles that have a matching title or content.
#
# @param search [String] The search query.
# @return [ActiveRecord::Relation<Article>] An ActiveRecord relation of articles that match the search query.
def self.search(search)
where("title LIKE ? or content LIKE ?", "%#{search}%", "%#{search}%")
end

end
31 changes: 31 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= form_with model: @article do |form| %>
<div>
<%= form.label :title %><br>
<%= form.text_field :title %>
<%= @article.errors.full_messages_for(:title).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :content %><br>
<%= form.text_area :content %>
<%= @article.errors.full_messages_for(:content).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :author %><br>
<%= form.text_field :author %>
</div>

<div>
<%= form.label :date %><br>
<%= form.text_field :date %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/articles/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Articles#destroy</h1>
<p>Find me in app/views/articles/destroy.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Edit <%= @article.title %></h1>
<%= render "form", article: @article %>
15 changes: 15 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>Articles</h1>
<%= form_with(url: articles_path, method: "get") do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>
<ul>
<% @articles.each do |article| %>
<li>
<a href="<%= article_path(article) %>">
<%= article.title %>
</a>
</li>
<% end %>
</ul>
<%= link_to "Create New Article", new_article_path %>
2 changes: 2 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>New Article</h1>
<%= render "form", article: @article %>
10 changes: 10 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1><%= @article.title %></h1>
<h3><%= @article.author %></h3>
<h4><%= @article.date %></h4>
<p><%= @article.content %></p>

<%= link_to "Edit", edit_article_path(@article) %> <br>
<%= link_to "Destroy", article_path(@article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
10 changes: 2 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# 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"
root "articles#index" # set root to direct to index method of articles_controller
resources :articles # sets up routes for articles automatically
end
12 changes: 12 additions & 0 deletions db/migrate/20240129234034_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.string :content
t.string :author
t.date :date

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240129235228_change_content_to_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeContentToText < ActiveRecord::Migration[7.1]
def change
change_column :articles,
:content, :text
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.

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: MyString
author: MyString
date: 2024-01-29

two:
title: MyString
content: MyString
author: MyString
date: 2024-01-29
5 changes: 5 additions & 0 deletions test/models/article_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require 'test_helper'

class ArticleTest < ActiveSupport::TestCase

setup do
Article.delete_all
end

test 'starts with no articles' do
assert_equal 0, Article.count
end
Expand Down