mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-03 02:21:33 +00:00
Display product count next to taxons in sidebar
This commit is contained in:
@@ -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
|
||||
|
||||
19
app/helpers/spree/base_helper_decorator.rb
Normal file
19
app/helpers/spree/base_helper_decorator.rb
Normal file
@@ -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
|
||||
33
spec/requests/consumer/taxonomy_spec.rb
Normal file
33
spec/requests/consumer/taxonomy_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user