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

[Sean Li] Feature Completion: Encyclopedia Article Management (Shopify Backend Internship Challenge) #288

Open
wants to merge 3 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
Empty file added 3.0
Empty file.
27 changes: 5 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
mini_mime (1.1.5)
mini_portile2 (2.8.5)
minitest (5.21.2)
msgpack (1.7.2)
mutex_m (0.2.0)
Expand All @@ -142,16 +141,7 @@ GEM
net-smtp (0.4.0.1)
net-protocol
nio4r (2.7.0)
nokogiri (1.16.0)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.16.0-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.0-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.0-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.0-x86_64-linux)
nokogiri (1.16.0-x64-mingw-ucrt)
racc (~> 1.4)
psych (5.1.2)
stringio
Expand Down Expand Up @@ -216,12 +206,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.7.1)
mini_portile2 (~> 2.8.0)
sqlite3 (1.7.1-aarch64-linux)
sqlite3 (1.7.1-arm64-darwin)
sqlite3 (1.7.1-x86_64-darwin)
sqlite3 (1.7.1-x86_64-linux)
sqlite3 (1.7.1-x64-mingw-ucrt)
stimulus-rails (1.3.3)
railties (>= 6.0.0)
stringio (3.1.0)
Expand All @@ -233,6 +218,8 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2023.4)
tzinfo (>= 1.0.0)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand All @@ -252,11 +239,7 @@ GEM
zeitwerk (2.6.12)

PLATFORMS
aarch64-linux
arm64-darwin
ruby
x86_64-darwin
x86_64-linux
x64-mingw-ucrt

DEPENDENCIES
bootsnap
Expand Down
7 changes: 5 additions & 2 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link application.js
//= link rails-ujs.js
//= link controllers/application.js
//= link controllers/hello_controller.js
//= link controllers/index.js
58 changes: 58 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class ArticlesController < ApplicationController
# Ensures set_article method is called before specified actions to DRY up finding an article by ID
before_action :set_article, only: [:show, :edit, :update, :destroy]

# Lists all articles
def index
@articles = Article.all
end

# Shows a single article, @article is set by set_article method
def show
end

# Initializes a new article instance for the 'new' form
def new
@article = Article.new
end

# Creates a new article from the form parameters
def create
@article = Article.new(article_params)
if @article.save # If the article saves successfully
redirect_to @article, notice: 'Article was successfully created.'
else # If the article doesn't save due to validation failures
render :new
end
end

# Loads the article for editing, @article is set by set_article method
def edit
end

# Updates the article with the form parameters
def update
if @article.update(article_params) # If the article updates successfully
redirect_to @article, notice: 'Article was successfully updated.'
else # If the update fails due to validation errors
render :edit
end
end

# Deletes the article
def destroy
@article.destroy
redirect_to articles_url, notice: 'Article was successfully destroyed.'
end

private
# Sets the @article instance variable from the ID in the parameters
def set_article
@article = Article.find(params[:id])
end

# Strong parameters: whitelists the parameters allowed for article creation/updating
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
2 changes: 2 additions & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
import Rails from "@rails/ujs";
Rails.start();
12 changes: 12 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Article < ApplicationRecord
# Validations
validates :title, presence: true
validates :content, presence: true

# Search functionality
def self.search(query)
where("title LIKE ? OR content LIKE ?", "%#{query}%", "%#{query}%")
end

end

49 changes: 49 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!-- Start the form for creating or editing an article -->
<%= form_with(model: article, local: true) do |form| %>

<!-- Check if there are any validation errors for the article -->
<% if article.errors.any? %>
<!-- Display a block containing the error messages -->
<div id="error_explanation">
<!-- Display the number of errors -->
<h2><%= pluralize(article.errors.count, "error") %> prohibited this article from being saved:</h2>

<!-- List each error message -->
<ul>
<% article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<!-- Field for the article's title -->
<div class="field">
<%= form.label :title %> <!-- Label for the title field -->
<%= form.text_field :title %> <!-- Text input for the title -->
</div>

<!-- Field for the article's content -->
<div class="field">
<%= form.label :content %> <!-- Label for the content field -->
<%= form.text_area :content %> <!-- Textarea for the content -->
</div>

<!-- Field for the article's author -->
<div class="field">
<%= form.label :author %> <!-- Label for the author field -->
<%= form.text_field :author %> <!-- Text input for the author -->
</div>

<!-- Field for the article's publication date -->
<div class="field">
<%= form.label :date %> <!-- Label for the date field -->
<%= form.date_field :date %> <!-- Date picker for the date -->
</div>

<!-- Form submission button -->
<div class="actions">
<%= form.submit %> <!-- Submit button for the form -->
</div>

<% end %> <!-- End of the form block -->
3 changes: 3 additions & 0 deletions app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Edit Article</h1>
<%= render 'form', article: @article %>
<%= link_to 'Back', article_path(@article) %>
42 changes: 42 additions & 0 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!-- Display the heading for the articles listing page -->
<h1>Listing Articles</h1>

<!-- Link to the page for creating a new article -->
<%= link_to 'New Article', new_article_path %>

<!-- Start of the table for listing articles -->
<table>
<!-- Table header -->
<thead>
<tr>
<th>Title</th> <!-- Column heading for article titles -->
<th>Author</th> <!-- Column heading for article authors -->
<th>Date</th> <!-- Column heading for article publication dates -->
<th colspan="3">Actions</th> <!-- Column heading for actions (show, edit, destroy) -->
</tr>
</thead>

<!-- Table body -->
<tbody>
<!-- Loop through each article and display its information in a table row -->
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td> <!-- Display the article's title -->
<td><%= article.author %></td> <!-- Display the article's author -->
<td><%= article.date.strftime('%b %d, %Y') if article.date %></td> <!-- Format and display the article's date -->
<td><%= link_to 'Show', article_path(article) %></td> <!-- Link to view the article's details -->
<td><%= link_to 'Edit', edit_article_path(article) %></td> <!-- Link to edit the article -->
<td>
<!-- Form for deleting an article -->
<%= form_with url: article_path(article), method: :delete, local: true do %>
<!-- Submit button for the form with a data-confirm attribute to prompt for confirmation before deletion -->
<%= submit_tag 'Destroy', data: { confirm: 'Are you sure?' } %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>

<!-- Link to go back to the root path of the application -->
<%= link_to 'Back', root_path %>
3 changes: 3 additions & 0 deletions app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>New Article</h1>
<%= render 'form', article: @article %>
<%= link_to 'Back', articles_path %>
14 changes: 14 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1><%= @article.title %></h1>
<p><%= @article.content %></p>
<p>
<strong>Author:</strong>
<%= @article.author %>
</p>

<p>
<strong>Date:</strong>
<%= @article.date %>
</p>

<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
2 changes: 2 additions & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "@rails/ujs", to: "rails-ujs.js"

4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
# 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

# Article CRUD routes
resources :articles
root 'articles#index'

# Defines the root path route ("/")
# root "posts#index"
end
12 changes: 12 additions & 0 deletions db/migrate/20240130012331_create_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateArticles < ActiveRecord::Migration[6.0]
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.

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

class ArticlesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end