From 93e3f823921293f9a5aec6dddf736d6b77d75c83 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 21 Jun 2012 16:15:40 +1000 Subject: [PATCH] Display product count next to taxons in sidebar --- app/controllers/application_controller.rb | 4 +-- app/helpers/spree/base_helper_decorator.rb | 19 +++++++++++++ spec/requests/consumer/taxonomy_spec.rb | 33 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/helpers/spree/base_helper_decorator.rb create mode 100644 spec/requests/consumer/taxonomy_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 17058d8c66..d6380e04e9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,10 @@ class ApplicationController < ActionController::Base protect_from_forgery - before_filter :load_suppliers_and_distributors + before_filter :load_data_for_sidebar private - def load_suppliers_and_distributors + def load_data_for_sidebar @suppliers = Spree::Supplier.all @distributors = Spree::Distributor.all end diff --git a/app/helpers/spree/base_helper_decorator.rb b/app/helpers/spree/base_helper_decorator.rb new file mode 100644 index 0000000000..54036a2213 --- /dev/null +++ b/app/helpers/spree/base_helper_decorator.rb @@ -0,0 +1,19 @@ +module Spree + BaseHelper.class_eval do + def taxons_tree(root_taxon, current_taxon, max_level = 1) + return '' if max_level < 1 || root_taxon.children.empty? + 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 + content_tag :li, :class => css_class do + link_to(taxon.name, seo_url(taxon)) + + " (#{num_products})" + + taxons_tree(taxon, current_taxon, max_level - 1) + end + end.join("\n").html_safe + end + end + + end +end diff --git a/spec/requests/consumer/taxonomy_spec.rb b/spec/requests/consumer/taxonomy_spec.rb new file mode 100644 index 0000000000..ae36d558dc --- /dev/null +++ b/spec/requests/consumer/taxonomy_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +feature %q{ + As a consumer + I want to see product counts (for my chosen distributor) next to each taxon + So that I can locate products (at my chosen distributor) +} do + include AuthenticationWorkflow + include WebHelper + + scenario "viewing product counts when no distributor selected" do + # Given some taxons and some products + 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) + + 1.times { create(:product, :taxons => [taxon_one]) } + 2.times { create(:product, :taxons => [taxon_two]) } + 3.times { create(:product, :taxons => [taxon_three]) } + + # When I visit the home page + visit spree.root_path + + # Then I should see product counts next to the taxons + page.should have_selector 'nav#taxonomies li', :text => 'Taxon one (1)' + page.should have_selector 'nav#taxonomies li', :text => 'Taxon two (2)' + page.should have_selector 'nav#taxonomies li', :text => 'Taxon three (3)' + end + +end