Commit 18bbbd048d5806859fef5fb67c6f559f562578da
1 parent
2c1b5026
Exists in
master
and in
28 other branches
New subcategory uses parent class
* Also including a migration to remove subcategories of
ProductCategory that aren't a subclass of ProductCategory.
(ActionItem2007)
Showing
6 changed files
with
43 additions
and
3 deletions
Show diff stats
app/controllers/admin/categories_controller.rb
| ... | ... | @@ -14,7 +14,7 @@ class CategoriesController < AdminController |
| 14 | 14 | |
| 15 | 15 | # posts back |
| 16 | 16 | def new |
| 17 | - type = (params[:type] || 'Category') | |
| 17 | + type = (params[:type] || params[:parent_type] || 'Category') | |
| 18 | 18 | raise 'Type not allowed' unless ALLOWED_TYPES.include?(type) |
| 19 | 19 | |
| 20 | 20 | @category = type.constantize.new(params[:category]) | ... | ... |
app/controllers/my_profile/manage_products_controller.rb
| ... | ... | @@ -40,8 +40,8 @@ class ManageProductsController < ApplicationController |
| 40 | 40 | end |
| 41 | 41 | |
| 42 | 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 | 45 | @categories = ProductCategory.top_level_for(environment) |
| 46 | 46 | @level = 0 |
| 47 | 47 | if request.post? | ... | ... |
db/migrate/20110520150544_remove_categories_with_invalid_type.rb
0 → 100644
| ... | ... | @@ -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 | 167 | assert_equal [r], assigns(:regions) |
| 168 | 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 | 177 | end | ... | ... |
test/functional/manage_products_controller_test.rb
| ... | ... | @@ -170,6 +170,14 @@ class ManageProductsControllerTest < Test::Unit::TestCase |
| 170 | 170 | end |
| 171 | 171 | end |
| 172 | 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 | + | |
| 173 | 181 | should 'filter html from name of product' do |
| 174 | 182 | category = fast_create(ProductCategory, :name => 'Category 1') |
| 175 | 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 | 244 | assert product.errors.invalid?(:product_category_id) |
| 245 | 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 | 254 | should 'format values to float with 2 decimals' do |
| 248 | 255 | ent = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1') |
| 249 | 256 | product = fast_create(Product, :enterprise_id => ent.id, :price => 12.994, :discount => 1.994) | ... | ... |