Commit 7c0c84cfce4ea22624c342464c046e1a90161ca4
Exists in
master
and in
29 other branches
Merge commit 'refs/merge-requests/4' of git://gitorious.org/noosfero/noosfero into merge-requests/4
Showing
6 changed files
with
51 additions
and
4 deletions
Show diff stats
app/controllers/admin/categories_controller.rb
@@ -14,7 +14,7 @@ class CategoriesController < AdminController | @@ -14,7 +14,7 @@ class CategoriesController < AdminController | ||
14 | 14 | ||
15 | # posts back | 15 | # posts back |
16 | def new | 16 | def new |
17 | - type = (params[:type] || 'Category') | 17 | + type = (params[:type] || params[:parent_type] || 'Category') |
18 | raise 'Type not allowed' unless ALLOWED_TYPES.include?(type) | 18 | raise 'Type not allowed' unless ALLOWED_TYPES.include?(type) |
19 | 19 | ||
20 | @category = type.constantize.new(params[:category]) | 20 | @category = type.constantize.new(params[:category]) |
app/controllers/my_profile/manage_products_controller.rb
@@ -40,8 +40,8 @@ class ManageProductsController < ApplicationController | @@ -40,8 +40,8 @@ class ManageProductsController < ApplicationController | ||
40 | end | 40 | end |
41 | 41 | ||
42 | def new | 42 | def new |
43 | - @product = @profile.products.build(:product_category_id => params[:selected_category_id]) | ||
44 | - @category = @product.product_category | 43 | + @category = params[:selected_category_id] ? Category.find(params[:selected_category_id]) : nil |
44 | + @product = @profile.products.build(:product_category => @category) | ||
45 | @categories = ProductCategory.top_level_for(environment) | 45 | @categories = ProductCategory.top_level_for(environment) |
46 | @level = 0 | 46 | @level = 0 |
47 | if request.post? | 47 | if request.post? |
@@ -72,7 +72,7 @@ class ManageProductsController < ApplicationController | @@ -72,7 +72,7 @@ class ManageProductsController < ApplicationController | ||
72 | 72 | ||
73 | def edit_category | 73 | def edit_category |
74 | @product = @profile.products.find(params[:id]) | 74 | @product = @profile.products.find(params[:id]) |
75 | - @category = @product.product_category | 75 | + @category = @product.product_category || ProductCategory.first |
76 | @categories = ProductCategory.top_level_for(environment) | 76 | @categories = ProductCategory.top_level_for(environment) |
77 | @edit = true | 77 | @edit = true |
78 | @level = @category.level | 78 | @level = @category.level |
db/migrate/20110520150544_remove_categories_with_invalid_type.rb
0 → 100644
@@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
1 | +class RemoveCategoriesWithInvalidType < ActiveRecord::Migration | ||
2 | + | ||
3 | + def self.remove_invalid(category) | ||
4 | + if category.class != ProductCategory && !category.class.ancestors.include?(ProductCategory) | ||
5 | + category.destroy | ||
6 | + else | ||
7 | + category.children.map { |child| remove_invalid(child) } | ||
8 | + end | ||
9 | + end | ||
10 | + | ||
11 | + def self.up | ||
12 | + ProductCategory.all.map { |category| remove_invalid(category)} | ||
13 | + end | ||
14 | + | ||
15 | + def self.down | ||
16 | + say "this migration can't be reverted" | ||
17 | + end | ||
18 | +end |
test/functional/categories_controller_test.rb
@@ -167,4 +167,11 @@ class CategoriesControllerTest < Test::Unit::TestCase | @@ -167,4 +167,11 @@ class CategoriesControllerTest < Test::Unit::TestCase | ||
167 | assert_equal [r], assigns(:regions) | 167 | assert_equal [r], assigns(:regions) |
168 | end | 168 | end |
169 | 169 | ||
170 | + should 'use parent\'s type to determine subcategory\'s type' do | ||
171 | + parent = ProductCategory.create!(:name => 'Sample category', :environment => Environment.default) | ||
172 | + post :new, :parent_id => parent.id, :parent_type => parent.class.name, :category => {:name => 'Subcategory'} | ||
173 | + sub = Category.last | ||
174 | + assert_equal parent.class, sub.class | ||
175 | + end | ||
176 | + | ||
170 | end | 177 | end |
test/functional/manage_products_controller_test.rb
@@ -123,6 +123,13 @@ class ManageProductsControllerTest < Test::Unit::TestCase | @@ -123,6 +123,13 @@ class ManageProductsControllerTest < Test::Unit::TestCase | ||
123 | assert_template 'shared/_dialog_error_messages' | 123 | assert_template 'shared/_dialog_error_messages' |
124 | end | 124 | end |
125 | 125 | ||
126 | + should "not crash if product has no category" do | ||
127 | + product = fast_create(Product, :enterprise_id => @enterprise.id) | ||
128 | + assert_nothing_raised do | ||
129 | + post 'edit_category', :profile => @enterprise.identifier, :id => product.id | ||
130 | + end | ||
131 | + end | ||
132 | + | ||
126 | should "destroy product" do | 133 | should "destroy product" do |
127 | product = fast_create(Product, :name => 'test product', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) | 134 | product = fast_create(Product, :name => 'test product', :enterprise_id => @enterprise.id, :product_category_id => @product_category.id) |
128 | assert_difference Product, :count, -1 do | 135 | assert_difference Product, :count, -1 do |
@@ -163,6 +170,14 @@ class ManageProductsControllerTest < Test::Unit::TestCase | @@ -163,6 +170,14 @@ class ManageProductsControllerTest < Test::Unit::TestCase | ||
163 | end | 170 | end |
164 | end | 171 | end |
165 | 172 | ||
173 | + should 'not create a new product with an invalid category' do | ||
174 | + category1 = fast_create(Category, :name => 'Category 1') | ||
175 | + category2 = fast_create(Category, :name => 'Category 2', :parent_id => category1) | ||
176 | + assert_raise ActiveRecord::AssociationTypeMismatch do | ||
177 | + post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product' }, :selected_category_id => category2.id | ||
178 | + end | ||
179 | + end | ||
180 | + | ||
166 | should 'filter html from name of product' do | 181 | should 'filter html from name of product' do |
167 | category = fast_create(ProductCategory, :name => 'Category 1') | 182 | category = fast_create(ProductCategory, :name => 'Category 1') |
168 | post 'new', :profile => @enterprise.identifier, :product => { :name => "<b id='html_name'>name bold</b>" }, :selected_category_id => category.id | 183 | post 'new', :profile => @enterprise.identifier, :product => { :name => "<b id='html_name'>name bold</b>" }, :selected_category_id => category.id |
test/unit/product_test.rb
@@ -244,6 +244,13 @@ class ProductTest < Test::Unit::TestCase | @@ -244,6 +244,13 @@ class ProductTest < Test::Unit::TestCase | ||
244 | assert product.errors.invalid?(:product_category_id) | 244 | assert product.errors.invalid?(:product_category_id) |
245 | end | 245 | end |
246 | 246 | ||
247 | + should 'not save with a invalid category' do | ||
248 | + category = Category.new(:name => 'Region', :environment => Environment.default) | ||
249 | + assert_raise ActiveRecord::AssociationTypeMismatch do | ||
250 | + Product.new(:name => 'Invalid category product', :product_category => category) | ||
251 | + end | ||
252 | + end | ||
253 | + | ||
247 | should 'format values to float with 2 decimals' do | 254 | should 'format values to float with 2 decimals' do |
248 | ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1') | 255 | ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1') |
249 | product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994) | 256 | product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994) |