Only show distributors in sidebar that have products in stock

This commit is contained in:
Rohan Mitchell
2012-08-02 11:49:10 +10:00
parent 93867682bb
commit 63bf25293d
4 changed files with 9 additions and 6 deletions

View File

@@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base
private
def load_data_for_sidebar
@suppliers = Spree::Supplier.all
@distributors = Spree::Distributor.with_products
@distributors = Spree::Distributor.with_products_on_hand
end
end

View File

@@ -13,7 +13,7 @@ module Spree
validates_associated :pickup_address
scope :by_name, order('name')
scope :with_products, joins(:products).group('distributors.id')
scope :with_products_on_hand, joins(:products).where('spree_products.count_on_hand > 0').group('distributors.id')
after_initialize :initialize_country
before_validation :set_unused_address_fields

View File

@@ -40,6 +40,7 @@ end
FactoryGirl.modify do
factory :simple_product do
supplier { Spree::Supplier.first || FactoryGirl.create(:supplier) }
on_hand 3
# before(:create) do |product, evaluator|
# product.product_distributions = [FactoryGirl.create(:product_distribution, :product => product)]

View File

@@ -19,14 +19,16 @@ module Spree
end
describe "scopes" do
it "returns distributors with products" do
it "returns distributors with products in stock" do
d1 = create(:distributor)
d2 = create(:distributor)
d3 = create(:distributor)
create(:product, :distributors => [d1, d2])
create(:product, :distributors => [d1])
d4 = create(:distributor)
create(:product, :distributors => [d1, d2], :on_hand => 5)
create(:product, :distributors => [d1], :on_hand => 5)
create(:product, :distributors => [d3], :on_hand => 0)
Distributor.with_products.sort.should == [d1, d2]
Distributor.with_products_on_hand.sort.should == [d1, d2]
end
end