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

Feature: Encyclopedia CRUD Actions Implementation [Emaan Khan] #294

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions app/controllers/articles_controller.rb
@@ -0,0 +1,68 @@
# The ArticlesController class is responsible for handling requests related to articles.
class ArticlesController < ApplicationController
# Retrieves all articles from the database and assigns them to the @articles instance variable.
def index
@articles = Article.all
end

# Retrieves a specific article from the database based on the provided ID and assigns it to the @article instance variable.
def show
@article = Article.find(params[:id])
end

# Initializes a new instance of the Article model and assigns it to the @article instance variable.
def new
@article = Article.new
end

# Creates a new article with the provided parameters and saves it to the database.
# If the article is successfully saved, redirects to the show page of the created article.
# Otherwise, renders the new page with a status of unprocessable entity.
def create
@article = Article.create(article_params.merge(:date => Date.today))

if @article.save
redirect_to @article
else
render :new, status: :unprocessable_entity
end
end

# Updates an existing article with the provided parameters and saves it to the database.
# If the article is successfully updated, redirects to the show page of the updated article.
# Otherwise, renders the edit page with a status of unprocessable entity.
def update
@article = Article.find(params[:id])

if @article.update(article_params.merge(:date => Date.today))
redirect_to @article
else
render :edit, status: :unprocessable_entity
end
end

# Retrieves an existing article from the database based on the provided ID and assigns it to the @article instance variable.
def edit
@article = Article.find(params[:id])
end

# Deletes an existing article from the database based on the provided ID.
# After deletion, redirects to the root path with a status of see other.
def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to root_path, status: :see_other
end

# Searches for articles based on the provided query and assigns the result to the @article instance variable.
def search
@article = Article.search(params[:query])
end

private
# Defines the permitted parameters for creating or updating an article.
def article_params
params.require(:article).permit(:title, :content, :author)
end
end
2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
@@ -0,0 +1,2 @@
module ArticlesHelper
end
16 changes: 16 additions & 0 deletions app/models/article.rb
@@ -0,0 +1,16 @@
# The Article model represents an article in the application.
class Article < ApplicationRecord
# Validates that the title is present.
validates :title, presence: true

# Validates that the content is present.
validates :content, presence: true

# Validates the uniqueness of the title.
validates_uniqueness_of :title

# Searches for articles based on a query string.
def self.search(query)
@article = self.where("title LIKE ? or content LIKE ?", "%#{query}%", "%#{query}%")
end
end
28 changes: 28 additions & 0 deletions app/views/articles/edit.html.erb
@@ -0,0 +1,28 @@
<h1>Edit Article</h1>

<%= form_with model: @article, local: true do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>

<p>
<%= form.label :content %><br>
<%= form.text_area :content %>
</p>

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

<p>
<%= form.label :date %><br>
<%= form.date_select :date %>
</p>

<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back to Article', article_path(@article) %> | <%= link_to 'Back to Articles', articles_path %>
28 changes: 28 additions & 0 deletions app/views/articles/new.html.erb
@@ -0,0 +1,28 @@
<h1>New Article</h1>

<%= form_with model: @article, local: true do |form| %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>

<p>
<%= form.label :content %><br>
<%= form.text_area :content %>
</p>

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

<p>
<%= form.label :date %><br>
<%= form.date_select :date %>
</p>

<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back to Articles', articles_path %>
14 changes: 14 additions & 0 deletions app/views/articles/show.html.erb
@@ -0,0 +1,14 @@
<h1><%= @article.title %></h1>

<p>
<strong>Author:</strong>
<%= @article.author %>
</p>

<p>
<strong>Content:</strong>
<%= @article.content %>
</p>

<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back to Articles', articles_path %>
5 changes: 5 additions & 0 deletions config/routes.rb
Expand Up @@ -7,4 +7,9 @@

# Defines the root path route ("/")
# root "posts#index"

root "articles#index"
get 'articles', to: 'articles#index'
get 'search', to: 'articles#search'
resources :articles
end
11 changes: 11 additions & 0 deletions db/migrate/20240125004458_create_articles.rb
@@ -0,0 +1,11 @@
class CreateArticles < ActiveRecord::Migration[7.1]
def change
create_table :articles do |t|
t.string :title
t.string :content
t.string :author

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240130033615_add_date_to_articles.rb
@@ -0,0 +1,5 @@
class AddDateToArticles < ActiveRecord::Migration[7.1]
def change
add_column :articles, :date, :date
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.

7 changes: 7 additions & 0 deletions db/seeds.rb
Expand Up @@ -7,3 +7,10 @@
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end

# Article.destroy_all

# Article.create(title: 'Sample Article', content: 'Lorem ipsum dolor sit amet.', author: 'John Doe')
# Article.create(title: 'Another Article', content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', author: 'Jane Smith')
# Article.create(title: 'Third Article', content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', author: 'John Doe')
# Article.create(title: 'Fourth Article', content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
7 changes: 7 additions & 0 deletions 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
1 change: 1 addition & 0 deletions test/fixtures/articles.yml
@@ -0,0 +1 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2 changes: 1 addition & 1 deletion test/models/article_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'

class ArticleTest < ActiveSupport::TestCase
test 'starts with no articles' do
test 'starts with no articles' do
assert_equal 0, Article.count
end

Expand Down