From 8f8a1191cb2866d209985ddc7922c039773468a9 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 22 Apr 2016 11:44:04 +1000 Subject: [PATCH] Remove stock cap on max_quantity --- .../darkswarm/services/cart.js.coffee | 2 +- .../shop_variant_with_group_buy.html.haml | 2 +- app/models/spree/line_item_decorator.rb | 7 +------ app/models/spree/order_populator_decorator.rb | 2 +- spec/features/consumer/shopping/shopping_spec.rb | 16 +++++++++------- .../unit/darkswarm/services/cart_spec.js.coffee | 4 ++-- spec/models/spree/line_item_spec.rb | 4 ++-- spec/models/spree/order_populator_spec.rb | 4 ++-- 8 files changed, 19 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index d75ef7a628..9ee03e26b3 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -54,7 +54,7 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo if li.quantity > li.variant.count_on_hand li.quantity = li.variant.count_on_hand scope.variants.push li.variant - if li.max_quantity > li.variant.count_on_hand + if li.variant.count_on_hand == 0 && li.max_quantity > li.variant.count_on_hand li.max_quantity = li.variant.count_on_hand scope.variants.push(li.variant) unless li.variant in scope.variants diff --git a/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml b/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml index c44a71948b..f2b0b95837 100644 --- a/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml +++ b/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml @@ -18,6 +18,6 @@ "ng-model" => "variant.line_item.max_quantity", placeholder: "{{'shop_variant_quantity_max' | t}}", "ofn-disable-scroll" => true, - max: "{{variant.on_demand && 9999 || variant.count_on_hand }}", + min: "{{variant.line_item.quantity}}", name: "variant_attributes[{{variant.id}}][max_quantity]", id: "variants_{{variant.id}}_max"} diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index b9f8af577f..8a89d8fdef 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -44,12 +44,7 @@ Spree::LineItem.class_eval do def cap_quantity_at_stock! - attrs = {} - - attrs[:quantity] = variant.on_hand if quantity > variant.on_hand - attrs[:max_quantity] = variant.on_hand if (max_quantity || 0) > variant.on_hand - - update_attributes!(attrs) if attrs.any? + update_attributes!(quantity: variant.on_hand) if quantity > variant.on_hand end diff --git a/app/models/spree/order_populator_decorator.rb b/app/models/spree/order_populator_decorator.rb index a106de6936..db03f1f2e2 100644 --- a/app/models/spree/order_populator_decorator.rb +++ b/app/models/spree/order_populator_decorator.rb @@ -77,7 +77,7 @@ Spree::OrderPopulator.class_eval do on_hand = variant.on_hand on_hand = [quantity, max_quantity].compact.max if Spree::Config.allow_backorders quantity_to_add = [quantity, on_hand].min - max_quantity_to_add = [max_quantity, on_hand].min if max_quantity + max_quantity_to_add = max_quantity # max_quantity is not capped [quantity_to_add, max_quantity_to_add] end diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index 9dc0a39dac..5b617e7db6 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -288,33 +288,32 @@ feature "As a consumer I want to shop with a distributor", js: true do # Update amount available in product list # If amount falls to zero, variant should be greyed out and input disabled page.should have_selector "#variant-#{variant.id}.out-of-stock" - page.should have_selector "#variants_#{variant.id}_max[max='0']" page.should have_selector "#variants_#{variant.id}_max[disabled='disabled']" end end context "when the update is for another product" do it "updates quantity" do - fill_in "variants[#{variant.id}]", with: '1' + fill_in "variants[#{variant.id}]", with: '2' wait_until { !cart_dirty } - variant.update_attributes! on_hand: 0 + variant.update_attributes! on_hand: 1 fill_in "variants[#{variant2.id}]", with: '1' wait_until { !cart_dirty } within(".out-of-stock-modal") do page.should have_content "stock levels for one or more of the products in your cart have reduced" - page.should have_content "#{product.name} - #{variant.unit_to_display} is now out of stock." + page.should have_content "#{product.name} - #{variant.unit_to_display} now only has 1 remaining" end end context "group buy products" do let(:product) { create(:simple_product, group_buy: true) } - it "updates max_quantity" do - fill_in "variants[#{variant.id}]", with: '1' - fill_in "variant_attributes[#{variant.id}][max_quantity]", with: '2' + it "does not update max_quantity" do + fill_in "variants[#{variant.id}]", with: '2' + fill_in "variant_attributes[#{variant.id}][max_quantity]", with: '3' wait_until { !cart_dirty } variant.update_attributes! on_hand: 1 @@ -325,6 +324,9 @@ feature "As a consumer I want to shop with a distributor", js: true do page.should have_content "stock levels for one or more of the products in your cart have reduced" page.should have_content "#{product.name} - #{variant.unit_to_display} now only has 1 remaining" end + + page.should have_field "variants[#{variant.id}]", with: '1' + page.should have_field "variant_attributes[#{variant.id}][max_quantity]", with: '3' end end end diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 6df74e3ede..d985904f81 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -159,12 +159,12 @@ describe 'Cart service', -> expect(li.quantity).toEqual 5 expect(li.max_quantity).toBeUndefined() - it "reduces the max_quantity in the cart", -> + it "does not reduce the max_quantity in the cart", -> li = {variant: {id: 1}, quantity: 6, max_quantity: 7} stockLevels = {1: {quantity: 5, max_quantity: 5, on_hand: 5}} spyOn(Cart, 'line_items_present').andReturn [li] Cart.compareAndNotifyStockLevels stockLevels - expect(li.max_quantity).toEqual 5 + expect(li.max_quantity).toEqual 7 it "resets the count on hand available", -> li = {variant: {id: 1}, quantity: 6} diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 2b5362e94f..111108f7b4 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -55,9 +55,9 @@ module Spree li.reload.quantity.should == 5 end - it "caps max_quantity" do + it "does not cap max_quantity" do li.cap_quantity_at_stock! - li.reload.max_quantity.should == 5 + li.reload.max_quantity.should == 10 end it "works for products without max_quantity" do diff --git a/spec/models/spree/order_populator_spec.rb b/spec/models/spree/order_populator_spec.rb index 4a50e59fa8..f0bbc81f55 100644 --- a/spec/models/spree/order_populator_spec.rb +++ b/spec/models/spree/order_populator_spec.rb @@ -213,8 +213,8 @@ module Spree op.quantities_to_add(v, 5, 6).should == [5, 6] end - it "returns a limited amount when not entirely available" do - op.quantities_to_add(v, 15, 16).should == [10, 10] + it "also returns the full amount when not entirely available" do + op.quantities_to_add(v, 15, 16).should == [10, 16] end end end