-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CRUD actions and fix tests
- Loading branch information
Showing
23 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//= link_tree ../images | ||
//= link_directory ../stylesheets .css | ||
//= link_tree ../../javascript .js | ||
//= link_tree ../../../vendor/javascript .js | ||
//= link_tree ../builds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@import 'bootstrap/scss/bootstrap'; | ||
@import 'bootstrap-icons/font/bootstrap-icons'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
class ArticlesController < ApplicationController | ||
# Ensures that the set_article method is called before the specified actions | ||
before_action :set_article, only: %i[show edit update destroy] | ||
|
||
# Displays a list of articles, filtered by title if a query parameter is present | ||
def index | ||
if params[:query].present? | ||
@articles = Article.where("title LIKE ?", params[:query]) | ||
else | ||
@articles = Article.all | ||
end | ||
end | ||
|
||
# Displays the details of a specific article | ||
def show | ||
end | ||
|
||
# Initializes a new article instance for creation | ||
def new | ||
@article = Article.new | ||
end | ||
|
||
# Renders the form for editing an existing article | ||
def edit | ||
end | ||
|
||
# Creates a new article based on parameters, and renders turbo_stream if successful | ||
def create | ||
@article = Article.new(article_params) | ||
if @article.save | ||
respond_to do |format| | ||
format.turbo_stream | ||
end | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# Updates an existing article with the provided parameters, redirects on success | ||
def update | ||
if @article.update(article_params) | ||
respond_to do |format| | ||
format.html { redirect_to article_url(@article), notice: "Article was successfully updated." } | ||
end | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
end | ||
end | ||
|
||
# Destroys the specified article instance, rendering turbo_stream | ||
def destroy | ||
@article.destroy! | ||
|
||
respond_to do |format| | ||
format.turbo_stream | ||
end | ||
end | ||
|
||
private | ||
# Sets the @article instance variable based on the provided ID parameter | ||
def set_article | ||
@article = Article.find(params[:id]) | ||
end | ||
|
||
# Defines the permitted parameters for article creation and updates | ||
def article_params | ||
params.require(:article).permit(:title, :content, :author, :date) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ArticlesHelper | ||
def display_errors_for_field(object, field) | ||
if object.errors.any? | ||
object.errors.full_messages_for(field).each do |message| | ||
concat content_tag(:div, message, class: 'text-danger') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails | ||
import "@hotwired/turbo-rails" | ||
import "controllers" | ||
import * as bootstrap from "bootstrap" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Article < ApplicationRecord | ||
# Validates presence of title and content fields | ||
validates :title, presence: true | ||
validates :content, presence: true | ||
|
||
# Defines a class method to search for articles based on a query | ||
def self.search(query) | ||
where("title LIKE :query OR content LIKE :query", query: "%#{query}%") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<%= turbo_frame_tag article do %> | ||
<div class="card p-4 mt-2"> | ||
<p> | ||
<strong>Title:</strong> | ||
<%= link_to article.title, article, data: { turbo_frame: '_top'} %> | ||
</p> | ||
|
||
<p> | ||
<strong>Content:</strong> | ||
<%= article.content %> | ||
</p> | ||
|
||
<p> | ||
<strong>Author:</strong> | ||
<%= article.author %> | ||
</p> | ||
|
||
<p> | ||
<strong>Date:</strong> | ||
<%= article.date %> | ||
</p> | ||
|
||
<div class="d-flex gap-2"> | ||
<%= link_to 'Edit', edit_article_path(article), class: 'btn btn-success w-100' %> | ||
<%= button_to 'Delete', article_path(article), method: :delete, class: 'btn btn-danger'%> | ||
</div> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<div class="card p-4 mt-1"> | ||
<%= form_with(model: article) do |form| %> | ||
|
||
<div> | ||
<%= form.label :title, style: "display: block" %> | ||
<%= form.text_field :title, class: 'form-control' %> | ||
<% display_errors_for_field(@article, :title) %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :content, style: "display: block" %> | ||
<%= form.text_field :content, class: 'form-control' %> | ||
<% display_errors_for_field(@article, :content) %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :author, style: "display: block" %> | ||
<%= form.text_field :author, class: 'form-control' %> | ||
</div> | ||
|
||
<div> | ||
<%= form.hidden_field :date, class: 'form-control', value: Time.now %> | ||
</div> | ||
|
||
<div class="d-flex gap-2"> | ||
<%= form.submit class: 'btn btn-success mt-2 w-100' %> | ||
<%= link_to 'Cancel', root_path, class: 'btn btn-danger mt-2'%> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= turbo_stream.append "articles" do%> | ||
<%= render 'articles/article', article: @article%> | ||
<% end %> | ||
|
||
<%= turbo_stream.update Article.new, '' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= turbo_stream.remove @article %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= turbo_frame_tag @article do %> | ||
<%= render "form", article: @article %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<h1 class="text-dark fw-bold">Encyclopedia</h1> | ||
<p class="text-muted">CRUD with Rails</p> | ||
<hr> | ||
|
||
<%= form_with(url: articles_path, method: :get, data: { turbo_frame: "article", turbo_action: 'advance' }) do |form| %> | ||
<div class="d-flex gap-1"> | ||
<%= form.text_field :query, class: 'form-control' %> | ||
<%= form.submit class: 'btn btn-success' %> | ||
<% if params[:query].present? %> | ||
<%= link_to 'Clear', articles_path, class: 'btn btn-danger' %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
|
||
<hr> | ||
|
||
<%= link_to 'Add Article', new_article_path, data: { turbo_frame: dom_id(Article.new) }, class: 'btn btn-dark' %> | ||
<%= turbo_frame_tag Article.new %> | ||
|
||
<div id="articles" class="d-flex gap-3"> | ||
<% @articles.each do |article| %> | ||
<%= render article %> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= turbo_frame_tag @article do%> | ||
<%= render "form", article: @article %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render @article %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= turbo_stream.update @article %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
</head> | ||
|
||
<body> | ||
<%= yield %> | ||
<div class="container mt-5"> | ||
<%= yield %> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
Rails.application.routes.draw do | ||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html | ||
|
||
# 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. | ||
root "articles#index" | ||
get '/search', to: "articles#search" | ||
resources :articles | ||
get "up" => "rails/health#show", as: :rails_health_check | ||
|
||
# Defines the root path route ("/") | ||
# root "posts#index" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateArticles < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :articles do |t| | ||
t.string :title | ||
t.string :content | ||
t.string :author | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddDateToArticle < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :articles, :date, :datetime | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
# one: | ||
# title: MyString | ||
# content: MyString | ||
# author: MyString | ||
|
||
# two: | ||
# title: MyString | ||
# content: MyString | ||
# author: MyString |