diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d6380e04e9..fad8e785cd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base private def load_data_for_sidebar @suppliers = Spree::Supplier.all - @distributors = Spree::Distributor.all + @distributors = Spree::Distributor.with_products end end diff --git a/app/models/spree/distributor.rb b/app/models/spree/distributor.rb index 8ba54538e5..fe920c7820 100644 --- a/app/models/spree/distributor.rb +++ b/app/models/spree/distributor.rb @@ -13,6 +13,7 @@ module Spree validates_associated :pickup_address scope :by_name, order('name') + scope :with_products, joins(:products).group('distributors.id') after_initialize :initialize_country before_validation :set_unused_address_fields diff --git a/spec/models/distributors_spec.rb b/spec/models/distributors_spec.rb index f9db087d70..7614e53cc5 100644 --- a/spec/models/distributors_spec.rb +++ b/spec/models/distributors_spec.rb @@ -9,13 +9,26 @@ module Spree it { should have_many(:orders) } end + describe "validations" do + it { should validate_presence_of(:name) } + end + it "should default country to system country" do distributor = Distributor.new distributor.pickup_address.country.should == Country.find_by_id(Config[:default_country_id]) end - describe "validations" do - it { should validate_presence_of(:name) } + describe "scopes" do + it "returns distributors with products" do + d1 = create(:distributor) + d2 = create(:distributor) + d3 = create(:distributor) + create(:product, :distributors => [d1, d2]) + create(:product, :distributors => [d1]) + + Distributor.with_products.sort.should == [d1, d2] + end end + end end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index f435c4bb0a..6b5cbd32a8 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -13,6 +13,7 @@ feature %q{ d1 = create(:distributor) d2 = create(:distributor) p = create(:product, :distributors => [d1]) + create(:product, :distributors => [d2]) # When I choose a distributor visit spree.root_path diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index 421c05b2c8..eb5e7dc216 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -10,22 +10,28 @@ feature %q{ scenario "viewing a list of distributors" do # Given some distributors - 3.times { create(:distributor) } + d1 = create(:distributor) + d2 = create(:distributor) + d3 = create(:distributor) + + # And some of those distributors have a product + create(:product, :distributors => [d1, d2]) # When I go to the home page visit spree.root_path - # Then I should see a list containing all distributors - Spree::Distributor.all.each do |distributor| - page.should have_selector 'a', :text => distributor.name - end + # Then I should see a list containing the distributors that have products + page.should have_selector 'a', :text => d1.name + page.should have_selector 'a', :text => d2.name + page.should_not have_selector 'a', :text => d3.name end context "when a distributor is selected" do it "displays the distributor's name" do - # Given a distributor + # Given a distributor with a product d = create(:distributor, :name => 'Melb Uni Co-op') + create(:product, :distributors => [d]) # When I select the distributor visit spree.root_path @@ -65,8 +71,9 @@ feature %q{ end it "allows the user to leave the distributor" do - # Given a distributor + # Given a distributor with a product d = create(:distributor, :name => 'Melb Uni Co-op') + create(:product, :distributors => [d]) # When I select the distributor and then leave it visit spree.root_path @@ -106,10 +113,11 @@ feature %q{ end it "works when viewing a product from a remote distributor" do - # Given two distributors and a product under one + # Given two distributors and our product under one distributor_product = create(:distributor) distributor_no_product = create(:distributor) product = create(:product, :distributors => [distributor_product]) + create(:product, :distributors => [distributor_no_product]) # When we select the distributor without the product and then view the product visit spree.root_path