From 18bbbd048d5806859fef5fb67c6f559f562578da Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Fri, 20 May 2011 21:02:59 -0300 Subject: [PATCH] New subcategory uses parent class --- app/controllers/admin/categories_controller.rb | 2 +- app/controllers/my_profile/manage_products_controller.rb | 4 ++-- db/migrate/20110520150544_remove_categories_with_invalid_type.rb | 18 ++++++++++++++++++ test/functional/categories_controller_test.rb | 7 +++++++ test/functional/manage_products_controller_test.rb | 8 ++++++++ test/unit/product_test.rb | 7 +++++++ 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20110520150544_remove_categories_with_invalid_type.rb diff --git a/app/controllers/admin/categories_controller.rb b/app/controllers/admin/categories_controller.rb index 5964843..4c66793 100644 --- a/app/controllers/admin/categories_controller.rb +++ b/app/controllers/admin/categories_controller.rb @@ -14,7 +14,7 @@ class CategoriesController < AdminController # posts back def new - type = (params[:type] || 'Category') + type = (params[:type] || params[:parent_type] || 'Category') raise 'Type not allowed' unless ALLOWED_TYPES.include?(type) @category = type.constantize.new(params[:category]) diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb index 3735409..cc2d988 100644 --- a/app/controllers/my_profile/manage_products_controller.rb +++ b/app/controllers/my_profile/manage_products_controller.rb @@ -40,8 +40,8 @@ class ManageProductsController < ApplicationController end def new - @product = @profile.products.build(:product_category_id => params[:selected_category_id]) - @category = @product.product_category + @category = params[:selected_category_id] ? Category.find(params[:selected_category_id]) : nil + @product = @profile.products.build(:product_category => @category) @categories = ProductCategory.top_level_for(environment) @level = 0 if request.post? diff --git a/db/migrate/20110520150544_remove_categories_with_invalid_type.rb b/db/migrate/20110520150544_remove_categories_with_invalid_type.rb new file mode 100644 index 0000000..53f44d7 --- /dev/null +++ b/db/migrate/20110520150544_remove_categories_with_invalid_type.rb @@ -0,0 +1,18 @@ +class RemoveCategoriesWithInvalidType < ActiveRecord::Migration + + def self.remove_invalid(category) + if category.class != ProductCategory && !category.class.ancestors.include?(ProductCategory) + category.destroy + else + category.children.map { |child| remove_invalid(child) } + end + end + + def self.up + ProductCategory.all.map { |category| remove_invalid(category)} + end + + def self.down + say "this migration can't be reverted" + end +end diff --git a/test/functional/categories_controller_test.rb b/test/functional/categories_controller_test.rb index 84f4ff9..e11112e 100644 --- a/test/functional/categories_controller_test.rb +++ b/test/functional/categories_controller_test.rb @@ -167,4 +167,11 @@ class CategoriesControllerTest < Test::Unit::TestCase assert_equal [r], assigns(:regions) end + should 'use parent\'s type to determine subcategory\'s type' do + parent = ProductCategory.create!(:name => 'Sample category', :environment => Environment.default) + post :new, :parent_id => parent.id, :parent_type => parent.class.name, :category => {:name => 'Subcategory'} + sub = Category.last + assert_equal parent.class, sub.class + end + end diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb index e2a0550..a5b2847 100644 --- a/test/functional/manage_products_controller_test.rb +++ b/test/functional/manage_products_controller_test.rb @@ -170,6 +170,14 @@ class ManageProductsControllerTest < Test::Unit::TestCase end end + should 'not create a new product with an invalid category' do + category1 = fast_create(Category, :name => 'Category 1') + category2 = fast_create(Category, :name => 'Category 2', :parent_id => category1) + assert_raise ActiveRecord::AssociationTypeMismatch do + post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product' }, :selected_category_id => category2.id + end + end + should 'filter html from name of product' do category = fast_create(ProductCategory, :name => 'Category 1') post 'new', :profile => @enterprise.identifier, :product => { :name => "name bold" }, :selected_category_id => category.id diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index 2562122..3e643c0 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -244,6 +244,13 @@ class ProductTest < Test::Unit::TestCase assert product.errors.invalid?(:product_category_id) end + should 'not save with a invalid category' do + category = Category.new(:name => 'Region', :environment => Environment.default) + assert_raise ActiveRecord::AssociationTypeMismatch do + Product.new(:name => 'Invalid category product', :product_category => category) + end + end + should 'format values to float with 2 decimals' do ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1') product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994) -- libgit2 0.21.2