My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 151 attachment: 151_154_patch (4.5 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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: test/functional/store_controller_test.rb
===================================================================
--- test/functional/store_controller_test.rb (revision 163)
+++ test/functional/store_controller_test.rb (working copy)
@@ -184,6 +184,12 @@
a_cart = assigns(:order)
# It should not have added anything.
assert_equal a_cart.items.length, 2
+
+ # Try adding a product with a non-numerical quantity
+ a_product = items(:towel)
+ xhr(:post, :add_to_cart_ajax, :id => a_product.id, :quantity => "a")
+ a_cart = assigns(:order)
+ assert_equal a_cart.items.length, 1
end


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]
Index: app/controllers/store_controller.rb
===================================================================
--- app/controllers/store_controller.rb (revision 163)
+++ app/controllers/store_controller.rb (working copy)
@@ -173,19 +173,23 @@
else
product = Product.find(params[:id])
end
- quantity = params[:quantity]
- quantity ||= 1

+ begin
+ quantity = params[:quantity].to_int
+ rescue NoMethodError
+ quantity = (params[:quantity].to_i != 0) ? params[:quantity].to_i : 1
+ end
+
logger.info "QUANTITY: #{quantity}"
logger.info "PRODUCT QUANTITY: #{product.quantity}"
logger.info "Quantity too much? #{(quantity.to_i > product.quantity.to_i)}"

# Checks quantity against available.
- if quantity.to_i > product.quantity.to_i
+ if quantity > product.quantity.to_i
logger.info "There's an error adding to the cart..."
render :nothing => true, :status => 400 and return
else
- @order.add_product(product, quantity.to_i)
+ @order.add_product(product, quantity)
logger.info "Product added...success"
render :partial => 'cart' and return
end
Powered by Google Project Hosting