From b6590000a4018fc0c4e81426771bf69e7fe99544 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 17 Jan 2014 14:41:15 +1100 Subject: [PATCH] Improving Product's has_stock, filtering Shop#products to in_stock products --- app/controllers/shop_controller.rb | 4 ++- app/models/spree/product_decorator.rb | 5 +++ spec/features/consumer/shopping_spec.rb | 42 ++++++++++++++++++++++++- spec/models/spree/product_spec.rb | 8 +++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index dde9af99f4..3b96d7e8d3 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -10,7 +10,9 @@ class ShopController < BaseController end def products - unless @products = current_order_cycle.andand.products_distributed_by(@distributor) + unless @products = current_order_cycle.andand + .products_distributed_by(@distributor) + .select(&:has_stock?) render json: "", status: 404 end end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 1377ba97ba..8d54edc189 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -92,6 +92,11 @@ Spree::Product.class_eval do def product_distribution_for(distributor) self.product_distributions.find_by_distributor_id(distributor) end + + # overriding to check self.on_demand as well + def has_stock? + has_variants? ? variants.any?(&:in_stock?) : (on_demand || master.in_stock?) + end # Build a product distribution for each distributor def build_product_distributions_for_user user diff --git a/spec/features/consumer/shopping_spec.rb b/spec/features/consumer/shopping_spec.rb index e976320381..f9d53fd94e 100644 --- a/spec/features/consumer/shopping_spec.rb +++ b/spec/features/consumer/shopping_spec.rb @@ -95,7 +95,7 @@ feature "As a consumer I want to shop with a distributor", js: true do end end - describe "with products with variants" do + describe "After selecting an order cycle with products visible" do let(:oc) { create(:simple_order_cycle, distributors: [distributor]) } let(:product) { create(:simple_product) } let(:variant) { create(:variant, product: product) } @@ -107,6 +107,45 @@ feature "As a consumer I want to shop with a distributor", js: true do it "should not show quantity field for product with variants" do page.should_not have_selector("#variants_#{product.master.id}", visible: true) end + + end + + describe "Filtering on hand and on demand products" do + let(:oc) { create(:simple_order_cycle, distributors: [distributor]) } + let(:p1) { create(:simple_product, on_demand: false) } + let(:p2) { create(:simple_product, on_demand: true) } + let(:p3) { create(:simple_product, on_demand: false) } + let(:p4) { create(:simple_product, on_demand: false) } + let(:v1) { create(:variant, product: p4) } + + before do + p1.master.count_on_hand = 1 + p2.master.count_on_hand = 0 + p1.master.update_attribute(:count_on_hand, 1) + p2.master.update_attribute(:count_on_hand, 0) + p3.master.update_attribute(:count_on_hand, 0) + v1.update_attribute(:count_on_hand, 1) + exchange = Exchange.find(oc.exchanges.to_enterprises(distributor).outgoing.first.id) + exchange.update_attribute :pickup_time, "frogs" + exchange.variants << p1.master + exchange.variants << p2.master + exchange.variants << p3.master + exchange.variants << v1 + visit shop_path + select "frogs", :from => "order_cycle_id" + exchange + end + + it "shows on hand products" do + page.should have_content p1.name + page.should have_content p4.name + end + it "shows on demand products" do + page.should have_content p2.name + end + it "does not show products that are neither on hand or on demand" do + page.should_not have_content p3.name + end end describe "adding products to cart" do @@ -151,4 +190,5 @@ def build_and_select_order_cycle exchange.variants << variant visit shop_path select "frogs", :from => "order_cycle_id" + exchange end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 2b7a9b8c69..6c7dfb9091 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -411,5 +411,13 @@ module Spree Spree::Product.all_variant_unit_option_types.sort.should == [ot1, ot2, ot3].sort end end + + describe "Stock filtering" do + it "considers products that are on_demand as being in stock" do + product = create(:simple_product, on_demand: true) + product.master.update_attribute(:count_on_hand, 0) + product.has_stock?.should == true + end + end end end