diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb index 8f4a137..99cdaa6 100644 --- a/app/controllers/my_profile/manage_products_controller.rb +++ b/app/controllers/my_profile/manage_products_controller.rb @@ -12,6 +12,8 @@ class ManageProductsController < ApplicationController end def new + @current_category = ProductCategory.top_level_for(environment).first + @categories = @current_category.nil? ? [] : @current_category.children @product = @profile.products.build(params[:product]) @product.build_image unless @product.image if request.post? @@ -26,6 +28,8 @@ class ManageProductsController < ApplicationController def edit @product = @profile.products.find(params[:id]) + @current_category = @product.product_category + @categories = @current_category.nil? ? [] : @current_category.children if request.post? if @product.update_attributes(params[:product]) flash[:notice] = _('Product succesfully updated') @@ -46,5 +50,11 @@ class ManageProductsController < ApplicationController redirect_back_or_default :action => 'show', :id => @product end end + + def update_subcategories + @current_category = ProductCategory.find(params[:id]) + @categories = @current_category.children + render :partial => 'subcategories' + end end diff --git a/app/views/manage_products/_form.rhtml b/app/views/manage_products/_form.rhtml index 8957bff..e938fff 100644 --- a/app/views/manage_products/_form.rhtml +++ b/app/views/manage_products/_form.rhtml @@ -5,7 +5,11 @@ <%= display_form_field( _('Price:'), f.text_field(:price) ) %> <%= display_form_field( _('Description:'), f.text_area(:description) ) %> <%= display_form_field( _('Image:'), file_field_tag( "product[image_builder][uploaded_data]" ) ) %> - <%= display_form_field( _('category:'), f.select( :product_category_id,ProductCategory.find(:all).map{|pc|[pc.name,pc.id]}, {:include_blank => true} )) %> + +
+ <%= render :partial => 'subcategories' %> +
+ <% button_bar do %> <%= submit_button('save', (mode == 'new' ? _('Create product') : _('Save changes')), :cancel => {:action => 'index'} ) %> <% end %> diff --git a/app/views/manage_products/_subcategories.rhtml b/app/views/manage_products/_subcategories.rhtml new file mode 100644 index 0000000..3938987 --- /dev/null +++ b/app/views/manage_products/_subcategories.rhtml @@ -0,0 +1,18 @@ +

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

+<% + categories = [@current_category] + categories.push(@current_category) while @current_category and @current_category = @current_category.parent +%> +<%= categories.compact.reverse.map{|i| link_to(i.name, {}, :class => 'select-current-category-link')}.join(' → ') %> + +<% unless @categories.empty? %> +

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

+<% end %> +<% for category in @categories do %> + <%= link_to_remote category.name, { + :update => "subcategories", + :url => { :action => "update_subcategories", :id => category.id }, + :loaded => visual_effect(:highlight, "subcategories") }, + :class => 'select-subcategory-link' + %> +<% end %> diff --git a/public/stylesheets/controller_manage_products.css b/public/stylesheets/controller_manage_products.css new file mode 100644 index 0000000..cdabb1a --- /dev/null +++ b/public/stylesheets/controller_manage_products.css @@ -0,0 +1,11 @@ +a.select-subcategory-link:hover { + background-color: gray; +} +a.select-subcategory-link { + border: 1px solid gray; + padding: 2px 4px 2px 4px; + text-decoration: none; +} +a.select-current-category-link { + /* ... */ +} diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb index cb8cce4..4ed08c8 100644 --- a/test/functional/manage_products_controller_test.rb +++ b/test/functional/manage_products_controller_test.rb @@ -103,5 +103,39 @@ class ManageProductsControllerTest < Test::Unit::TestCase assert Product.find_by_name('test product') end end + + should 'show current category' do + environment = Environment.default + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) + category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) + category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2) + category4 = ProductCategory.create!(:name => 'Category 4', :environment => environment, :parent => category3) + get :new, :profile => @enterprise.identifier + assert_tag :tag => 'p', :content => /Current category:/, :sibling => { :tag => 'a', :content => /#{category1.name}/ } + end + + should 'show subcategories list' do + environment = Environment.default + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) + category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) + get 'new', :profile => @enterprise.identifier + assert !assigns(:categories).empty? + assert_tag :tag => 'p', :content => /Select a subcategory:/, :sibling => { :tag => 'a', :attributes => { :href => '#' }, :content => /#{category2.name}/ } + end + + should 'update subcategories' do + environment = Environment.default + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) + category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) + get 'update_subcategories', :profile => @enterprise.identifier, :id => category1.id + assert_tag :tag => 'a', :attributes => { :href => '#' }, :content => /#{category2.name}/ + end + + should 'not show subcategories list when no subcategories' do + environment = Environment.default + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) + get 'update_subcategories', :profile => @enterprise.identifier, :id => category1.id + assert_no_tag :tag => 'p', :content => 'Select a subcategory:' + end end -- libgit2 0.21.2