diff --git a/.gitignore b/.gitignore index fbcab40..a8d1ff4 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ # Ignore key files for decrypting credentials and more. /config/*.key +.idea/ diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb new file mode 100644 index 0000000..a3f6b2c --- /dev/null +++ b/app/controllers/todos_controller.rb @@ -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 diff --git a/app/helpers/todos_helper.rb b/app/helpers/todos_helper.rb new file mode 100644 index 0000000..65ab195 --- /dev/null +++ b/app/helpers/todos_helper.rb @@ -0,0 +1,2 @@ +module TodosHelper +end diff --git a/app/models/todo.rb b/app/models/todo.rb new file mode 100644 index 0000000..e7adee6 --- /dev/null +++ b/app/models/todo.rb @@ -0,0 +1,2 @@ +class Todo < ApplicationRecord +end diff --git a/app/views/todos/_form.html.erb b/app/views/todos/_form.html.erb new file mode 100644 index 0000000..011c569 --- /dev/null +++ b/app/views/todos/_form.html.erb @@ -0,0 +1,27 @@ +<%= form_with(model: todo) do |form| %> + <% if todo.errors.any? %> +
+

<%= pluralize(todo.errors.count, "error") %> prohibited this todo from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :name, style: "display: block" %> + <%= form.text_field :name %> +
+ +
+ <%= form.label :description, style: "display: block" %> + <%= form.textarea :description %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/todos/_todo.html.erb b/app/views/todos/_todo.html.erb new file mode 100644 index 0000000..f04b0c8 --- /dev/null +++ b/app/views/todos/_todo.html.erb @@ -0,0 +1,12 @@ +
+
+ Name: + <%= todo.name %> +
+ +
+ Description: + <%= todo.description %> +
+ +
diff --git a/app/views/todos/_todo.json.jbuilder b/app/views/todos/_todo.json.jbuilder new file mode 100644 index 0000000..726d212 --- /dev/null +++ b/app/views/todos/_todo.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! todo, :id, :name, :description, :created_at, :updated_at +json.url todo_url(todo, format: :json) diff --git a/app/views/todos/edit.html.erb b/app/views/todos/edit.html.erb new file mode 100644 index 0000000..023da66 --- /dev/null +++ b/app/views/todos/edit.html.erb @@ -0,0 +1,12 @@ +<% content_for :title, "Editing todo" %> + +

Editing todo

+ +<%= render "form", todo: @todo %> + +
+ +
+ <%= link_to "Show this todo", @todo %> | + <%= link_to "Back to todos", todos_path %> +
diff --git a/app/views/todos/index.html.erb b/app/views/todos/index.html.erb new file mode 100644 index 0000000..09bfa92 --- /dev/null +++ b/app/views/todos/index.html.erb @@ -0,0 +1,16 @@ +

<%= notice %>

+ +<% content_for :title, "Todos" %> + +

Todos

+ +
+ <% @todos.each do |todo| %> + <%= render todo %> +

+ <%= link_to "Show this todo", todo %> +

+ <% end %> +
+ +<%= link_to "New todo", new_todo_path %> diff --git a/app/views/todos/index.json.jbuilder b/app/views/todos/index.json.jbuilder new file mode 100644 index 0000000..fc70353 --- /dev/null +++ b/app/views/todos/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @todos, partial: "todos/todo", as: :todo diff --git a/app/views/todos/new.html.erb b/app/views/todos/new.html.erb new file mode 100644 index 0000000..669ec00 --- /dev/null +++ b/app/views/todos/new.html.erb @@ -0,0 +1,11 @@ +<% content_for :title, "New todo" %> + +

New todo

+ +<%= render "form", todo: @todo %> + +
+ +
+ <%= link_to "Back to todos", todos_path %> +
diff --git a/app/views/todos/show.html.erb b/app/views/todos/show.html.erb new file mode 100644 index 0000000..66b1329 --- /dev/null +++ b/app/views/todos/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @todo %> + +
+ <%= link_to "Edit this todo", edit_todo_path(@todo) %> | + <%= link_to "Back to todos", todos_path %> + + <%= button_to "Destroy this todo", @todo, method: :delete %> +
diff --git a/app/views/todos/show.json.jbuilder b/app/views/todos/show.json.jbuilder new file mode 100644 index 0000000..4ab75c3 --- /dev/null +++ b/app/views/todos/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "todos/todo", todo: @todo diff --git a/config/routes.rb b/config/routes.rb index 48254e8..52a955c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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. diff --git a/db/migrate/20251027091014_create_todos.rb b/db/migrate/20251027091014_create_todos.rb new file mode 100644 index 0000000..7c796b9 --- /dev/null +++ b/db/migrate/20251027091014_create_todos.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..134129d --- /dev/null +++ b/db/schema.rb @@ -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 diff --git a/test/controllers/todos_controller_test.rb b/test/controllers/todos_controller_test.rb new file mode 100644 index 0000000..2ba843d --- /dev/null +++ b/test/controllers/todos_controller_test.rb @@ -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 diff --git a/test/fixtures/todos.yml b/test/fixtures/todos.yml new file mode 100644 index 0000000..382f6d8 --- /dev/null +++ b/test/fixtures/todos.yml @@ -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 diff --git a/test/models/todo_test.rb b/test/models/todo_test.rb new file mode 100644 index 0000000..614a849 --- /dev/null +++ b/test/models/todo_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TodoTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end