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

Assessment Solution: Add Encyclopedia Article Functionality #300

Open
wants to merge 10 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.3
ruby-version: 3.3.0
bundler-cache: true

- name: Run tests
Expand Down
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"

ruby "3.2.3"
ruby "3.3.0"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.1.2"
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -276,7 +276,7 @@ DEPENDENCIES
webdrivers

RUBY VERSION
ruby 3.2.3p157
ruby 3.3.0p157

BUNDLED WITH
2.5.5
59 changes: 6 additions & 53 deletions README.md
@@ -1,55 +1,8 @@
# Technical Instructions
1. Fork this repo to your local Github account.
2. Create a new branch to complete all your work in.
3. Test your work using the provided tests
4. Create a Pull Request against the Shopify Main branch when you're done and all tests are passing
# Shopify Technical Challenge Submission

# Project Overview
The Rails application you will be working on is an Encyclopedia, which allows users to create, view, edit, and delete articles. The application also provides search functionality to help users find relevant articles. Be sure to implement basic CRUD actions on articles. Your task is to implement these features as well as write the code that makes the tests pass.
I have implemented all of the required CRUD actions, keeping in mind MVC principles. Throughout implementing the functionality, I have made a concious effort to follow best practices to ensure code quality.

# Project Goals
The main goal of this internship project is to implement the functionality required to make the existing tests pass. The provided tests cover various aspects of the application, including creating and viewing articles, editing and updating articles, deleting articles, and searching for articles. Along with completing the tests, be sure to implement all basic CRUD actions on your articles on a controller and create views to see your work in the app.

## Your specific goals for this project are as follows:

1. Review Existing Tests: Start by reviewing the existing tests provided in the article_test.rb file located in the test/models directory. Understand the requirements and expectations of each test.

2. Implement Functionality: Write the code necessary to make the existing tests pass. This involves implementing the required actions and logic in the models, controllers, and views to fulfill the specified requirements. Also be sure to implement basic CRUD actions and demonstrate proper MVC principals.

3. Ensure Code Quality: Write clean, well-structured, and maintainable code. Follow best practices and adhere to the Ruby on Rails conventions. Pay attention to code readability, modularity, and performance.

4. Test Your Code: After implementing the functionality, run the tests to ensure that they pass successfully. Fix any failures or errors that occur and retest until all tests pass.

5. Code Documentation: Document your code by adding comments and explanatory notes where necessary. This will help other developers understand your implementation and make future maintenance easier.

6. Version Control: Use Git for version control. Commit your changes regularly and push them to a branch in your forked repository.

7. Create a Pull Request: Once you have completed the project goals, create a pull request to merge your changes into the main repository. Provide a clear description of the changes made and any relevant information for the code review.

## Getting Started
To get started with this project, follow these steps:

1. Clone the repository to your local development environment.

2. Install the necessary dependencies by running bundle install in the project directory.

3. Familiarize yourself with the existing codebase, including the models, controllers, and views.

4. Review the existing tests in the article_test.rb file and understand their purpose and functionality.

5. Run the tests locally to ensure they are passing.

6. Start working on the goals outlined above, making improvements to the existing tests and adding new tests as needed.

7. Commit your changes regularly and push them to a branch in your forked repository.

8. Once you have completed the project goals, create a pull request to merge your changes into the main repository.

## Resources
Here are some resources that may be helpful during your internship project:

- [Ruby on Rails Guides](https://guides.rubyonrails.org/) - Comprehensive guides on Ruby on Rails, covering various aspects of web application development.

- [Ruby Style Guide](https://rubystyle.guide/) - A community-driven Ruby coding style guide to ensure consistent and readable code.

- [Git Documentation](https://git-scm.com/doc) - Official documentation for Git, the version control system used in this project.
Main Functionalities Added:
- CRUD endpoints for the Article model.
- Views to create, edit and see article information.
- Search endpoint to search title and content by a keyword.
54 changes: 54 additions & 0 deletions app/controllers/articles_controller.rb
@@ -0,0 +1,54 @@
class ArticlesController < ApplicationController
def index
@articles = Article.all
end

def show
@article = Article.find(params[:id])
end

def new
@article = Article.new
end

# Creates article with at least a title and a content body.
def create
@article = Article.new(article_params)

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

def edit
@article = Article.find(params[:id])
end

def update
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to root_path, status: :see_other
end

def search
@article = Article.search(params[:keyword])
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
# Title and content are mandatory columns
validates :title, presence: true
validates :content, presence: true

# Queries Database For Keywords
def self.search(keyword)
@article = self.where("title LIKE ? OR content LIKE ?", "%" + keyword + "%", "%" + keyword + "%")
end
end


38 changes: 38 additions & 0 deletions app/views/articles/_form.html.erb
@@ -0,0 +1,38 @@
<%= 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 %><br>
<% article.errors.full_messages_for(:content).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

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

<div>
<%= form.label :date %><br>
<%= form.date_field :date %>
<% article.errors.full_messages_for(:date).each do |message| %>
<div><%= message %></div>
<% end %>
</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 %>
11 changes: 11 additions & 0 deletions app/views/articles/index.html.erb
@@ -0,0 +1,11 @@
<h1>Encyclopedia</h1>

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

<%= link_to "New Article", new_article_path %>
3 changes: 3 additions & 0 deletions app/views/articles/new.html.erb
@@ -0,0 +1,3 @@
<h1>New Article</h1>

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

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

<ul>
<li><%= link_to "Edit", edit_article_path(@article) %></li>
<li><%= link_to "Destroy", article_path(@article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %></li>
</ul>
Empty file added app/views/layouts/index.html.rb
Empty file.
11 changes: 4 additions & 7 deletions config/routes.rb
@@ -1,10 +1,7 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
root "articles#index"

# 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"
# Routes CRUD operations for articles
resources :articles
get '/search', to: 'articles#search'
end
12 changes: 12 additions & 0 deletions db/migrate/20240129171832_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
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
@@ -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
7 changes: 7 additions & 0 deletions test/models/article_test.rb
Expand Up @@ -38,6 +38,13 @@ class ArticleTest < ActiveSupport::TestCase
assert_equal Date.yesterday, article.date
end

test 'updates the article metadata with no author and date initially' do
article = Article.create(title: 'Sample Article', content: 'Lorem ipsum dolor sit amet.')
article.update(author: 'Jane Smith', date: Date.yesterday)
assert_equal 'Jane Smith', article.author
assert_equal Date.yesterday, article.date
end

test 'deletes an article' do
article = Article.create(title: 'Sample Article', content: 'Lorem ipsum dolor sit amet.')
article.destroy
Expand Down