From a5f478bde708eb87beec27576f7dd17f6a79b2ca Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Mon, 28 Jul 2014 13:33:13 +1000 Subject: [PATCH] Ajax submission of max quantity --- .../darkswarm/services/cart.js.coffee | 5 +++- app/models/spree/order_decorator.rb | 24 +++++++++++++++++++ app/models/spree/order_populator_decorator.rb | 13 +++++----- .../consumer/shopping/shopping_spec.rb | 11 +++++---- spec/models/spree/order_populator_spec.rb | 11 ++++++++- spec/models/spree/order_spec.rb | 3 +-- spec/support/request/ui_component_helper.rb | 11 +++++++++ 7 files changed, 62 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index 4e17d40d72..2d91eab370 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -24,7 +24,9 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> data: => variants = {} for li in @line_items_present() - variants[li.variant.id] = li.quantity + variants[li.variant.id] = + quantity: li.quantity + max_quantity: li.max_quantity {variants: variants} @@ -56,4 +58,5 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> variant.line_item = variant: variant quantity: 0 + max_quantity: null @line_items.push variant.line_item diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index c0f4a8fb4f..4f2be99fa8 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -92,6 +92,30 @@ Spree::Order.class_eval do end end + # Overridden to support max_quantity + def add_variant(variant, quantity = 1, max_quantity = nil, currency = nil) + current_item = find_line_item_by_variant(variant) + if current_item + current_item.quantity += quantity + current_item.max_quantity += max_quantity.to_i + current_item.currency = currency unless currency.nil? + current_item.save + else + current_item = Spree::LineItem.new(:quantity => quantity, max_quantity: max_quantity) + current_item.variant = variant + if currency + current_item.currency = currency unless currency.nil? + current_item.price = variant.price_in(currency).amount + else + current_item.price = variant.price + end + self.line_items << current_item + end + + self.reload + current_item + end + def set_distributor!(distributor) self.distributor = distributor self.order_cycle = nil unless self.order_cycle.andand.has_distributor? distributor diff --git a/app/models/spree/order_populator_decorator.rb b/app/models/spree/order_populator_decorator.rb index d7d4dc24bf..4bc1cdf610 100644 --- a/app/models/spree/order_populator_decorator.rb +++ b/app/models/spree/order_populator_decorator.rb @@ -10,12 +10,13 @@ Spree::OrderPopulator.class_eval do if valid? @order.with_lock do @order.empty! if overwrite - from_hash[:products].each do |product_id,variant_id| + + from_hash[:products].each do |product_id, variant_id| attempt_cart_add(variant_id, from_hash[:quantity]) end if from_hash[:products] - from_hash[:variants].each do |variant_id, quantity| - attempt_cart_add(variant_id, quantity) + from_hash[:variants].each do |variant_id, args| + attempt_cart_add(variant_id, args[:quantity], args[:max_quantity]) end if from_hash[:variants] end end @@ -23,16 +24,14 @@ Spree::OrderPopulator.class_eval do valid? end - # Copied from Spree::OrderPopulator, with additional validations added - def attempt_cart_add(variant_id, quantity) + def attempt_cart_add(variant_id, quantity, max_quantity = nil) quantity = quantity.to_i variant = Spree::Variant.find(variant_id) if quantity > 0 if check_stock_levels(variant, quantity) && check_order_cycle_provided_for(variant) && check_variant_available_under_distribution(variant) - - @order.add_variant(variant, quantity, currency) + @order.add_variant(variant, quantity, max_quantity, currency) end end end diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index 1b6ecd883b..288e628b38 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -168,9 +168,12 @@ feature "As a consumer I want to shop with a distributor", js: true do it "should save group buy data to ze cart" do fill_in "variants[#{variant.id}]", with: 6 fill_in "variant_attributes[#{variant.id}][max_quantity]", with: 7 - add_to_cart - page.should have_content product.name + page.should have_in_cart product.name li = Spree::Order.order(:created_at).last.line_items.order(:created_at).last + while li == nil + sleep 0.1 + li = Spree::Order.order(:created_at).last.line_items.order(:created_at).last + end li.max_quantity.should == 7 li.quantity.should == 6 end @@ -188,9 +191,7 @@ feature "As a consumer I want to shop with a distributor", js: true do end it "should let us add products to our cart" do fill_in "variants[#{variant.id}]", with: "1" - add_to_cart - current_path.should == "/cart" - page.should have_content product.name + page.should have_in_cart product.name end end diff --git a/spec/models/spree/order_populator_spec.rb b/spec/models/spree/order_populator_spec.rb index a18fd952f0..bfe2e123f8 100644 --- a/spec/models/spree/order_populator_spec.rb +++ b/spec/models/spree/order_populator_spec.rb @@ -34,6 +34,15 @@ module Spree order.should_receive(:with_lock) op.populate(params, true) end + + it "attempts cart add with max_quantity" do + op.stub(:distribution_can_supply_products_in_cart).and_return true + order.should_receive(:empty!) + params = {variants: {"1" => {quantity: 1, max_quantity: 2}}} + order.stub(:with_lock).and_yield + op.should_receive(:attempt_cart_add).with("1", 1, 2).and_return true + op.populate(params, true) + end end describe "attempt_cart_add" do @@ -46,7 +55,7 @@ module Spree op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true) op.should_receive(:check_variant_available_under_distribution).with(variant). and_return(true) - order.should_receive(:add_variant).with(variant, quantity, currency) + order.should_receive(:add_variant).with(variant, quantity, nil, currency) op.attempt_cart_add(333, quantity.to_s) end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 3a7c19d775..898a08d075 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -9,8 +9,7 @@ describe Spree::Order do subject.distributor = d subject.save! - subject.add_variant(p.master, 1) - subject.set_variant_attributes(p.master, {'max_quantity' => '3'}) + subject.add_variant(p.master, 1, 3) li = Spree::LineItem.last li.max_quantity.should == 3 diff --git a/spec/support/request/ui_component_helper.rb b/spec/support/request/ui_component_helper.rb index 26472492c7..759f2a0557 100644 --- a/spec/support/request/ui_component_helper.rb +++ b/spec/support/request/ui_component_helper.rb @@ -56,6 +56,17 @@ module UIComponentHelper have_content "An email with instructions on resetting your password has been sent!" end + def have_in_cart name + show_cart + within "li.cart" do + have_content name + end + end + + def show_cart + find("#cart").click + end + def be_logged_in_as(user_or_email) if user_or_email.is_a? Spree::User have_content user_or_email.email