Skip to content

Commit 79840e3

Browse files
authored
Add Turbo Stream support for navigation item CRUD (#1004)
## Summary - support Turbo Stream responses for navigation item creation and deletion - append/remove navigation items in real time using Turbo templates - add DOM target for navigation items table ## Testing - `bundle exec bundler-audit --update` *(fails: command not found)* - `bundle exec rubocop` *(fails: command not found)* - `bundle exec brakeman -q -w2` *(fails: command not found)* - `bin/codex_style_guard` *(fails: command not found)* - `bin/ci` *(fails: command not found)* ------ https://chatgpt.com/codex/tasks/task_e_689b64b451d08321a6cb73678bf9f765
2 parents 89bcc96 + adbf4ea commit 79840e3

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

app/controllers/better_together/navigation_items_controller.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,30 @@ def edit
3232
authorize @navigation_item
3333
end
3434

35-
def create
35+
def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
3636
@navigation_item = new_navigation_item
3737
@navigation_item.assign_attributes(navigation_item_params)
3838
authorize @navigation_item
39-
40-
if @navigation_item.save
41-
redirect_to @navigation_area, only_path: true, notice: 'Navigation item was successfully created.'
42-
else
43-
render :new
39+
respond_to do |format|
40+
if @navigation_item.save
41+
flash.now[:notice] = 'Navigation item was successfully created.'
42+
format.html do
43+
redirect_to @navigation_area, only_path: true,
44+
notice: 'Navigation item was successfully created.'
45+
end
46+
format.turbo_stream { render :create }
47+
else
48+
format.html { render :new, status: :unprocessable_entity }
49+
format.turbo_stream do
50+
render turbo_stream: [
51+
turbo_stream.update('form_errors', partial: 'layouts/better_together/errors',
52+
locals: { object: @navigation_item }),
53+
turbo_stream.update('navigation_item_form', partial: 'better_together/navigation_items/form',
54+
locals: { navigation_item: @navigation_item,
55+
navigation_area: @navigation_area })
56+
]
57+
end
58+
end
4459
end
4560
end
4661

@@ -74,8 +89,15 @@ def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
7489
def destroy
7590
authorize @navigation_item
7691
@navigation_item.destroy
77-
redirect_to navigation_area_navigation_items_url(@navigation_area),
78-
notice: 'Navigation item was successfully destroyed.'
92+
flash.now[:notice] = 'Navigation item was successfully destroyed.'
93+
94+
respond_to do |format|
95+
format.html do
96+
redirect_to navigation_area_navigation_items_url(@navigation_area),
97+
notice: 'Navigation item was successfully destroyed.'
98+
end
99+
format.turbo_stream { render :destroy }
100+
end
79101
end
80102

81103
private

app/views/better_together/navigation_items/_navigation_items_table.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<th class="text-end"><%= t('globals.actions') %></th>
1313
</tr>
1414
</thead>
15-
<tbody>
15+
<tbody id="navigation_items">
1616
<%= render partial: 'better_together/navigation_items/navigation_item_row',
1717
collection: navigation_items,
1818
as: :item
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<% target = @navigation_item.parent ? dom_id(@navigation_item.parent, :children) : 'navigation_items' %>
2+
<%= turbo_stream.append target,
3+
partial: 'better_together/navigation_items/navigation_item_row',
4+
locals: { item: @navigation_item } %>
5+
<%= turbo_stream.replace 'flash_messages',
6+
partial: 'layouts/better_together/flash_messages',
7+
locals: { flash: } %>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<%= turbo_stream.remove dom_id(@navigation_item) %>
2+
<%= turbo_stream.replace 'flash_messages',
3+
partial: 'layouts/better_together/flash_messages',
4+
locals: { flash: } %>

0 commit comments

Comments
 (0)