Skip to content

Commit ccba9ee

Browse files
committed
Finishing Card and Orders
1 parent 12cf011 commit ccba9ee

18 files changed

Lines changed: 210 additions & 110 deletions

'ap'

Whitespace-only changes.

app/assets/stylesheets/custom.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
/* mixins, variables, etc. */
55
$gray-medium-light: #eaeaea;
66

7+
hr {
8+
-moz-border-bottom-colors: none;
9+
-moz-border-image: none;
10+
-moz-border-left-colors: none;
11+
-moz-border-right-colors: none;
12+
-moz-border-top-colors: none;
13+
border-color: #EEEEEE -moz-use-text-color #FFFFFF;
14+
border-style: solid none;
15+
border-width: 1px 0;
16+
margin: 18px 0;
17+
}
718

819
/* miscellaneous */
920
@mixin box_sizing {

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ def user_is_admin
3737
redirect_back(fallback_location: root_url)
3838
end
3939
end
40+
4041
end

app/controllers/carts_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class CartsController < ApplicationController
2+
before_action :logged_in_user, only: %i[show destroy]
3+
24
def show
35
@cart = @current_cart
46
end

app/controllers/line_items_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
class LineItemsController < ApplicationControllers
1+
class LineItemsController < ApplicationController
2+
before_action :logged_in_user, only: %i[create destroy add_quantity reduce_quantity]
23

34
def create
45
# Find associated product and current cart
@@ -15,10 +16,12 @@ def create
1516
@line_item = LineItem.new
1617
@line_item.cart = current_cart
1718
@line_item.product = chosen_product
19+
@line_item.quantity = 1
1820
end
1921

2022
# Save and redirect to cart show path
21-
@line_item.save
23+
@line_item.save!
24+
2225
redirect_to cart_path(current_cart)
2326
end
2427

app/controllers/orders_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class OrdersController < ApplicationController
2+
before_action :logged_in_user, only: %i[index show new create]
3+
24
def index
35
@orders = Order.all
46
end
@@ -13,14 +15,15 @@ def new
1315

1416
def create
1517
@order = Order.new(order_params)
18+
@order.update(user_id: @current_user.id)
1619
@current_cart.line_items.each do |item|
1720
@order.line_items << item
1821
item.cart_id = nil
1922
end
2023
@order.save
2124
Cart.destroy(session[:cart_id])
2225
session[:cart_id] = nil
23-
redirect_to root_path
26+
redirect_to orders_path
2427
end
2528

2629
private

app/controllers/products_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ class ProductsController < ApplicationController
55
# GET /products
66
# GET /products.json
77
def index
8-
@products = Product.all.paginate(page: params[:page], per_page: 5)
9-
@current_user = current_user
8+
# @products = Product.all.paginate(page: params[:page], per_page: 5)
9+
# @current_user = current_user
10+
redirect_to root_url
1011
end
1112

1213
# GET /products/1

app/models/cart.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Cart < ApplicationRecord
33
has_many :products, through: :line_items
44

55
# LOGIC
6-
def sub_total
6+
def sub_total
77
sum = 0
88
self.line_items.each do |line_item|
99
sum+= line_item.total_price

app/models/line_item.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
class LineItem < ApplicationRecord
22
belongs_to :product
33
belongs_to :cart
4-
belongs_to :order
4+
# belongs_to :order
55

66
# LOGIC
77
def total_price
8-
self.quantity * self.product.price
8+
if !quantity.to_s.strip.empty? && !product.price.to_s.strip.empty?
9+
quantity.to_s.to_d * product.price.to_s.to_d
10+
else
11+
0.0
12+
end
913
end
1014
end

app/views/carts/show.html.erb

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
<div class="center jumbotron">
22
<p>
3-
Card Show
3+
Your Card => Total Items (<%= @cart.line_items.count %>)
44
</p>
5-
<p>
6-
Total Items (<%= @cart.line_items.count %>)
7-
</p>
8-
<ul>
5+
<ul class="users">
96
<% @cart.line_items.each do |line_item| %>
10-
<li>Item:
11-
<%= line_item.product.name %>/ Price:
12-
<%= number_to_currency(line_item.product.price) %>/ Quantity:
13-
<%= line_item.quantity %>/ Total Price:
7+
<%= render line_item.product %>
8+
<li>Quantity:
9+
<%= line_item.quantity %>
10+
| Total Price:
1411
<%= number_to_currency(line_item.total_price) %>
1512
<!-- See lineItem section below for explanation of line_item links -->
1613
<ul>
17-
<li>
18-
<%= link_to "Add one", line_item_add_path(:id => line_item), method: :post %>
19-
<%= link_to "Reduce one", line_item_reduce_path(:id => line_item), method: :post %>
20-
</li>
21-
<li>
22-
<%= link_to "Remove item", line_item_path(line_item), method: :delete %>
23-
</li>
24-
</ul>
14+
<!-- <li> -->
15+
<!-- <%#= link_to "Add one", line_item_add_path(:id => line_item), method: :post %> -->
16+
17+
<!-- reduce -->
18+
<%=link_to line_item_reduce_path(:id => line_item) , method: :post, class: "btn btn-danger" , style: "margin-top:15px;" do %>
19+
<i class="glyphicon glyphicon-minus" style="color:black;"></i>
20+
Reduce
21+
<% end %>
2522

23+
<!-- add -->
24+
<%=link_to line_item_add_path(:id => line_item) , method: :post , class: "btn btn-success" , style: "margin-top:15px;" do %>
25+
<i class="glyphicon glyphicon-plus" style="color:black;"></i>
26+
Add
2627
<% end %>
27-
</ul>
28-
<h3>Total price:
29-
<%= number_to_currency(@cart.sub_total) %></h3>
30-
<!-- See Order section below for explanation of new_order_path -->
31-
<%= link_to "Empty cart", cart_path(@current_cart),method: :delete, data: {confirm: "Are you sure?"} %>
32-
|
33-
<%= link_to "Proceed to checkout", new_order_path %>
28+
29+
<!-- Remove -->
30+
<%=link_to line_item_path(line_item) ,method: :delete ,class: "btn btn-warning" , style: "margin-top:15px;" do %>
31+
<i class="glyphicon glyphicon-remove" style="color:black;"></i>
32+
Remove Item
33+
<% end %>
34+
35+
<!-- <%#= link_to "Reduce one", line_item_reduce_path(:id => line_item), method: :post %> -->
36+
<!-- </li> -->
37+
<!-- <li> -->
38+
<%#= link_to "Remove item", line_item_path(line_item), method: :delete %>
39+
<!-- </li> -->
40+
<hr />
41+
</ul>
42+
43+
<% end %>
44+
</ul>
45+
<h3>Total price:
46+
<%= number_to_currency(@cart.sub_total) %></h3>
47+
<!-- See Order section below for explanation of new_order_path -->
48+
<%= link_to "Empty cart", cart_path(@current_cart),method: :delete, data: {confirm: "Are you sure?"} %>
49+
|
50+
<%= link_to "Proceed to checkout", new_order_path %>
3451
</div>

0 commit comments

Comments
 (0)