From 1d3800696ee9a89be8298615beff9207732f680a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 18 Dec 2014 10:40:56 +1100 Subject: [PATCH] Variant overrides can override stock levels --- app/controllers/shop_controller.rb | 13 ++++++--- app/models/spree/product_decorator.rb | 2 +- lib/open_food_network/scope_product_to_hub.rb | 16 +++++++++++ .../shopping/variant_overrides_spec.rb | 28 +++++++++++++++---- 4 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 lib/open_food_network/scope_product_to_hub.rb diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 2c1b4cd6d5..b229666524 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -1,3 +1,5 @@ +require 'open_food_network/scope_product_to_hub' + class ShopController < BaseController layout "darkswarm" before_filter :require_distributor_chosen @@ -10,10 +12,13 @@ class ShopController < BaseController def products # Can we make this query less slow? # - if @products = current_order_cycle.andand - .valid_products_distributed_by(current_distributor).andand - .select { |p| !p.deleted? && p.has_stock_for_distribution?(current_order_cycle, current_distributor) }.andand - .sort_by {|p| p.name } + if current_order_cycle + @products = current_order_cycle. + valid_products_distributed_by(current_distributor). + each { |p| p.scope_to_hub current_distributor }. + select { |p| !p.deleted? && p.has_stock_for_distribution?(current_order_cycle, current_distributor) }. + sort_by(&:name) + render status: 200, json: ActiveModel::ArraySerializer.new(@products, each_serializer: Api::ProductSerializer, current_order_cycle: current_order_cycle, current_distributor: current_distributor).to_json diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 4ecdd55bfc..cc8b213bde 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -137,7 +137,7 @@ Spree::Product.class_eval do # This product has stock for a distribution if it is available on-demand # or if one of its variants in the distribution is in stock (!has_variants? && on_demand) || - variants_distributed_by(order_cycle, distributor).any? { |v| v.in_stock? } + variants_distributed_by(order_cycle, distributor).any?(&:in_stock?) end def variants_distributed_by(order_cycle, distributor) diff --git a/lib/open_food_network/scope_product_to_hub.rb b/lib/open_food_network/scope_product_to_hub.rb new file mode 100644 index 0000000000..7d5e0aa067 --- /dev/null +++ b/lib/open_food_network/scope_product_to_hub.rb @@ -0,0 +1,16 @@ +require 'open_food_network/scope_variant_to_hub' + +module OpenFoodNetwork + module ScopeProductToHub + def variants_distributed_by(order_cycle, distributor) + super.each { |v| v.scope_to_hub @hub } + end + end +end + +Spree::Product.class_eval do + def scope_to_hub(hub) + extend OpenFoodNetwork::ScopeProductToHub + @hub = hub + end +end diff --git a/spec/features/consumer/shopping/variant_overrides_spec.rb b/spec/features/consumer/shopping/variant_overrides_spec.rb index 9f44fbcf83..f96eeddc61 100644 --- a/spec/features/consumer/shopping/variant_overrides_spec.rb +++ b/spec/features/consumer/shopping/variant_overrides_spec.rb @@ -13,26 +13,42 @@ feature "shopping with variant overrides defined", js: true do let(:producer) { create(:supplier_enterprise) } let(:oc) { create(:simple_order_cycle, suppliers: [producer], coordinator: hub, distributors: [hub]) } let(:outgoing_exchange) { oc.exchanges.outgoing.first } - let(:product) { create(:simple_product, supplier: producer) } - let(:variant) { create(:variant, product: product, price: 11.11) } - let!(:vo) { create(:variant_override, hub: hub, variant: variant, price: 22.22) } + let(:p1) { create(:simple_product, supplier: producer) } + let(:p2) { create(:simple_product, supplier: producer) } + let(:v1) { create(:variant, product: p1, price: 11.11, unit_value: 1) } + let(:v2) { create(:variant, product: p1, price: 22.22, unit_value: 2) } + let(:v3) { create(:variant, product: p2, price: 33.33, unit_value: 3) } + let!(:vo1) { create(:variant_override, hub: hub, variant: v1, price: 99.99) } + let!(:vo2) { create(:variant_override, hub: hub, variant: v2, count_on_hand: 0) } + let!(:vo3) { create(:variant_override, hub: hub, variant: v3, count_on_hand: 0) } before do - outgoing_exchange.variants << variant + outgoing_exchange.variants << v1 + outgoing_exchange.variants << v2 + outgoing_exchange.variants << v3 visit shop_path click_link hub.name end it "shows the overridden price" do page.should_not have_price "$11.11" - page.should have_price "$22.22" + page.should have_price "$99.99" + end + + it "looks up stock from the override" do + # Product should appear but one of the variants is out of stock + page.should_not have_content v2.options_text + + # Entire product should not appear - no stock + page.should_not have_content p2.name + page.should_not have_content v3.options_text end - it "takes stock from the override" it "calculates fees correctly" it "shows the overridden price with fees in the quick cart" it "shows the correct prices in the shopping cart" it "shows the correct prices in the checkout" it "creates the order with the correct prices" + it "subtracts stock from the override" end end