Commit 14319a1428a1e2b10c9a21f1b9aa75fd4699496d

Authored by JoenioCosta
1 parent 59edcdd4

ActionItem266: reimplementing addition of products by enterprises

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1653 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/manage_products_controller.rb
... ... @@ -12,6 +12,8 @@ class ManageProductsController < ApplicationController
12 12 end
13 13  
14 14 def new
  15 + @current_category = ProductCategory.top_level_for(environment).first
  16 + @categories = @current_category.nil? ? [] : @current_category.children
15 17 @product = @profile.products.build(params[:product])
16 18 @product.build_image unless @product.image
17 19 if request.post?
... ... @@ -26,6 +28,8 @@ class ManageProductsController < ApplicationController
26 28  
27 29 def edit
28 30 @product = @profile.products.find(params[:id])
  31 + @current_category = @product.product_category
  32 + @categories = @current_category.nil? ? [] : @current_category.children
29 33 if request.post?
30 34 if @product.update_attributes(params[:product])
31 35 flash[:notice] = _('Product succesfully updated')
... ... @@ -46,5 +50,11 @@ class ManageProductsController < ApplicationController
46 50 redirect_back_or_default :action => 'show', :id => @product
47 51 end
48 52 end
  53 +
  54 + def update_subcategories
  55 + @current_category = ProductCategory.find(params[:id])
  56 + @categories = @current_category.children
  57 + render :partial => 'subcategories'
  58 + end
49 59  
50 60 end
... ...
app/views/manage_products/_form.rhtml
... ... @@ -5,7 +5,11 @@
5 5 <%= display_form_field( _('Price:'), f.text_field(:price) ) %>
6 6 <%= display_form_field( _('Description:'), f.text_area(:description) ) %>
7 7 <%= display_form_field( _('Image:'), file_field_tag( "product[image_builder][uploaded_data]" ) ) %>
8   - <%= display_form_field( _('category:'), f.select( :product_category_id,ProductCategory.find(:all).map{|pc|[pc.name,pc.id]}, {:include_blank => true} )) %>
  8 +
  9 + <div id='subcategories'>
  10 + <%= render :partial => 'subcategories' %>
  11 + </div>
  12 +
9 13 <% button_bar do %>
10 14 <%= submit_button('save', (mode == 'new' ? _('Create product') : _('Save changes')), :cancel => {:action => 'index'} ) %>
11 15 <% end %>
... ...
app/views/manage_products/_subcategories.rhtml 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +<p><%= _('Current category:') %></p>
  2 +<%
  3 + categories = [@current_category]
  4 + categories.push(@current_category) while @current_category and @current_category = @current_category.parent
  5 +%>
  6 +<%= categories.compact.reverse.map{|i| link_to(i.name, {}, :class => 'select-current-category-link')}.join(' &rarr; ') %>
  7 +
  8 +<% unless @categories.empty? %>
  9 + <p><%= _('Select a subcategory:') %></p>
  10 +<% end %>
  11 +<% for category in @categories do %>
  12 + <%= link_to_remote category.name, {
  13 + :update => "subcategories",
  14 + :url => { :action => "update_subcategories", :id => category.id },
  15 + :loaded => visual_effect(:highlight, "subcategories") },
  16 + :class => 'select-subcategory-link'
  17 + %>
  18 +<% end %>
... ...
public/stylesheets/controller_manage_products.css 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +a.select-subcategory-link:hover {
  2 + background-color: gray;
  3 +}
  4 +a.select-subcategory-link {
  5 + border: 1px solid gray;
  6 + padding: 2px 4px 2px 4px;
  7 + text-decoration: none;
  8 +}
  9 +a.select-current-category-link {
  10 + /* ... */
  11 +}
... ...
test/functional/manage_products_controller_test.rb
... ... @@ -103,5 +103,39 @@ class ManageProductsControllerTest &lt; Test::Unit::TestCase
103 103 assert Product.find_by_name('test product')
104 104 end
105 105 end
  106 +
  107 + should 'show current category' do
  108 + environment = Environment.default
  109 + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment)
  110 + category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1)
  111 + category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2)
  112 + category4 = ProductCategory.create!(:name => 'Category 4', :environment => environment, :parent => category3)
  113 + get :new, :profile => @enterprise.identifier
  114 + assert_tag :tag => 'p', :content => /Current category:/, :sibling => { :tag => 'a', :content => /#{category1.name}/ }
  115 + end
  116 +
  117 + should 'show subcategories list' do
  118 + environment = Environment.default
  119 + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment)
  120 + category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1)
  121 + get 'new', :profile => @enterprise.identifier
  122 + assert !assigns(:categories).empty?
  123 + assert_tag :tag => 'p', :content => /Select a subcategory:/, :sibling => { :tag => 'a', :attributes => { :href => '#' }, :content => /#{category2.name}/ }
  124 + end
  125 +
  126 + should 'update subcategories' do
  127 + environment = Environment.default
  128 + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment)
  129 + category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1)
  130 + get 'update_subcategories', :profile => @enterprise.identifier, :id => category1.id
  131 + assert_tag :tag => 'a', :attributes => { :href => '#' }, :content => /#{category2.name}/
  132 + end
  133 +
  134 + should 'not show subcategories list when no subcategories' do
  135 + environment = Environment.default
  136 + category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment)
  137 + get 'update_subcategories', :profile => @enterprise.identifier, :id => category1.id
  138 + assert_no_tag :tag => 'p', :content => 'Select a subcategory:'
  139 + end
106 140  
107 141 end
... ...