My favorites
▼
|
Sign in
substruct
Open-source Ruby on Rails E-Commerce
Project Home
Downloads
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
154
attachment: issue_154_patch
(2.8 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Index: test/unit/order_line_item_test.rb
===================================================================
--- test/unit/order_line_item_test.rb (revision 163)
+++ test/unit/order_line_item_test.rb (working copy)
@@ -41,6 +41,23 @@
}
end
+ # Test valid order_line_item quantities
+ def test_order_line_item_quantity_is_postive
+ a_towel = items(:towel)
+ an_order_line_item = OrderLineItem.for_product(a_towel)
+ an_order_line_item.quantity = -1;
+ assert_raise(ActiveRecord::RecordInvalid) {
+ an_order_line_item.save!
+ }
+ an_order_line_item.quantity = 0
+ assert_raise(ActiveRecord::RecordInvalid) {
+ an_order_line_item.save!
+ }
+ an_order_line_item.quantity = 1
+ assert_nothing_raised {
+ an_order_line_item.save!
+ }
+ end
# TODO: I think that all these methods should be protected.
# Theres no much things to play with, as an order line item should only reflect an item,
Index: test/unit/order_test.rb
===================================================================
--- test/unit/order_test.rb (revision 163)
+++ test/unit/order_test.rb (working copy)
@@ -1294,6 +1294,19 @@
assert_equal 1, a_cart.items.length
assert_equal 4, a_cart.items[0].quantity
end
+
+ # Test if a add_product properly handles negative quantities
+ def test_add_product_with_negative_quantity
+ a_cart = Order.new
+ a_cart.add_product(items(:blue_lightsaber), 2)
+ a_cart.add_product(items(:blue_lightsaber), -1)
+ a_cart.reload
+ # Calling add_product with a negative quantity should remove that many units
+ assert_equal 1, a_cart.items[0].quantity
+ a_cart.add_product(items(:blue_lightsaber), -3)
+# a_cart.reload
+ assert a_cart.empty?
+ end
# Test if a product can be removed from the cart.
def test_remove_product
Index: app/models/order_line_item.rb
===================================================================
--- app/models/order_line_item.rb (revision 163)
+++ app/models/order_line_item.rb (working copy)
@@ -3,6 +3,9 @@
belongs_to :item
belongs_to :order
alias_attribute :price, :unit_price
+
+ validates_numericality_of :quantity, :greater_than_or_equal_to => 1, :only_integer => true, :message => "must be positive"
+
# Creates and returns a line item when a product is passed in
def self.for_product(item)
ol_item = self.new
Index: app/models/order.rb
===================================================================
--- app/models/order.rb (revision 163)
+++ app/models/order.rb (working copy)
@@ -348,6 +348,9 @@
# Adds a product to our shopping cart
def add_product(product, quantity=1)
+ if quantity < 0
+ remove_product(product, quantity.abs) and return
+ end
item = self.order_line_items.find(
:first,
:conditions => ["item_id = ?", product.id]
Powered by
Google Project Hosting