From 2be42e591c913abc01acb3efc5e55ed7eb808959 Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Wed, 8 Oct 2008 18:16:59 -0300 Subject: [PATCH] ActionItem726: new products begin selecting top level categories --- app/controllers/my_profile/manage_products_controller.rb | 8 ++++---- app/views/manage_products/_subcategories.rhtml | 27 ++++++++++++--------------- test/functional/manage_products_controller_test.rb | 11 ++++++++++- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb index f462c0b..32938d1 100644 --- a/app/controllers/my_profile/manage_products_controller.rb +++ b/app/controllers/my_profile/manage_products_controller.rb @@ -23,9 +23,8 @@ class ManageProductsController < ApplicationController end def new - @current_category = ProductCategory.top_level_for(environment).first @object = Product.new - @categories = @current_category.nil? ? [] : @current_category.children + @categories = @current_category.nil? ? ProductCategory.top_level_for(environment) : @current_category.children @product = @profile.products.build(params[:product]) @product.build_image unless @product.image if request.post? @@ -75,9 +74,10 @@ class ManageProductsController < ApplicationController end render :partial => 'shared/select_categories', :locals => {:object_name => 'product', :multiple => false}, :layout => false end + def update_subcategories - @current_category = ProductCategory.find(params[:id]) - @categories = @current_category.children + @current_category = ProductCategory.find(params[:id]) if params[:id] + @categories = @current_category ? @current_category.children : ProductCategory.top_level_for(environment) render :partial => 'subcategories' end diff --git a/app/views/manage_products/_subcategories.rhtml b/app/views/manage_products/_subcategories.rhtml index c6f58ac..bc3c060 100644 --- a/app/views/manage_products/_subcategories.rhtml +++ b/app/views/manage_products/_subcategories.rhtml @@ -1,24 +1,21 @@ -<% if !@current_category.nil? and !@current_category.top_level? %> +<% if !@current_category.nil? %>

<%= _('Current category:') %>

<%= hidden_field_tag 'product[product_category_id]', @current_category.id %> - <% - categories = [@current_category] - categories.push(@current_category) while @current_category = @current_category.parent - categories.last.name = _('Back') + <%= link_to_remote( _('Back'), + :update => "subcategories", + :url => { :action => 'update_subcategories', :id => nil, :loaded => visual_effect(:highlight, "subcategories") }, + :class => 'select-current-category-link') + ' → ' + %> + <%= ([@current_category] + @current_category.ancestors).reverse.map{|i| + link_to_remote(i.name, + :update => "subcategories", + :url => { :action => 'update_subcategories', :id => i, :loaded => visual_effect(:highlight, "subcategories") }, + :class => 'select-current-category-link')}.join(' → ') %> - <% if categories.size > 1 %> - <%= categories.compact.reverse.map{|i| - link_to_remote(i.name, - :update => "subcategories", - :url => { :action => 'update_subcategories', :id => i, :loaded => visual_effect(:highlight, "subcategories") }, - :class => 'select-current-category-link')}.join(' → ') - %> - <% end %> - <% end %> -<% if @current_category %> +<% if @current_category.nil? %>

<%= _('Select a category:') %>

<% elsif !@categories.empty? %>

<%= _('Select a subcategory:') %>

diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb index a2185d7..7229055 100644 --- a/test/functional/manage_products_controller_test.rb +++ b/test/functional/manage_products_controller_test.rb @@ -120,7 +120,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2) get :new, :profile => @enterprise.identifier assert_tag :tag => 'p', :content => /Select a category:/ - assert_tag :tag => 'a', :content => /#{category2.name}/ + assert_tag :tag => 'a', :content => /#{category1.name}/ end should 'show current category' do @@ -265,5 +265,14 @@ class ManageProductsControllerTest < Test::Unit::TestCase assert_template 'not_found.rhtml' end + + should 'show top level product categories for the user to choose' do + pc1 = ProductCategory.create!(:name => 'test_category1', :environment => Environment.default) + pc2 = ProductCategory.create!(:name => 'test_category2', :environment => Environment.default) + + get :new, :profile => @enterprise.identifier + + assert_equivalent [pc1, pc2], assigns(:categories) + end end -- libgit2 0.21.2