Skip to content

Commit 8e98edb

Browse files
committed
quote total (no live updating)
1 parent 6e54231 commit 8e98edb

File tree

10 files changed

+64
-8
lines changed

10 files changed

+64
-8
lines changed

NOTES.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ broadcasts_to ->(quote) { [quote.company, "quotes"] }, inserts_by: :prepend
5656
view:
5757
<%= turbo_stream_from(current_company, "quotes") %>
5858

59-
nested_dom_id(parent, identifier)
59+
60+
Note: Now that we have more experience with Turbo Frames, let's discuss when to use a <turbo-frame> tag or a <div>.
61+
62+
We must use a Turbo Frame when we need Turbo to intercept clicks on links and form sumbissions for us. For example, the content of the line_items/_line_item.html.erb partial must be wrapped inside a Turbo Frame because when we click on the "Edit" button for a line item, we want Turbo to replace the content of this Turbo Frame with the form extracted from the LineItems#edit page. This feature wouldn't work with a simple div.
63+
64+
On the other hand, we don't need a Turbo Frame when we only target an id of the DOM in a Turbo Stream view. For example, we didn't use a Turbo Frame for flash messages. Instead, we used a div with an id of flash to prepend new messages. We could have used a div with the id of quotes on the Quotes#index page instead of a Turbo Frame, because the quotes id is only used in Turbo Stream views to prepend quotes to the list. It never serves to intercept clicks on links and form submissions.
65+
66+
My personal preference here is always to use Turbo Frame tags as it makes it obvious that the id is used somewhere in a Turbo Stream view. This is why we use Turbo Frames everywhere in this tutorial. However, it is perfectly fine if you disagree and prefer to use a div when you don't need a Turbo Frame.
67+

app/assets/stylesheets/application.sass.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
@import "config/reset";
88
@import "config/animations";
99

10+
// Utilities
11+
@import "utilities/margins";
12+
1013
// Components
1114
@import "components/btn";
1215
@import "components/error_message";
@@ -19,6 +22,7 @@
1922
@import "components/empty_state";
2023
@import "components/line_item_date";
2124
@import "components/line_item";
25+
@import "components/quote_total";
2226

2327
// Layouts
2428
@import "layouts/container";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.quote-total {
2+
position: fixed;
3+
bottom: 0;
4+
width: 100%;
5+
6+
font-size: var(--font-size-xl);
7+
font-weight: bold;
8+
background-color: var(--color-white);
9+
box-shadow: var(--shadow-large);
10+
11+
padding-top: var(--space-xs);
12+
padding-bottom: var(--space-xs);
13+
14+
@include media(tabletAndUp) {
15+
padding-top: var(--space-m);
16+
padding-bottom: var(--space-m);
17+
}
18+
19+
&__inner {
20+
display: flex;
21+
align-items: center;
22+
justify-content: space-between;
23+
}
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mb-xxxxl {
2+
margin-bottom: var(--space-xxxxl);
3+
}

app/models/line_item.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ class LineItem < ApplicationRecord
66
validates :unit_price, presence: true, numericality: { greater_than: 0 }
77

88
delegate :quote, to: :line_item_date
9+
10+
def total_price
11+
quantity * unit_price
12+
end
913
end

app/models/quote.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
class Quote < ApplicationRecord
22
belongs_to :company
33
has_many :line_item_dates, dependent: :destroy #, strict_loading: true
4+
has_many :line_items, through: :line_item_dates
45

56
validates :name, presence: true
67

78
scope :desc_id_ordered, -> { order(id: :desc) }
89

910
broadcasts_to ->(quote) { [quote.company, "quotes"] }, inserts_by: :prepend
11+
12+
def total_price
13+
line_items.sum(&:total_price)
14+
end
1015
end

app/views/quotes/_total.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<footer class="quote-total">
2+
<div class="quote-total__inner container">
3+
<div>Total:</div>
4+
<div><%= number_to_currency quote.total_price %></div>
5+
</div>
6+
</footer>

app/views/quotes/show.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
COMMENT
1616
%>
1717

18-
<main class="container">
18+
<main class="container mb-xxxxl">
1919
<%= link_to sanitize("&larr; Back to quotes"), quotes_path %>
2020
<div class="header">
2121
<h1>
@@ -32,3 +32,5 @@ COMMENT
3232
<%= render @line_item_dates, quote: @quote %>
3333
<% end %>
3434
</main>
35+
36+
<%= render "quotes/total", quote: @quote %>

test/models/line_item_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "test_helper"
22

33
class LineItemTest < ActiveSupport::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
test "#total_price returns the total price of the line item" do
5+
assert_equal 250, line_items(:catering_today).total_price
6+
end
77
end

test/models/quote_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "test_helper"
22

33
class QuoteTest < ActiveSupport::TestCase
4-
# test "the truth" do
5-
# assert true
6-
# end
4+
test "#total_price returns the sum of the total price of all line items" do
5+
assert_equal 2500, quotes(:one).total_price
6+
end
77
end

0 commit comments

Comments
 (0)