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

[Safa Al-Siaudi] Coding Assessment #312

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base

end
59 changes: 59 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
@@ -0,0 +1,2 @@
module ArticlesHelper
end
12 changes: 12 additions & 0 deletions 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
33 changes: 33 additions & 0 deletions app/views/articles/_form.html.erb
@@ -0,0 +1,33 @@
<%= form_with model: @article do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
<% article.errors.full_messages_for(:title).each do |msg| %>
<% end %>
</div>

<div>
<%= form.label :author %>
<%= form.text_field :author %>
<% article.errors.full_messages_for(:author).each do |msg| %>
<% end %>
</div>

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

<div>
<%= form.label :date %>
<%= form.date_field :date, value: Date.today %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>


3 changes: 3 additions & 0 deletions app/views/articles/edit.html.erb
@@ -0,0 +1,3 @@
<h1> Edit Article </h1>

<%= render "form", article: @article %>
19 changes: 19 additions & 0 deletions app/views/articles/index.html.erb
@@ -0,0 +1,19 @@

<h1> Encyclopedia </h1>

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

<ul>
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article %>
</li>
<% end %>
</ul>

<%= link_to "Create A New Article", new_article_path %>

3 changes: 3 additions & 0 deletions app/views/articles/new.html.erb
@@ -0,0 +1,3 @@
<h1>Create A New Article</h1>

<%= render "form", article: @article%>
12 changes: 12 additions & 0 deletions app/views/articles/show.html.erb
@@ -0,0 +1,12 @@
<h1>Showing Article</h1>
<p> Title: <%= @article.title %> </p>
<p> Content: <%= @article.content %> </p>
<p> Author: <%= @article.author %> </p>
<p> Date: <%= @article.date %> </p>

<ul>
<li> <%= link_to "Edit", edit_article_path(@article) %> </li>
<li> <%= link_to "Destroy", article_path(@article), data: {turbo_method: delete} </li>
</ul>

<%= link_to "Home", root_path %>
9 changes: 8 additions & 1 deletion 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
12 changes: 12 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions db/schema.rb

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

9 changes: 9 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions test/models/post_test.rb
@@ -0,0 +1,7 @@
require "test_helper"

class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end