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

Use stream actions #6

Draft
wants to merge 2 commits into
base: initial
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/controllers/tasks_controller.rb
Expand Up @@ -25,7 +25,10 @@ def create
def update
@task.update! task_params

redirect_to project_url(@project), notice: "Task was successfully updated."
respond_to do |format|
format.html { redirect_to project_url(@project), notice: "Task was successfully updated." }
format.turbo_stream
end
end

def destroy
Expand Down
7 changes: 7 additions & 0 deletions app/models/task.rb
Expand Up @@ -5,4 +5,11 @@ class Task < ApplicationRecord
scope :pending, -> { where(completed: false) }

validates :title, presence: true

after_update_commit :broadcast_update_later

private
def broadcast_update_later
broadcast_render_later_to project, partial: "tasks/update", locals: { task: self, project: project }
end
end
2 changes: 2 additions & 0 deletions app/views/projects/show.html.erb
@@ -1,3 +1,5 @@
<%= turbo_stream_from @project %>

<nav class="breadcrumb" aria-label="breadcrumbs">
<ul>
<li>
Expand Down
13 changes: 13 additions & 0 deletions app/views/tasks/_update.turbo_stream.erb
@@ -0,0 +1,13 @@
<%= turbo_stream.replace dom_id(task) do %>
<%= render "projects/task", project: project, task: task %>
<% end %>

<%= turbo_stream.replace dom_id(project, :progress_bar) do %>
<%= project_progress_bar_for(project) %>
<% end %>

<%= turbo_stream.replace dom_id(project, :complete_count) do %>
<%= render "projects/complete_count", project: project %>
<% end %>


1 change: 1 addition & 0 deletions app/views/tasks/update.turbo_stream.erb
@@ -0,0 +1 @@
<%= render "tasks/update", task: @task, project: @project %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you use an extra _update partial in the update template?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yshmarov because I want to use the same content for:

  1. The controller response (update.html.erb), which is not a partial, but an action template.
  2. The broadcasted partial (_update.html.erb).

It's just to reuse the same code in both.