-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
5c8cd75
commit 0772dab
Showing
7 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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,37 @@ | ||
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 | ||
item = cart.line_items.build(product_id: product_id) | ||
item.quantity = quantity | ||
item.save! | ||
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 | ||
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