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

[Edison Qu] Ruby on rails Encyclopedia #304

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
.history

# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
@@ -1,7 +1,7 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=2.7.6
ARG RUBY_VERSION=3.2.3
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
Expand All @@ -21,6 +21,8 @@ FROM base as build
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libvips pkg-config

RUN gem install bundler:2.5.5

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -66,3 +66,7 @@ group :test do
gem "selenium-webdriver"
gem "webdrivers"
end

# group :test do
# gem 'database_cleaner'
# end
7 changes: 7 additions & 0 deletions Gemfile.lock
Expand Up @@ -95,6 +95,12 @@ GEM
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crass (1.0.6)
database_cleaner (2.0.2)
database_cleaner-active_record (>= 2, < 3)
database_cleaner-active_record (2.1.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.3.4)
debug (1.9.1)
irb (~> 1.10)
Expand Down Expand Up @@ -261,6 +267,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
capybara
database_cleaner
debug
importmap-rails
jbuilder
Expand Down
57 changes: 57 additions & 0 deletions app/controllers/articles_controller.rb
@@ -0,0 +1,57 @@
class ArticlesController < ApplicationController
# Displays a list of all articles
def index
@articles = Article.all
end

# Shows details of a specific article identified by params[:id]
def show
@article = Article.find(params[:id])
end

# Prepares a new, unsaved article for creation
def new
@article = Article.new
end

# Creates a new article from parameters and redirects on success
# On failure (e.g., validation errors), it re-renders the 'new' view for correction
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end

# Fetches the article for editing
def edit
@article = Article.find(params[:id])
end

# Updates the article and redirects on success
# On failure, re-renders the 'edit' view for correction
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end

# Deletes the article and redirects to the list of articles
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end

private

# Strong parameters method for creating/updating articles
def article_params
params.require(:article).permit(:title, :content, :author, :date)
end
end
12 changes: 12 additions & 0 deletions app/models/article.rb
@@ -0,0 +1,12 @@
class Article < ApplicationRecord
# Validations
validates :title, presence: true
validates :content, presence: true
# validates :author, presence: true


# search
def self.search(query)
where("title LIKE ? OR content LIKE ?", "%#{query}%", "%#{query}%")
end
end
10 changes: 10 additions & 0 deletions app/views/articles/_form_errors.html.erb
@@ -0,0 +1,10 @@
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited this object from being saved:</h2>
<ul>
<% object.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
22 changes: 22 additions & 0 deletions app/views/articles/edit.html.erb
@@ -0,0 +1,22 @@
<h1>Edit Article</h1>

<%= form_with(model: @article, local: true) do |form| %>

<%= render 'form_errors', object: @article %>

<%= form.label :title %>
<%= form.text_field :title %>

<%= form.label :content %>
<%= form.text_area :content %>

<%= form.label :author %>
<%= form.text_field :author %>

<%= form.label :date %>
<%= form.date_select :date %>

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

<%= link_to 'Back', articles_path %>
17 changes: 17 additions & 0 deletions app/views/articles/index.html.erb
@@ -0,0 +1,17 @@

<h1>Articles</h1>

<!-- Link to new article -->
<%= link_to 'New Article', new_article_path %>

<ul>
<% @articles.each do |article| %>
<li>
<h2><%= link_to article.title, article_path(article) %></h2>
<p><%= truncate(article.content, length: 100) %></p>
<p>By: <%= article.author %> | Published on: <%= article.date %></p>
<%= link_to 'Edit', edit_article_path(article) %> |
<%= link_to 'Delete', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
<% end %>
</ul>
32 changes: 32 additions & 0 deletions app/views/articles/new.html.erb
@@ -0,0 +1,32 @@
<h1>New Article</h1>

<%= form_with model: @article, local: true do |form| %>

<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form.label :title %>
<%= form.text_field :title, id: :article_title %>

<%= form.label :content %>
<%= form.text_area :content, id: :article_content %>

<%= form.label :author %>
<%= form.text_field :author, id: :article_author %>

<%= form.label :date %>
<%= form.date_select :date, id: :article_date %>

<%= form.submit %>

<%= link_to 'Back', articles_path %>

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

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

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

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

<!-- Links for editing and going back -->
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -4,6 +4,7 @@
# 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
resources :articles

# Defines the root path route ("/")
# root "posts#index"
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20240130033010_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.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.

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
1 change: 1 addition & 0 deletions test/helpers/test_helper.rb
@@ -0,0 +1 @@
# test/test_helper.rb