Commit 86f1c20e544ec7c9151a29f14f30c5263b87224b

Authored by Rodrigo Souto
1 parent 3d572c26

[catalog-categories] Filtering products on catalog by category

app/controllers/public/catalog_controller.rb
1 1 class CatalogController < PublicController
2 2 needs_profile
  3 + no_design_blocks
3 4  
4 5 before_filter :check_enterprise_and_environment
5 6  
6 7 def index
7   - @products = @profile.products.paginate(:order => 'name asc', :per_page => 9, :page => params[:page])
  8 + @category = params[:level] ? ProductCategory.find(params[:level]) : nil
  9 + @products = @profile.products.from_category(@category).paginate(:order => 'available desc, highlighted desc, name asc', :per_page => 9, :page => params[:page])
  10 + @categories = ProductCategory.on_level(params[:level])
8 11 end
9 12  
10 13 protected
... ...
test/functional/catalog_controller_test.rb
... ... @@ -109,4 +109,53 @@ class CatalogControllerTest &lt; ActionController::TestCase
109 109 assert_tag :tag => 'span', :content => 'This is Plugin2 speaking!', :attributes => {:id => 'plugin2'}
110 110 end
111 111  
  112 + should 'get categories of the right level' do
  113 + pc1 = ProductCategory.create!(:name => "PC1", :environment => @enterprise.environment)
  114 + pc2 = ProductCategory.create!(:name => "PC2", :environment => @enterprise.environment, :parent_id => pc1.id)
  115 + pc3 = ProductCategory.create!(:name => "PC3", :environment => @enterprise.environment, :parent_id => pc1.id)
  116 + pc4 = ProductCategory.create!(:name => "PC4", :environment => @enterprise.environment, :parent_id => pc2.id)
  117 + p1 = fast_create(Product, :product_category_id => pc1.id, :enterprise_id => @enterprise.id)
  118 + p2 = fast_create(Product, :product_category_id => pc2.id, :enterprise_id => @enterprise.id)
  119 + p3 = fast_create(Product, :product_category_id => pc3.id, :enterprise_id => @enterprise.id)
  120 + p4 = fast_create(Product, :product_category_id => pc4.id, :enterprise_id => @enterprise.id)
  121 +
  122 + get :index, :profile => @enterprise.identifier, :level => pc1.id
  123 +
  124 + assert_not_includes assigns(:categories), pc1
  125 + assert_includes assigns(:categories), pc2
  126 + assert_includes assigns(:categories), pc3
  127 + assert_not_includes assigns(:categories), pc4
  128 + end
  129 +
  130 + should 'filter products based on level selected' do
  131 + pc1 = ProductCategory.create!(:name => "PC1", :environment => @enterprise.environment)
  132 + pc2 = ProductCategory.create!(:name => "PC2", :environment => @enterprise.environment, :parent_id => pc1.id)
  133 + pc3 = ProductCategory.create!(:name => "PC3", :environment => @enterprise.environment, :parent_id => pc1.id)
  134 + pc4 = ProductCategory.create!(:name => "PC4", :environment => @enterprise.environment, :parent_id => pc2.id)
  135 + p1 = fast_create(Product, :product_category_id => pc1.id, :enterprise_id => @enterprise.id)
  136 + p2 = fast_create(Product, :product_category_id => pc2.id, :enterprise_id => @enterprise.id)
  137 + p3 = fast_create(Product, :product_category_id => pc3.id, :enterprise_id => @enterprise.id)
  138 + p4 = fast_create(Product, :product_category_id => pc4.id, :enterprise_id => @enterprise.id)
  139 +
  140 + get :index, :profile => @enterprise.identifier, :level => pc2.id
  141 +
  142 + assert_not_includes assigns(:products), p1
  143 + assert_includes assigns(:products), p2
  144 + assert_not_includes assigns(:products), p3
  145 + assert_includes assigns(:products), p4
  146 + end
  147 +
  148 + should 'get products ordered by availability, highlighted and then name' do
  149 + p1 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Zebra', :available => true, :highlighted => true)
  150 + p2 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Car', :available => true)
  151 + p3 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Panda', :available => true)
  152 + p4 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Pen', :available => false, :highlighted => true)
  153 + p5 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Ball', :available => false)
  154 + p6 = fast_create(Product, :enterprise_id => @enterprise.id, :name => 'Medal', :available => false)
  155 +
  156 + get :index, :profile => @enterprise.identifier
  157 +
  158 + assert_equal [p1,p2,p3,p4,p5,p6], assigns(:products)
  159 + end
  160 +
112 161 end
... ...