Commit 2be42e591c913abc01acb3efc5e55ed7eb808959

Authored by Moises Machado
1 parent a70c44b1

ActionItem726: new products begin selecting top level categories

app/controllers/my_profile/manage_products_controller.rb
@@ -23,9 +23,8 @@ class ManageProductsController < ApplicationController @@ -23,9 +23,8 @@ class ManageProductsController < ApplicationController
23 end 23 end
24 24
25 def new 25 def new
26 - @current_category = ProductCategory.top_level_for(environment).first  
27 @object = Product.new 26 @object = Product.new
28 - @categories = @current_category.nil? ? [] : @current_category.children 27 + @categories = @current_category.nil? ? ProductCategory.top_level_for(environment) : @current_category.children
29 @product = @profile.products.build(params[:product]) 28 @product = @profile.products.build(params[:product])
30 @product.build_image unless @product.image 29 @product.build_image unless @product.image
31 if request.post? 30 if request.post?
@@ -75,9 +74,10 @@ class ManageProductsController < ApplicationController @@ -75,9 +74,10 @@ class ManageProductsController < ApplicationController
75 end 74 end
76 render :partial => 'shared/select_categories', :locals => {:object_name => 'product', :multiple => false}, :layout => false 75 render :partial => 'shared/select_categories', :locals => {:object_name => 'product', :multiple => false}, :layout => false
77 end 76 end
  77 +
78 def update_subcategories 78 def update_subcategories
79 - @current_category = ProductCategory.find(params[:id])  
80 - @categories = @current_category.children 79 + @current_category = ProductCategory.find(params[:id]) if params[:id]
  80 + @categories = @current_category ? @current_category.children : ProductCategory.top_level_for(environment)
81 render :partial => 'subcategories' 81 render :partial => 'subcategories'
82 end 82 end
83 83
app/views/manage_products/_subcategories.rhtml
1 -<% if !@current_category.nil? and !@current_category.top_level? %> 1 +<% if !@current_category.nil? %>
2 2
3 <p><%= _('Current category:') %></p> 3 <p><%= _('Current category:') %></p>
4 <%= hidden_field_tag 'product[product_category_id]', @current_category.id %> 4 <%= hidden_field_tag 'product[product_category_id]', @current_category.id %>
5 - <%  
6 - categories = [@current_category]  
7 - categories.push(@current_category) while @current_category = @current_category.parent  
8 - categories.last.name = _('Back') 5 + <%= link_to_remote( _('Back'),
  6 + :update => "subcategories",
  7 + :url => { :action => 'update_subcategories', :id => nil, :loaded => visual_effect(:highlight, "subcategories") },
  8 + :class => 'select-current-category-link') + ' &rarr; '
  9 + %>
  10 + <%= ([@current_category] + @current_category.ancestors).reverse.map{|i|
  11 + link_to_remote(i.name,
  12 + :update => "subcategories",
  13 + :url => { :action => 'update_subcategories', :id => i, :loaded => visual_effect(:highlight, "subcategories") },
  14 + :class => 'select-current-category-link')}.join(' &rarr; ')
9 %> 15 %>
10 - <% if categories.size > 1 %>  
11 - <%= categories.compact.reverse.map{|i|  
12 - link_to_remote(i.name,  
13 - :update => "subcategories",  
14 - :url => { :action => 'update_subcategories', :id => i, :loaded => visual_effect(:highlight, "subcategories") },  
15 - :class => 'select-current-category-link')}.join(' &rarr; ')  
16 - %>  
17 - <% end %>  
18 -  
19 <% end %> 16 <% end %>
20 17
21 -<% if @current_category %> 18 +<% if @current_category.nil? %>
22 <p><%= _('Select a category:') %></p> 19 <p><%= _('Select a category:') %></p>
23 <% elsif !@categories.empty? %> 20 <% elsif !@categories.empty? %>
24 <p><%= _('Select a subcategory:') %></p> 21 <p><%= _('Select a subcategory:') %></p>
test/functional/manage_products_controller_test.rb
@@ -120,7 +120,7 @@ class ManageProductsControllerTest &lt; Test::Unit::TestCase @@ -120,7 +120,7 @@ class ManageProductsControllerTest &lt; Test::Unit::TestCase
120 category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2) 120 category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2)
121 get :new, :profile => @enterprise.identifier 121 get :new, :profile => @enterprise.identifier
122 assert_tag :tag => 'p', :content => /Select a category:/ 122 assert_tag :tag => 'p', :content => /Select a category:/
123 - assert_tag :tag => 'a', :content => /#{category2.name}/ 123 + assert_tag :tag => 'a', :content => /#{category1.name}/
124 end 124 end
125 125
126 should 'show current category' do 126 should 'show current category' do
@@ -265,5 +265,14 @@ class ManageProductsControllerTest &lt; Test::Unit::TestCase @@ -265,5 +265,14 @@ class ManageProductsControllerTest &lt; Test::Unit::TestCase
265 265
266 assert_template 'not_found.rhtml' 266 assert_template 'not_found.rhtml'
267 end 267 end
  268 +
  269 + should 'show top level product categories for the user to choose' do
  270 + pc1 = ProductCategory.create!(:name => 'test_category1', :environment => Environment.default)
  271 + pc2 = ProductCategory.create!(:name => 'test_category2', :environment => Environment.default)
  272 +
  273 + get :new, :profile => @enterprise.identifier
  274 +
  275 + assert_equivalent [pc1, pc2], assigns(:categories)
  276 + end
268 277
269 end 278 end