active_cart 0.0.10 → 0.0.10.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.10.1
@@ -94,6 +94,22 @@ module ActiveCart
94
94
  end
95
95
  self.reload
96
96
  end
97
+
98
+ #:nodoc
99
+ def update_cart(item, quantity = 1)
100
+ cart_item = find_cart_item(item)
101
+ if cart_item
102
+ diff = quantity - cart_item.quantity
103
+
104
+ if diff < 0
105
+ return remove_from_cart(item, -1 * diff)
106
+ else
107
+ return add_to_cart(item, diff)
108
+ end
109
+ else
110
+ return add_to_cart(item, quantity)
111
+ end
112
+ end
97
113
  end
98
114
 
99
115
  aasm_column self.aac_config[:state_column]
@@ -425,6 +425,42 @@ class ActsAsCartTest < ActiveSupport::TestCase
425
425
  end
426
426
  end
427
427
 
428
+ context 'update_cart' do
429
+ should 'update the number of items in the cart if the item is in the cart' do
430
+ item = Item.make
431
+
432
+ @cart.add_to_cart(item, 10)
433
+ assert_equal 1, @cart.size
434
+ assert_equal 10, @cart.quantity
435
+
436
+ @cart.update_cart(item, 20)
437
+ assert_equal 1, @cart.size
438
+ assert_equal 20, @cart.quantity
439
+ end
440
+
441
+ should 'set the given quantity of items in to the cart if the item is not yet in the cart' do
442
+ item = Item.make
443
+
444
+ @cart.update_cart(item, 20)
445
+ assert_equal 1, @cart.size
446
+ assert_equal 20, @cart.quantity
447
+ end
448
+
449
+ should 'set the given quantity of item in to the cart if the requested value is lower than the current quantity' do
450
+ item = Item.make
451
+
452
+ @cart.add_to_cart(item, 10)
453
+ assert_equal 1, @cart.size
454
+ assert_equal 10, @cart.quantity
455
+
456
+ @cart.update_cart(item, 4)
457
+ assert_equal 1, @cart.size
458
+ assert_equal 4, @cart.quantity
459
+ end
460
+ end
461
+
462
+
463
+
428
464
  context 'remove_from_cart' do
429
465
  should 'remove the quantity supplied from the cart' do
430
466
  item = Item.make
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_cart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myles Eftos