Skip to content

Commit 8702519

Browse files
committed
Add TaskStep model for subtasks, and integrate Turbo
1 parent 0f8c33a commit 8702519

File tree

13 files changed

+97
-14
lines changed

13 files changed

+97
-14
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ group :test do
2424
gem "factory_bot_rails"
2525
gem "rspec-rails", "~> 7.1"
2626
end
27+
28+
gem "turbo-rails", "~> 2.0"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ GEM
250250
stringio (3.1.2)
251251
thor (1.3.2)
252252
timeout (0.4.3)
253+
turbo-rails (2.0.11)
254+
actionpack (>= 6.0.0)
255+
railties (>= 6.0.0)
253256
tzinfo (2.0.6)
254257
concurrent-ruby (~> 1.0)
255258
unicode-display_width (3.1.4)
@@ -275,6 +278,7 @@ DEPENDENCIES
275278
rspec-rails (~> 7.1)
276279
rubocop-rails-omakase
277280
todoro!
281+
turbo-rails (~> 2.0)
278282

279283
BUNDLED WITH
280284
2.6.1
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Todoro
2+
class TaskStepsController < ApplicationController
3+
before_action :set_task
4+
before_action :set_task_step, only: [:complete]
5+
6+
def create
7+
@task_step = @task.task_steps.build(task_step_params)
8+
9+
if @task_step.save
10+
respond_to do |format|
11+
format.html { redirect_to [@taskable, @task.task_list], notice: "Subtask added." }
12+
format.turbo_stream
13+
end
14+
else
15+
render :new
16+
end
17+
end
18+
19+
20+
def complete
21+
@task_step.update(completed: true)
22+
respond_to do |format|
23+
format.html { redirect_to [@taskable, @task.task_list], notice: "Subtask marked as completed." }
24+
format.turbo_stream
25+
end
26+
end
27+
28+
private
29+
30+
def set_task
31+
@task = Todoro::Task.find(params[:task_id])
32+
end
33+
34+
def set_task_step
35+
@task_step = @task.task_steps.find(params[:id])
36+
end
37+
38+
def task_step_params
39+
params.require(:task_step).permit(:title, :complete)
40+
end
41+
end
42+
end

app/controllers/todoro/tasks_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TasksController < ApplicationController
55

66
def new
77
@task = @task_list.tasks.new
8+
@task.task_steps.build
89
end
910

1011
def create
@@ -33,7 +34,7 @@ def destroy
3334
end
3435

3536
def complete
36-
@task.update(status: "completed")
37+
@task.complete!
3738
redirect_to [ @taskable, @task_list ], notice: "Task marked as completed."
3839
end
3940

@@ -48,7 +49,8 @@ def set_task_list
4849
end
4950

5051
def task_params
51-
params.require(:task).permit(:title, :description, :status, :due_date)
52+
params.require(:task).permit(:title, :description, :status, :due_date,
53+
task_steps_attributes: [:id, :title, :completed, :_destroy])
5254
end
5355
end
5456
end

app/models/todoro/task.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Task < ApplicationRecord
1111

1212
after_create :set_default_reminders
1313

14+
accepts_nested_attributes_for :task_steps
15+
1416
def complete!
1517
update(status: "completed", completed_at: Time.zone.now)
1618
end
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
<h1><%= @task_list.name %></h1>
2-
3-
<%= link_to "Edit", edit_polymorphic_path([@task_list.taskable, @task_list]), class: "btn btn-warning" %>
4-
5-
<%= button_to "Delete", polymorphic_path([@task_list.taskable, @task_list]), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-link" %>
6-
71
<h2>Tasks</h2>
82
<%= link_to "Add Task", new_polymorphic_path([@task_list.taskable, @task_list, :task]), class: "btn btn-primary" %>
93

@@ -13,12 +7,20 @@
137
<%= task.title %> - <%= task.status %>
148
<%= link_to "Edit", edit_polymorphic_path([@task_list.taskable, @task_list, task]) %> |
159

16-
<%= button_to "Complete", polymorphic_path([@task_list.taskable, @task_list, task], action: :complete), method: :patch, class: "btn btn-success btn-link", data: { turbo: true } if task.status == "pending" %>
10+
<%= button_to "Complete", polymorphic_path([@task_list.taskable, @task_list, task], action: :complete), method: :patch, class: "btn btn-success btn-link", data: { turbo: true } if task.status == "pending" %>
11+
12+
<%= button_to "Delete", polymorphic_path([@task_list.taskable, @task_list, task]), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-link" %>
13+
14+
<h3>Subtasks</h3>
15+
<div id="subtasks">
16+
<%= turbo_frame_tag "task_steps" do %>
17+
<%= render task.task_steps %>
18+
<% end %>
19+
</div>
1720

18-
<%= button_to "Delete", polymorphic_path([@task_list.taskable, @task_list, task]), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-link" %>
21+
<%= render "todoro/task_steps/form", task: task %>
1922

20-
</li>
21-
<% end %>
23+
<% end %>
2224
</ul>
2325

2426
<%= link_to "Back to Task Lists", polymorphic_path([@task_list.taskable, :task_lists]), class: "btn btn-secondary" %>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%= form_with model: Todoro::TaskStep.new, url: [@taskable, @task_list, task, :task_steps], method: :post, local: true do |form| %>
2+
<div class="form-group">
3+
<%= form.label :title, "New Subtask" %>
4+
<%= form.text_field :title, class: "form-control", required: true %>
5+
</div>
6+
7+
<%= form.submit "Add Subtask", class: "btn btn-primary" %>
8+
<% end %>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div id="task_step_<%= task_step.id %>">
2+
<%= task_step.title %> - <%= task_step.completed ? "✅" : "❌" %>
3+
4+
<%= button_to "Complete", polymorphic_path([@task_list.taskable, @task_list, task_step.task, task_step], action: :complete), method: :patch, class: "btn btn-success btn-link", data: { turbo: true } unless task_step.completed %>
5+
6+
<%= button_to "Delete", polymorphic_path([@task_list.taskable, @task_list, task_step.task, task_step]), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-link" %>
7+
</div>

app/views/todoro/tasks/_form.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<%= form_with model: task, url: polymorphic_path([@taskable, @task_list, @task]), local: true do |form| %>
1+
<%= form_with model: task, url: polymorphic_path([@taskable, @task_list, task]), local: true do |form| %>
2+
23
<div class="form-group">
34
<%= form.label :title %>
45
<%= form.text_field :title, class: "form-control", required: true %>
@@ -14,5 +15,5 @@
1415
<%= form.datetime_select :due_date, class: "form-control" %>
1516
</div>
1617

17-
<%= form.submit class: "btn btn-success" %>
18+
<%= form.submit "Save Task", class: "btn btn-success" %>
1819
<% end %>

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
resources :task_lists do
77
resources :tasks, only: [ :new, :edit, :create, :update, :destroy ] do
88
patch :complete, on: :member
9+
10+
resources :task_steps, only: [ :create ], path: 'step' do
11+
patch :complete, on: :member
12+
end
913
end
1014
end
1115
end

0 commit comments

Comments
 (0)