Improving Product's has_stock, filtering Shop#products to in_stock products

This commit is contained in:
Will Marshall
2014-01-17 14:41:15 +11:00
parent 6f5ff0ab2c
commit b6590000a4
4 changed files with 57 additions and 2 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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