From d09123eb22ead8f7efce92321d1fd6a413ba7944 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 17 Oct 2012 07:36:18 +1100 Subject: [PATCH] Do not show suppliers with no products in stock --- app/models/spree/supplier.rb | 14 ++++++++-- .../spree/products/_source_sidebar.html.haml | 3 +- spec/models/suppliers_spec.rb | 28 +++++++++++++++---- spec/requests/consumer/suppliers_spec.rb | 10 +++++-- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index ea2cba4e8d..93cb9aa675 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -12,6 +12,17 @@ module Spree after_initialize :initialize_country before_validation :set_unused_address_fields + def has_products_on_hand? + self.products.where('count_on_hand > 0').present? + end + + def to_param + "#{id}-#{name.parameterize}" + end + + + private + def initialize_country self.address ||= Address.new self.address.country = Country.find_by_id(Spree::Config[:default_country_id]) if self.address.new_record? @@ -21,8 +32,5 @@ module Spree address.firstname = address.lastname = address.phone = 'unused' if address.present? end - def to_param - "#{id}-#{name.parameterize}" - end end end diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index da178106da..ee4e43c526 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -2,7 +2,8 @@ %h6.filter_name Shop by Supplier %ul.filter_choices - @suppliers.each do |supplier| - %li.nowrap= link_to supplier.name, supplier + - if supplier.has_products_on_hand? + %li.nowrap= link_to supplier.name, supplier %h6.filter_name Shop by Distributor %ul.filter_choices diff --git a/spec/models/suppliers_spec.rb b/spec/models/suppliers_spec.rb index d731c65a81..7ddc4f8cab 100644 --- a/spec/models/suppliers_spec.rb +++ b/spec/models/suppliers_spec.rb @@ -8,14 +8,32 @@ module Spree it { should belong_to(:address) } end - it "should default country to system country" do - supplier = Supplier.new - supplier.address.country.should == Country.find_by_id(Config[:default_country_id]) - end - describe "validations" do it { should validate_presence_of(:name) } end + it "should default country to system country" do + subject.address.country.should == Country.find_by_id(Config[:default_country_id]) + end + + context "has_products_on_hand?" do + before :each do + @supplier = create(:supplier) + end + + it "returns false when no products" do + @supplier.should_not have_products_on_hand + end + + it "returns false when the product is out of stock" do + create(:product, :supplier => @supplier, :on_hand => 0) + @supplier.should_not have_products_on_hand + end + + it "returns true when the product is in stock" do + create(:product, :supplier => @supplier, :on_hand => 1) + @supplier.should have_products_on_hand + end + end end end diff --git a/spec/requests/consumer/suppliers_spec.rb b/spec/requests/consumer/suppliers_spec.rb index 13f231911b..46f5da25a3 100644 --- a/spec/requests/consumer/suppliers_spec.rb +++ b/spec/requests/consumer/suppliers_spec.rb @@ -14,11 +14,17 @@ feature %q{ s2 = create(:supplier) s3 = create(:supplier) + # And some of those suppliers have a product + create(:product, :supplier => s1) + create(:product, :supplier => s3) + # When I go to the home page visit spree.root_path - # Then I should see a list containing all the suppliers - [s1, s2, s3].each { |s| page.should have_selector 'a', :text => s.name } + # Then I should see a list containing all the suppliers that have products in stock + page.should have_selector 'a', :text => s1.name + page.should have_selector 'a', :text => s3.name + page.should_not have_selector 'a', :text => s2.name end scenario "viewing products provided by a supplier" do