Scaffold Todo
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled

This commit is contained in:
2025-10-27 10:18:59 +01:00
parent a33de989ae
commit 6776729eab
19 changed files with 262 additions and 0 deletions

1
.gitignore vendored
View File

@@ -33,3 +33,4 @@
# Ignore key files for decrypting credentials and more.
/config/*.key
.idea/

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, 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, notice: "Todo was successfully updated.", status: :see_other }
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_path, notice: "Todo was successfully destroyed.", status: :see_other }
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.expect(:id))
end
# Only allow a list of trusted parameters through.
def todo_params
params.expect(todo: [ :name, :description ])
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) do |form| %>
<% if todo.errors.any? %>
<div style="color: red">
<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>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :description, style: "display: block" %>
<%= form.textarea :description %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View File

@@ -0,0 +1,12 @@
<div id="<%= dom_id todo %>">
<div>
<strong>Name:</strong>
<%= todo.name %>
</div>
<div>
<strong>Description:</strong>
<%= todo.description %>
</div>
</div>

View File

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

View File

@@ -0,0 +1,12 @@
<% content_for :title, "Editing todo" %>
<h1>Editing todo</h1>
<%= render "form", todo: @todo %>
<br>
<div>
<%= link_to "Show this todo", @todo %> |
<%= link_to "Back to todos", todos_path %>
</div>

View File

@@ -0,0 +1,16 @@
<p style="color: green"><%= notice %></p>
<% content_for :title, "Todos" %>
<h1>Todos</h1>
<div id="todos">
<% @todos.each do |todo| %>
<%= render todo %>
<p>
<%= link_to "Show this todo", todo %>
</p>
<% end %>
</div>
<%= link_to "New todo", new_todo_path %>

View File

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

View File

@@ -0,0 +1,11 @@
<% content_for :title, "New todo" %>
<h1>New todo</h1>
<%= render "form", todo: @todo %>
<br>
<div>
<%= link_to "Back to todos", todos_path %>
</div>

View File

@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>
<%= render @todo %>
<div>
<%= link_to "Edit this todo", edit_todo_path(@todo) %> |
<%= link_to "Back to todos", todos_path %>
<%= button_to "Destroy this todo", @todo, method: :delete %>
</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[8.1]
def change
create_table :todos do |t|
t.string :name
t.text :description
t.timestamps
end
end
end

20
db/schema.rb generated Normal file
View File

@@ -0,0 +1,20 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2025_10_27_091014) do
create_table "todos", force: :cascade do |t|
t.datetime "created_at", null: false
t.text "description"
t.string "name"
t.datetime "updated_at", null: false
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: { description: @todo.description, name: @todo.name } }
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: { description: @todo.description, name: @todo.name } }
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:
name: MyString
description: MyText
two:
name: MyString
description: MyText

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