Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
209 additions
and
128 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
// Place all the styles related to the carts controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ | ||
/* START_HIGHLIGHT */ | ||
.carts { | ||
.cart_title { | ||
font: 120% bold; | ||
} | ||
|
||
.item_price, .total_line { | ||
text-align: right; | ||
} | ||
|
||
.total_line .total_cell { | ||
font-weight: bold; | ||
border-top: 1px solid #595; | ||
} | ||
} | ||
/* END_HIGHLIGHT */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
class Cart < ActiveRecord::Base | ||
has_many :line_items, dependent: :destroy | ||
end | ||
|
||
def add_product(product_id) | ||
current_item = line_items.find_by_product_id(product_id) | ||
if current_item | ||
current_item.quantity += 1 | ||
else | ||
current_item = line_items.build(product_id: product_id) | ||
end | ||
current_item | ||
end | ||
def total_price | ||
line_items.to_a.sum { |item| item.total_price } | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
class LineItem < ActiveRecord::Base | ||
belongs_to :product | ||
belongs_to :cart | ||
|
||
def total_price | ||
product.price * quantity | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
<% if notice %> | ||
<p id="notice"><%= notice %></p> | ||
<% end %> | ||
<h2>Your Pragmatic Cart</h2> | ||
<ul> | ||
<div class="cart_title">Your Cart</div> | ||
<table> | ||
<% @cart.line_items.each do |item| %> | ||
<li><%= item.product.title %></li> | ||
<tr> | ||
<td><%= item.quantity %>×</td> | ||
<td><%= item.product.title %></td> | ||
<td class="item_price"><%= number_to_currency(item.total_price) %></td> | ||
</tr> | ||
<% end %> | ||
</ul> | ||
<tr class="total_line"> | ||
<td colspan="2">Total</td> | ||
<td class="total_cell"><%= number_to_currency(@cart.total_price) %></td> | ||
</tr> | ||
</table> | ||
<%= button_to 'Empty cart', @cart, method: :delete, | ||
confirm: 'Are you sure?' %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddQuantityToLineItems < ActiveRecord::Migration | ||
def change | ||
add_column :line_items, :quantity, :integer, default: 1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class CombineItemsInCart < ActiveRecord::Migration | ||
def up | ||
# replace multiple items for a single product in a cart with a single item | ||
Cart.all.each do |cart| | ||
# count the number of each product in the cart | ||
sums = cart.line_items.group(:product_id).sum(:quantity) | ||
sums.each do |product_id, quantity| | ||
if quantity > 1 | ||
# remove individual items | ||
cart.line_items.where(product_id: product_id).delete_all | ||
# replace with a single item | ||
cart.line_items.create(product_id: product_id, quantity: quantity) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def down | ||
# split items with quantity>1 into multiple items | ||
LineItem.where("quantity>1").each do |line_item| | ||
# add individual items | ||
line_item.quantity.times do | ||
LineItem.create cart_id: line_item.cart_id, | ||
product_id: line_item.product_id, quantity: 1 | ||
end | ||
# remove original item | ||
line_item.destroy | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters