Scaffold todo model

This commit is contained in:
2024-07-28 18:56:44 +02:00
parent ed4a5759be
commit ef58474752
18 changed files with 286 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
class TodosController < ApplicationController
before_action :set_todo, only: %i[ show edit update destroy ]
# GET /todos or /todos.json
def index
@todos = Todo.all
end
# GET /todos/1 or /todos/1.json
def show
end
# GET /todos/new
def new
@todo = Todo.new
end
# GET /todos/1/edit
def edit
end
# POST /todos or /todos.json
def create
@todo = Todo.new(todo_params)
respond_to do |format|
if @todo.save
format.html { redirect_to todo_url(@todo), notice: "Todo was successfully created." }
format.json { render :show, status: :created, location: @todo }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /todos/1 or /todos/1.json
def update
respond_to do |format|
if @todo.update(todo_params)
format.html { redirect_to todo_url(@todo), notice: "Todo was successfully updated." }
format.json { render :show, status: :ok, location: @todo }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1 or /todos/1.json
def destroy
@todo.destroy!
respond_to do |format|
format.html { redirect_to todos_url, notice: "Todo was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo
@todo = Todo.find(params[:id])
end
# Only allow a list of trusted parameters through.
def todo_params
params.require(:todo).permit(:title, :status)
end
end

View File

@@ -0,0 +1,2 @@
module TodosHelper
end

2
app/models/todo.rb Normal file
View File

@@ -0,0 +1,2 @@
class Todo < ApplicationRecord
end

View File

@@ -0,0 +1,27 @@
<%= form_with(model: todo, class: "contents") do |form| %>
<% if todo.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(todo.errors.count, "error") %> prohibited this todo from being saved:</h2>
<ul>
<% todo.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= form.label :title %>
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :status %>
<%= form.number_field :status, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>

View File

@@ -0,0 +1,12 @@
<div id="<%= dom_id todo %>">
<p class="my-5">
<strong class="block font-medium mb-1">Title:</strong>
<%= todo.title %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Status:</strong>
<%= todo.status %>
</p>
</div>

View File

@@ -0,0 +1,2 @@
json.extract! todo, :id, :title, :status, :created_at, :updated_at
json.url todo_url(todo, format: :json)

View File

@@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing todo</h1>
<%= render "form", todo: @todo %>
<%= link_to "Show this todo", @todo, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to todos", todos_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,21 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<% content_for :title, "Todos" %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Todos</h1>
<%= link_to "New todo", new_todo_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="todos" class="min-w-full">
<% @todos.each do |todo| %>
<%= render todo %>
<p>
<%= link_to "Show this todo", todo, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</p>
<% end %>
</div>
</div>

View File

@@ -0,0 +1 @@
json.array! @todos, partial: "todos/todo", as: :todo

View File

@@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New todo</h1>
<%= render "form", todo: @todo %>
<%= link_to "Back to todos", todos_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View File

@@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @todo %>
<%= link_to "Edit this todo", edit_todo_path(@todo), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to todos", todos_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to "Destroy this todo", @todo, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
</div>
</div>

View File

@@ -0,0 +1 @@
json.partial! "todos/todo", todo: @todo

View File

@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :todos
# 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.

View File

@@ -0,0 +1,10 @@
class CreateTodos < ActiveRecord::Migration[7.1]
def change
create_table :todos do |t|
t.string :title
t.integer :status
t.timestamps
end
end
end

View File

@@ -0,0 +1,48 @@
require "test_helper"
class TodosControllerTest < ActionDispatch::IntegrationTest
setup do
@todo = todos(:one)
end
test "should get index" do
get todos_url
assert_response :success
end
test "should get new" do
get new_todo_url
assert_response :success
end
test "should create todo" do
assert_difference("Todo.count") do
post todos_url, params: { todo: { status: @todo.status, title: @todo.title } }
end
assert_redirected_to todo_url(Todo.last)
end
test "should show todo" do
get todo_url(@todo)
assert_response :success
end
test "should get edit" do
get edit_todo_url(@todo)
assert_response :success
end
test "should update todo" do
patch todo_url(@todo), params: { todo: { status: @todo.status, title: @todo.title } }
assert_redirected_to todo_url(@todo)
end
test "should destroy todo" do
assert_difference("Todo.count", -1) do
delete todo_url(@todo)
end
assert_redirected_to todos_url
end
end

9
test/fixtures/todos.yml vendored Normal file
View File

@@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
status: 1
two:
title: MyString
status: 1

7
test/models/todo_test.rb Normal file
View File

@@ -0,0 +1,7 @@
require "test_helper"
class TodoTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

43
test/system/todos_test.rb Normal file
View File

@@ -0,0 +1,43 @@
require "application_system_test_case"
class TodosTest < ApplicationSystemTestCase
setup do
@todo = todos(:one)
end
test "visiting the index" do
visit todos_url
assert_selector "h1", text: "Todos"
end
test "should create todo" do
visit todos_url
click_on "New todo"
fill_in "Status", with: @todo.status
fill_in "Title", with: @todo.title
click_on "Create Todo"
assert_text "Todo was successfully created"
click_on "Back"
end
test "should update Todo" do
visit todo_url(@todo)
click_on "Edit this todo", match: :first
fill_in "Status", with: @todo.status
fill_in "Title", with: @todo.title
click_on "Update Todo"
assert_text "Todo was successfully updated"
click_on "Back"
end
test "should destroy Todo" do
visit todo_url(@todo)
click_on "Destroy this todo", match: :first
assert_text "Todo was successfully destroyed"
end
end