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

Shopify internship assessment #310

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

def index
@articles = Article.all
end
def submit_query
@@query_string = params["query_string"]
print @@query_string
redirect_to "/search", query_string: params["query_string"]
end
def search
@query_string = @@query_string
print "SEARCH: " + @@query_string
@articles = Article.search(@@query_string)
end
def show
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
@article.update(
title: params["new_article_title"],
content: params["new_article_contents"]
)
redirect_to "/articles/" + params[:id]
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
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
def self.search( query_string )
# perform SQL query to find articles where
# either its title OR contents contains
# the query_string as a substring
Article.where([
"title LIKE ? OR content LIKE ?",
"%#{query_string}%",
"%#{query_string}%"
])
end
end
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>Shopify Internship assessment - Encyclopedia!!</h1>

<h2>Search for articles: </h2>
<form action="/submit_query" method="post">
<input type="text" value="" name="query_string" />
<button type="submit">Submit</button>
</form>

<ul>
<% @articles.each do |article| %>
<li>
<%= article.title %>
</li>
<% end %>
</ul>
9 changes: 9 additions & 0 deletions app/views/articles/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h2>Some results for: "<%= @query_string %>"</h2>
<ul>
<% @articles.each do |article| %>
<li>
<p>Title: <%= article.title %></p>
<p>Content: <%= article.content %></p>
</li>
<% end %>
</ul>
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>

<p><%= @article.content %></p>

<h4>Submit this form to update article:</h4>
<form action="/update" method="post">
<input type="text" value="" placeholder="New title" name="new_article_title" />
<input type="text" value="" placeholder="New contents" name="new_article_contents" />
<button type="submit">Submit</button>
</form>
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# 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
get "/articles", to: "articles#index"
root "articles#index"
get "/search", to: "articles#search"
post "/submit_query", to: "articles#submit_query"
get "/articles/:id", to: "articles#show"
post "/articles/:id/update", to: "articles#update"

# Defines the root path route ("/")
# root "posts#index"
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240130025832_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.datetime :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
1 change: 1 addition & 0 deletions test/fixtures/articles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html