Skip to content

Commit

Permalink
udah ada total tapi ngga auto update total abis CRUD line
Browse files Browse the repository at this point in the history
  • Loading branch information
buncis committed Jul 19, 2022
1 parent 397a81e commit f5b1215
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/assets/stylesheets/application.sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
@import "components/empty_state";
@import "components/line_item_date";
@import "components/line_item";
@import "components/quote_total";
@import "components/turbo_progress_bar";

// Layouts
@import "layouts/container";
@import "layouts/header";

@import "components/turbo_progress_bar";
// Utilities
@import "utilities/margins";
24 changes: 24 additions & 0 deletions app/assets/stylesheets/components/_quote_total.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.quote-total {
position: fixed;
bottom: 0;
width: 100%;

font-size: var(--font-size-xl);
font-weight: bold;
background-color: var(--color-white);
box-shadow: var(--shadow-large);

padding-top: var(--space-xs);
padding-bottom: var(--space-xs);

@include media(tabletAndUp) {
padding-top: var(--space-m);
padding-bottom: var(--space-m);
}

&__inner {
display: flex;
align-items: center;
justify-content: space-between;
}
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/utilities/_margins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.mb-xxxxl {
margin-bottom: var(--space-xxxxl);
}
5 changes: 5 additions & 0 deletions app/models/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ class LineItem < ApplicationRecord
validates :unit_price, presence: true, numericality: { greater_than: 0 }

delegate :quote, to: :line_item_date

def total_price
quantity * unit_price
end

end
5 changes: 5 additions & 0 deletions app/models/quote.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Quote < ApplicationRecord
has_many :line_item_dates, dependent: :destroy
has_many :line_items, through: :line_item_dates
belongs_to :company
validates :name, presence: true

Expand All @@ -9,4 +10,8 @@ class Quote < ApplicationRecord
# after_update_commit -> { broadcast_replace_later_to "quotes" }
# after_destroy_commit -> { broadcast_remove_to "quotes" }
broadcasts_to -> (quote) { [quote.company, "quotes"] }, inserts_by: :prepend

def total_price
line_items.sum(&:total_price)
end
end
6 changes: 6 additions & 0 deletions app/views/quotes/_total.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<footer class="quote-total">
<div class="quote-total__inner container">
<div>Total:</div>
<div><%= number_to_currency quote.total_price %></div>
</div>
</footer>
3 changes: 2 additions & 1 deletion app/views/quotes/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<main class="container">
<main class="container mb-xxxxl">
<%= link_to sanitize("&larr; Back to quotes"), quotes_path %>
<div class="header">
<h1>
Expand All @@ -14,3 +14,4 @@
<%= render @line_item_dates, quote: @quote %>
<% end %>
</main>
<%= render "quotes/total", quote: @quote %>
4 changes: 4 additions & 0 deletions test/system/line_items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class LineItemSystemTest < ApplicationSystemTestCase
visit quote_path(@quote)
end

test "#total_price returns the total price of the line item" do
assert_equal 250, line_items(:catering_today).total_price
end

test "Creating a new line item" do
assert_selector "h1", text: "First quote"
within "##{dom_id(@line_item_date)}" do
Expand Down
4 changes: 4 additions & 0 deletions test/system/quotes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class QuotesTest < ApplicationSystemTestCase
@quote = Quote.ordered.first
end

test "#total_price returns the sum of the total price of all line items" do
assert_equal 2500, quotes(:first).total_price
end

test "Showing a quote" do
visit quotes_path
click_link @quote.name
Expand Down

0 comments on commit f5b1215

Please sign in to comment.