From 17debd9fad33a52ba715099c0252947ddc2d373a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 5 Mar 2014 11:25:07 +1100 Subject: [PATCH] On product list view, do not show variants that are out of stock --- app/models/spree/variant_decorator.rb | 2 ++ app/views/shop/shop/_products.html.haml | 2 +- app/views/shop/shop/products.rabl | 2 +- .../consumer/shopping/shopping_spec.rb | 25 +++++++++++++++---- spec/models/spree/variant_spec.rb | 19 ++++++++++++++ 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 00ad88e47b..923dce4e11 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -14,6 +14,8 @@ Spree::Variant.class_eval do after_save :update_units + scope :in_stock, where('spree_variants.count_on_hand > 0 OR spree_variants.on_demand=?', true) + def price_with_fees(distributor, order_cycle) price + fees_for(distributor, order_cycle) diff --git a/app/views/shop/shop/_products.html.haml b/app/views/shop/shop/_products.html.haml index 3456ec29e7..500a918ad8 100644 --- a/app/views/shop/shop/_products.html.haml +++ b/app/views/shop/shop/_products.html.haml @@ -12,7 +12,7 @@ %th.bulk Bulk %th.price.text-right Price %tbody{"ng-repeat" => "product in data.products | filter:query"} - %tr.product + %tr{"class" => "product product-{{ product.id }}"} %td.name %img{"ng-src" => "{{ product.master.images[0].small_url }}"} %div diff --git a/app/views/shop/shop/products.rabl b/app/views/shop/shop/products.rabl index 6d5cfa3129..662196953e 100644 --- a/app/views/shop/shop/products.rabl +++ b/app/views/shop/shop/products.rabl @@ -23,7 +23,7 @@ child :master => :master do end node :variants do |product| - product.variants_for(current_order_cycle, current_distributor).map do |v| + product.variants_for(current_order_cycle, current_distributor).in_stock.map do |v| {id: v.id, is_master: v.is_master, count_on_hand: v.count_on_hand, diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index b7e085db10..5b94b9ff6a 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -12,6 +12,7 @@ feature "As a consumer I want to shop with a distributor", js: true do visit "/" click_link distributor.name end + it "shows a distributor" do visit shop_path page.should have_text distributor.name @@ -23,7 +24,7 @@ feature "As a consumer I want to shop with a distributor", js: true do first("#about img")['src'].should == distributor.promo_image.url(:large) end - describe "With products in order cycles" do + describe "with products in order cycles" do let(:supplier) { create(:supplier_enterprise) } let(:product) { create(:product, supplier: supplier) } let(:order_cycle) { create(:order_cycle, distributors: [distributor], coordinator: create(:distributor_enterprise)) } @@ -112,7 +113,7 @@ feature "As a consumer I want to shop with a distributor", js: true do end end - describe "After selecting an order cycle with products visible" 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) } @@ -154,13 +155,17 @@ feature "As a consumer I want to shop with a distributor", js: true do end end - describe "Filtering on hand and on demand products" do + 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) } + let(:p5) { create(:simple_product, on_demand: false) } + let(:v1) { create(:variant, product: p4, unit_value: 2) } + let(:v2) { create(:variant, product: p4, unit_value: 3, on_demand: false) } + let(:v3) { create(:variant, product: p5) } + let(:v4) { create(:variant, product: p5) } before do p1.master.count_on_hand = 1 @@ -169,12 +174,17 @@ feature "As a consumer I want to shop with a distributor", js: true do p2.master.update_attribute(:count_on_hand, 0) p3.master.update_attribute(:count_on_hand, 0) v1.update_attribute(:count_on_hand, 1) + v2.update_attribute(:count_on_hand, 0) + v3.update_attribute(:count_on_hand, 1) + v4.update_attribute(:count_on_hand, 0) 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 + exchange.variants << v1 + exchange.variants << v2 + exchange.variants << v4 visit shop_path select "frogs", :from => "order_cycle_id" exchange @@ -190,6 +200,11 @@ feature "As a consumer I want to shop with a distributor", js: true do it "does not show products that are neither on hand or on demand" do page.should_not have_content p3.name end + + it "does not show variants that are neither on hand or on demand" do + within(".product-#{p4.id}") { find(".expand", visible: true).trigger "click" } + page.should_not have_content v2.options_text + end end describe "group buy products" do diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 2031a4d22b..fee4dc1edd 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -2,6 +2,25 @@ require 'spec_helper' module Spree describe Variant do + describe "scopes" do + describe "finding variants in stock" do + before do + p = create(:product) + @v_in_stock = create(:variant, product: p) + @v_on_demand = create(:variant, product: p, on_demand: true) + @v_no_stock = create(:variant, product: p) + + @v_in_stock.update_attribute(:count_on_hand, 1) + @v_on_demand.update_attribute(:count_on_hand, 0) + @v_no_stock.update_attribute(:count_on_hand, 0) + end + + it "returns variants in stock or on demand, but not those that are neither" do + Variant.where(is_master: false).in_stock.should == [@v_in_stock, @v_on_demand] + end + end + end + describe "calculating the price with enterprise fees" do it "returns the price plus the fees" do distributor = double(:distributor)