From 32510c0a5e907db1b4dfa4f9ba436ebe564fba21 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 4 Mar 2013 15:24:35 -0300 Subject: [PATCH] Sorting catalog categories by name --- app/controllers/public/catalog_controller.rb | 2 +- app/helpers/catalog_helper.rb | 2 +- test/functional/catalog_controller_test.rb | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/controllers/public/catalog_controller.rb b/app/controllers/public/catalog_controller.rb index 5cde9e3..cd75f73 100644 --- a/app/controllers/public/catalog_controller.rb +++ b/app/controllers/public/catalog_controller.rb @@ -7,7 +7,7 @@ class CatalogController < PublicController def index @category = params[:level] ? ProductCategory.find(params[:level]) : nil @products = @profile.products.from_category(@category).paginate(:order => 'available desc, highlighted desc, name asc', :per_page => 9, :page => params[:page]) - @categories = ProductCategory.on_level(params[:level]) + @categories = ProductCategory.on_level(params[:level]).order(:name) end protected diff --git a/app/helpers/catalog_helper.rb b/app/helpers/catalog_helper.rb index 99ef1b4..b6c3da1 100644 --- a/app/helpers/catalog_helper.rb +++ b/app/helpers/catalog_helper.rb @@ -21,7 +21,7 @@ module CatalogHelper def category_sub_links(category) sub_categories = [] - category.children.each do |sub_category| + category.children.order(:name).each do |sub_category| sub_categories << category_link(sub_category, true) end content_tag('ul', sub_categories) if sub_categories.size > 1 diff --git a/test/functional/catalog_controller_test.rb b/test/functional/catalog_controller_test.rb index 168df43..3638a7a 100644 --- a/test/functional/catalog_controller_test.rb +++ b/test/functional/catalog_controller_test.rb @@ -224,4 +224,21 @@ class CatalogControllerTest < ActionController::TestCase assert_tag :tag => 'li', :attributes => {:id => "product-#{p2.id}", :class => /not-available/} end + should 'sort categories by name' do + environment = @enterprise.environment + environment.categories.destroy_all + pc1 = ProductCategory.create!(:name => "Drinks", :environment => environment) + pc2 = ProductCategory.create!(:name => "Bananas", :environment => environment) + pc3 = ProductCategory.create!(:name => "Sodas", :environment => environment) + pc4 = ProductCategory.create!(:name => "Pies", :environment => environment) + p1 = fast_create(Product, :product_category_id => pc1.id, :enterprise_id => @enterprise.id) + p2 = fast_create(Product, :product_category_id => pc2.id, :enterprise_id => @enterprise.id) + p3 = fast_create(Product, :product_category_id => pc3.id, :enterprise_id => @enterprise.id) + p4 = fast_create(Product, :product_category_id => pc4.id, :enterprise_id => @enterprise.id) + + get :index, :profile => @enterprise.identifier + + assert_equal [pc2, pc1, pc4, pc3], assigns(:categories) + end + end -- libgit2 0.21.2