From 4b118a26ffef308e8f4ecd4937e4ddd591cdb64c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 22 Jun 2012 13:40:43 +1000 Subject: [PATCH] Show distributor-scoped product counts next to taxons --- app/helpers/spree/base_helper_decorator.rb | 6 ++++- app/models/spree/product_decorator.rb | 2 +- spec/requests/consumer/taxonomy_spec.rb | 29 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/helpers/spree/base_helper_decorator.rb b/app/helpers/spree/base_helper_decorator.rb index 54036a2213..138d0a4742 100644 --- a/app/helpers/spree/base_helper_decorator.rb +++ b/app/helpers/spree/base_helper_decorator.rb @@ -5,7 +5,11 @@ module Spree content_tag :ul, :class => 'taxons-list' do root_taxon.children.map do |taxon| css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'current' : nil - num_products = Product.in_taxon(taxon).count + + products = Product.in_taxon(taxon) + products = products.in_distributor(current_distributor) if current_distributor + num_products = products.count + content_tag :li, :class => css_class do link_to(taxon.name, seo_url(taxon)) + " (#{num_products})" + diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index f196785ec2..a47b92baa8 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -7,5 +7,5 @@ Spree::Product.class_eval do validates_presence_of :supplier, :distributors scope :in_supplier, lambda { |supplier| where(:supplier_id => supplier) } - scope :in_distributor, lambda { |distributor_id| joins(:distributors).where('distributors.id = ?', distributor_id.to_i) } + scope :in_distributor, lambda { |distributor| joins(:distributors).where('distributors.id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) } end diff --git a/spec/requests/consumer/taxonomy_spec.rb b/spec/requests/consumer/taxonomy_spec.rb index ae36d558dc..ec0b3aec28 100644 --- a/spec/requests/consumer/taxonomy_spec.rb +++ b/spec/requests/consumer/taxonomy_spec.rb @@ -30,4 +30,33 @@ feature %q{ page.should have_selector 'nav#taxonomies li', :text => 'Taxon three (3)' end + + scenario "viewing product counts when a distributor is selected" do + # Given some taxons and some products under distributors + taxonomy = Spree::Taxonomy.find_by_name('Products') || create(:taxonomy, :name => 'Products') + taxonomy_root = taxonomy.root + + taxon_one = create(:taxon, :name => 'Taxon one', :parent_id => taxonomy_root.id) + taxon_two = create(:taxon, :name => 'Taxon two', :parent_id => taxonomy_root.id) + taxon_three = create(:taxon, :name => 'Taxon three', :parent_id => taxonomy_root.id) + + my_distributor = create(:distributor, :name => 'My Distributor') + other_distributor = create(:distributor, :name => 'Other Distributor') + + 1.times { create(:product, :taxons => [taxon_one], :distributors => [other_distributor]) } + 2.times { create(:product, :taxons => [taxon_two], :distributors => [other_distributor]) } + 2.times { create(:product, :taxons => [taxon_three], :distributors => [other_distributor]) } + 2.times { create(:product, :taxons => [taxon_three], :distributors => [my_distributor]) } + + # When I visit the home page and select my distributor + visit spree.root_path + click_link my_distributor.name + page.should have_selector '#current-distributor', :text => 'You are shopping at My Distributor' + + # Then I should see distributor-scoped product counts next to the taxons + page.should have_selector 'nav#taxonomies li', :text => 'Taxon one (0)' + page.should have_selector 'nav#taxonomies li', :text => 'Taxon two (0)' + page.should have_selector 'nav#taxonomies li', :text => 'Taxon three (2)' + end + end