Show distributor-scoped product counts next to taxons

This commit is contained in:
Rohan Mitchell
2012-06-22 13:40:43 +10:00
parent 0c5162f9fb
commit 4b118a26ff
3 changed files with 35 additions and 2 deletions

View File

@@ -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})" +

View File

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

View File

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