Skip to content

Commit 79664ea

Browse files
committed
quote line item dates CRUD
1 parent cb60d63 commit 79664ea

19 files changed

+188
-7
lines changed

app/assets/stylesheets/application.sass.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@import "components/navbar";
1818
@import "components/flash";
1919
@import "components/empty_state";
20+
@import "components/line_item_date";
2021

2122
// Layouts
2223
@import "layouts/container";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.line-item-date {
2+
margin-top: var(--space-xl);
3+
margin-bottom: var(--space-xxs);
4+
5+
&__header {
6+
display: flex;
7+
align-items: center;
8+
justify-content: space-between;
9+
gap: var(--space-xs);
10+
}
11+
12+
&__title {
13+
font-size: var(--font-size-xl);
14+
15+
@include media(tabletAndUp) {
16+
font-size: var(--font-size-xxl);
17+
}
18+
}
19+
20+
&__actions {
21+
display: flex;
22+
gap: var(--space-xs);
23+
}
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class LineItemDatesController < ApplicationController
2+
before_action :set_quote
3+
before_action :set_line_item_date, only: [:edit, :update, :destroy]
4+
5+
def new
6+
@line_item_date = @quote.line_item_dates.build
7+
end
8+
9+
def create
10+
@line_item_date = @quote.line_item_dates.build(line_item_date_params)
11+
12+
if @line_item_date.save
13+
redirect_to quote_path(@quote), notice: "Date was successfully created."
14+
else
15+
render :new, status: :unprocessable_entity
16+
end
17+
end
18+
19+
def edit
20+
end
21+
22+
def update
23+
if @line_item_date.update(line_item_date_params)
24+
redirect_to quote_path(@quote), notice: "Date was successfully updated."
25+
else
26+
render :edit, status: :unprocessable_entity
27+
end
28+
end
29+
30+
def destroy
31+
@line_item_date.destroy
32+
33+
redirect_to quote_path(@quote), notice: "Date was successfully destroyed."
34+
end
35+
36+
private
37+
38+
def line_item_date_params
39+
params.require(:line_item_date).permit(:date)
40+
end
41+
42+
def set_line_item_date
43+
@line_item_date = @quote.line_item_dates.find(params[:id])
44+
end
45+
46+
def set_quote
47+
@quote = current_company.quotes.find(params[:quote_id])
48+
end
49+
end

app/controllers/quotes_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def index
77
end
88

99
def show
10+
@line_item_dates = @quote.line_item_dates.ordered
1011
end
1112

1213
def new

app/helpers/application_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ module ApplicationHelper
22
def render_turbo_stream_flash_messages
33
turbo_stream.prepend "flash", partial: "layouts/flash"
44
end
5+
6+
def form_error_notification(object)
7+
if object.errors.any?
8+
tag.div class: "error-message" do
9+
object.errors.full_messages.to_sentence.capitalize
10+
end
11+
end
12+
end
513
end

app/models/line_item_date.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class LineItemDate < ApplicationRecord
2+
belongs_to :quote
3+
4+
validates :date, presence: true, uniqueness: { scope: :quote_id }
5+
6+
scope :ordered, -> { order(date: :asc) }
7+
end

app/models/quote.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Quote < ApplicationRecord
22
belongs_to :company
3+
has_many :line_item_dates, dependent: :destroy
34

45
validates :name, presence: true
56

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<%= simple_form_for [quote, line_item_date], html: { class: "form line-item-date" } do |f| %>
2+
<%= form_error_notification(line_item_date) %>
3+
4+
<%= f.input :date, html5: true, input_html: { autofocus: true } %>
5+
<%= link_to "Cancel", quote_path(quote), class: "btn btn--light" %>
6+
<%= f.submit class: "btn btn--secondary" %>
7+
<% end %>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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>
6+
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" %>
17+
</div>
18+
</div>
19+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<main class="container">
2+
<%= link_to sanitize("&larr; Back to quote"), quote_path(@quote) %>
3+
4+
<div class="header">
5+
<h1>Edit date</h1>
6+
</div>
7+
8+
<%= render "form", quote: @quote, line_item_date: @line_item_date %>
9+
</main>

0 commit comments

Comments
 (0)