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

Add delete functionality #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions README.md

This file was deleted.

4 changes: 0 additions & 4 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ def destroy
end
end

def task_field
render layout: false
end

private
# Use callbacks to share common setup or constraints between actions.
def set_project
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class TasksController < ApplicationController
def new
render turbo_stream: turbo_stream.append(:tasks, partial: "tasks/task", locals: { task: Task.new })
end

def destroy
task = Task.find(params[:id])
task.destroy
rescue ActiveRecord::RecordNotFound
task = Task.new(id: params[:id])
ensure
render turbo_stream: turbo_stream.remove(task.id)
end
end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module ApplicationHelper
def turbo_id_for(obj)
obj.persisted? ? obj.id : obj.hash
end
end
2 changes: 2 additions & 0 deletions app/helpers/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TasksHelper
end
3 changes: 0 additions & 3 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ import { application } from "./application"

import HelloController from "./hello_controller"
application.register("hello", HelloController)

import RenderResponseController from "./render_response_controller"
application.register("render-response", RenderResponseController)
21 changes: 0 additions & 21 deletions app/javascript/controllers/render_response_controller.js

This file was deleted.

8 changes: 2 additions & 6 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
class Project < ApplicationRecord
has_many :tasks, dependent: :destroy

accepts_nested_attributes_for :tasks
validates :name, presence: true

# def task_attributes=(task_attributes)
# task_attributes.each do |attr|
# tasks.build(attr)
# end
# end
accepts_nested_attributes_for :tasks
end
2 changes: 2 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Task < ApplicationRecord
belongs_to :project

validates :name, presence: true
end
12 changes: 4 additions & 8 deletions app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with(model: project, data: { controller: "render-response" }, class: "space-y-4") do |form| %>
<%= form_with(model: project, class: "space-y-4") do |form| %>
<% if project.errors.any? %>
<div style="color: red">
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>
Expand All @@ -18,18 +18,14 @@
</fieldset>

<div id="tasks" class="mb-2">
<%= render partial: "task", collection: @project.tasks %>
<%= render @project.tasks %>
</div>

<div class="mb-2">
<%= button_tag "Add a Task", type: "button", class: "px-4 py-2 rounded-md bg-blue-300 hover:bg-blue-400", data: { action: "render-response#getTaskPartial" } %>
<%= link_to "Add a Task", new_task_path, data: { turbo_stream: true }, class: "p-2 rounded-md bg-blue-300 hover:bg-blue-400" %>
</div>

<div>
<% if form.object.persisted? %>
<%= form.submit "Update", class: "p-2 rounded-md bg-blue-300 hover:bg-blue-400 w-full" %>
<% else %>
<%= form.submit "Create", class: "p-2 rounded-md bg-blue-300 hover:bg-blue-400 w-full" %>
<% end %>
<%= form.submit nil, class: "p-2 rounded-md bg-blue-300 hover:bg-blue-400 w-full" %>
</div>
<% end %>
9 changes: 0 additions & 9 deletions app/views/projects/_task.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/projects/task_field.html.erb

This file was deleted.

10 changes: 10 additions & 0 deletions app/views/tasks/_task.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= fields_for "project[tasks_attributes][#{turbo_id_for(task)}]", task do |ff| %>
<fieldset class="mb-4" id="<%= turbo_id_for(task) %>">
<legend>Task Info</legend>
<%= ff.label :name %><br>
<%= ff.text_field :name, class: "px-2 py-1 border w-full rounded-md" %>
<%= ff.hidden_field :id %>
<%= button_tag "Delete Task", formaction: task_path(turbo_id_for(task)), formmethod: :delete, class: "mt-4 float-right p-2 rounded-md bg-red-300 hover:bg-red-400" %>
</fieldset>
<% end %>

2 changes: 2 additions & 0 deletions app/views/tasks/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Tasks#destroy</h1>
<p>Find me in app/views/tasks/destroy.html.erb</p>
2 changes: 2 additions & 0 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Tasks#new</h1>
<p>Find me in app/views/tasks/new.html.erb</p>
10 changes: 2 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
Rails.application.routes.draw do
root "projects#index"
resources :projects do
collection do
get "task_field"
end
end
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
resources :tasks, only: [:new, :destroy]
resources :projects
end
13 changes: 13 additions & 0 deletions test/controllers/tasks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "test_helper"

class TasksControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get tasks_new_url
assert_response :success
end

test "should get destroy" do
get tasks_destroy_url
assert_response :success
end
end