Skip to content

Commit 44e3722

Browse files
committed
turbo for quote line item dates
1 parent 79664ea commit 44e3722

12 files changed

+137
-27
lines changed

app/controllers/line_item_dates_controller.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ def create
1010
@line_item_date = @quote.line_item_dates.build(line_item_date_params)
1111

1212
if @line_item_date.save
13-
redirect_to quote_path(@quote), notice: "Date was successfully created."
13+
respond_to do |format|
14+
notice = "Date was successfully created."
15+
format.html { redirect_to quote_path(@quote), notice: }
16+
format.turbo_stream { flash.now[:notice] = notice }
17+
end
1418
else
1519
render :new, status: :unprocessable_entity
1620
end
@@ -21,17 +25,25 @@ def edit
2125

2226
def update
2327
if @line_item_date.update(line_item_date_params)
24-
redirect_to quote_path(@quote), notice: "Date was successfully updated."
28+
respond_to do |format|
29+
notice = "Date was successfully updated."
30+
format.html { redirect_to quote_path(@quote), notice: }
31+
format.turbo_stream { flash.now[:notice] = notice }
32+
end
2533
else
2634
render :edit, status: :unprocessable_entity
2735
end
2836
end
2937

3038
def destroy
31-
@line_item_date.destroy
39+
@line_item_date.destroy
3240

33-
redirect_to quote_path(@quote), notice: "Date was successfully destroyed."
41+
respond_to do |format|
42+
notice = "Date was successfully destroyed."
43+
format.html { redirect_to quote_path(@quote), notice: }
44+
format.turbo_stream { flash.now[:notice] = notice }
3445
end
46+
end
3547

3648
private
3749

app/models/line_item_date.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class LineItemDate < ApplicationRecord
44
validates :date, presence: true, uniqueness: { scope: :quote_id }
55

66
scope :ordered, -> { order(date: :asc) }
7+
8+
def previous_date
9+
quote.line_item_dates.ordered.where("date < ?", date).last
10+
end
711
end
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
<div class="line-item-date">
2-
<div class="line-item-date__header">
3-
<h2 class="line-item-date__title">
4-
<%= l(line_item_date.date, format: :long) %>
5-
</h2>
1+
<%= turbo_frame_tag line_item_date do %>
2+
<div class="line-item-date">
3+
<div class="line-item-date__header">
4+
<h2 class="line-item-date__title">
5+
<%= l(line_item_date.date, format: :long) %>
6+
</h2>
67

7-
<div class="line-item-date__actions">
8-
<%# NOTE: polymorphic routes https://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html %>
9-
<%= button_to "Delete",
10-
quote_line_item_date_path(quote, line_item_date),
11-
method: :delete,
12-
form: { data: { turbo_confirm: "Are you sure?" } },
13-
class: "btn btn--light" %>
14-
<%= link_to "Edit",
15-
[:edit, quote, line_item_date],
16-
class: "btn btn--light" %>
8+
<div class="line-item-date__actions">
9+
<%# NOTE: polymorphic routes https://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html %>
10+
<%= button_to "Delete",
11+
quote_line_item_date_path(quote, line_item_date),
12+
method: :delete,
13+
form: { data: { turbo_confirm: "Are you sure?" } },
14+
class: "btn btn--light" %>
15+
<%= link_to "Edit",
16+
[:edit, quote, line_item_date],
17+
class: "btn btn--light" %>
18+
</div>
1719
</div>
1820
</div>
19-
</div>
21+
<% end %>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%# Step 1: remove the form from the Quotes#index page %>
2+
<%= turbo_stream.update LineItemDate.new, "" %>
3+
4+
<%# Step 2: add the date at the right place %>
5+
<% if previous_date = @line_item_date.previous_date %>
6+
<%= turbo_stream.after previous_date do %>
7+
<%= render @line_item_date, quote: @quote %>
8+
<% end %>
9+
<% else %>
10+
<%= turbo_stream.prepend "line_item_dates" do %>
11+
<%= render @line_item_date, quote: @quote %>
12+
<% end %>
13+
<% end %>
14+
15+
<%= render_turbo_stream_flash_messages %>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<%= turbo_stream.remove @line_item_date %>
2+
<%= render_turbo_stream_flash_messages %>

app/views/line_item_dates/edit.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
<h1>Edit date</h1>
66
</div>
77

8-
<%= render "form", quote: @quote, line_item_date: @line_item_date %>
8+
<%= turbo_frame_tag @line_item_date do %>
9+
<%= render "form", quote: @quote, line_item_date: @line_item_date %>
10+
<% end %>
911
</main>

app/views/line_item_dates/new.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
<h1>New date</h1>
66
</div>
77

8-
<%= render "form", quote: @quote, line_item_date: @line_item_date %>
8+
<%= turbo_frame_tag @line_item_date do %>
9+
<%= render "form", quote: @quote, line_item_date: @line_item_date %>
10+
<% end %>
911
</main>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%= turbo_stream.remove @line_item_date %>
2+
3+
<% if previous_date = @line_item_date.previous_date %>
4+
<%= turbo_stream.after previous_date do %>
5+
<%= render @line_item_date, quote: @quote %>
6+
<% end %>
7+
<% else %>
8+
<%= turbo_stream.prepend "line_item_dates" do %>
9+
<%= render @line_item_date, quote: @quote %>
10+
<% end %>
11+
<% end %>
12+
13+
<%= render_turbo_stream_flash_messages %>

app/views/quotes/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ COMMENT
2323
%>
2424

2525
<%= simple_form_for quote, html: { class: "quote form" } do |f| %>
26-
<%= form_error_notification(line_item_date) %>
26+
<%= form_error_notification(quote) %>
2727

2828
<%= f.input :name, input_html: { autofocus: true } %>
2929
<%= link_to "Cancel", quotes_path, class: "btn btn--light" %>

app/views/quotes/show.html.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ COMMENT
2323
</h1>
2424
<%= link_to "New date",
2525
new_quote_line_item_date_path(@quote),
26+
data: { turbo_frame: dom_id(LineItemDate.new) },
2627
class: "btn btn--primary" %>
2728
</div>
2829

29-
<%= render @line_item_dates, quote: @quote %>
30+
<%= turbo_frame_tag LineItemDate.new %>
31+
<%= turbo_frame_tag "line_item_dates" do %>
32+
<%= render @line_item_dates, quote: @quote %>
33+
<% end %>
3034
</main>

0 commit comments

Comments
 (0)