diff --git a/app/controllers/public/catalog_controller.rb b/app/controllers/public/catalog_controller.rb index 4bc9cfd..5cde9e3 100644 --- a/app/controllers/public/catalog_controller.rb +++ b/app/controllers/public/catalog_controller.rb @@ -1,10 +1,13 @@ class CatalogController < PublicController needs_profile + no_design_blocks before_filter :check_enterprise_and_environment def index - @products = @profile.products.paginate(:order => 'name asc', :per_page => 9, :page => params[:page]) + @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]) end protected diff --git a/test/functional/catalog_controller_test.rb b/test/functional/catalog_controller_test.rb index 0779405..aad916e 100644 --- a/test/functional/catalog_controller_test.rb +++ b/test/functional/catalog_controller_test.rb @@ -109,4 +109,53 @@ class CatalogControllerTest < ActionController::TestCase assert_tag :tag => 'span', :content => 'This is Plugin2 speaking!', :attributes => {:id => 'plugin2'} end + should 'get categories of the right level' do + pc1 = ProductCategory.create!(:name => "PC1", :environment => @enterprise.environment) + pc2 = ProductCategory.create!(:name => "PC2", :environment => @enterprise.environment, :parent_id => pc1.id) + pc3 = ProductCategory.create!(:name => "PC3", :environment => @enterprise.environment, :parent_id => pc1.id) + pc4 = ProductCategory.create!(:name => "PC4", :environment => @enterprise.environment, :parent_id => pc2.id) + 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, :level => pc1.id + + assert_not_includes assigns(:categories), pc1 + assert_includes assigns(:categories), pc2 + assert_includes assigns(:categories), pc3 + assert_not_includes assigns(:categories), pc4 + end + + should 'filter products based on level selected' do + pc1 = ProductCategory.create!(:name => "PC1", :environment => @enterprise.environment) + pc2 = ProductCategory.create!(:name => "PC2", :environment => @enterprise.environment, :parent_id => pc1.id) + pc3 = ProductCategory.create!(:name => "PC3", :environment => @enterprise.environment, :parent_id => pc1.id) + pc4 = ProductCategory.create!(:name => "PC4", :environment => @enterprise.environment, :parent_id => pc2.id) + 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, :level => pc2.id + + assert_not_includes assigns(:products), p1 + assert_includes assigns(:products), p2 + assert_not_includes assigns(:products), p3 + assert_includes assigns(:products), p4 + end + + should 'get products ordered by availability, highlighted and then name' do + p1 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Zebra', :available => true, :highlighted => true) + p2 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Car', :available => true) + p3 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Panda', :available => true) + p4 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Pen', :available => false, :highlighted => true) + p5 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Ball', :available => false) + p6 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Medal', :available => false) + + get :index, :profile => @enterprise.identifier + + assert_equal [p1,p2,p3,p4,p5,p6], assigns(:products) + end + end -- libgit2 0.21.2