diff --git a/app/controllers/environment_admin/categories_controller.rb b/app/controllers/environment_admin/categories_controller.rb index 3881e1d..2debba4 100644 --- a/app/controllers/environment_admin/categories_controller.rb +++ b/app/controllers/environment_admin/categories_controller.rb @@ -6,9 +6,14 @@ class CategoriesController < EnvironmentAdminController @categories = environment.top_level_categories end + ALLOWED_TYPES = CategoriesHelper::TYPES.map {|item| item[1] } + # posts back def new - @category = Category.new(params[:category]) + type = (params[:type] || 'Category') + raise 'Type not allowed' unless ALLOWED_TYPES.include?(type) + + @category = type.constantize.new(params[:category]) @category.environment = environment if params[:parent_id] @category.parent = environment.categories.find(params[:parent_id]) diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb index 9dea17d..53003fe 100644 --- a/app/helpers/categories_helper.rb +++ b/app/helpers/categories_helper.rb @@ -10,6 +10,11 @@ module CategoriesHelper [ N_('Orange'), 4 ], ] + TYPES = [ + [ _('Categoria Geral'), Category.to_s ], + [ _('Product Category'), ProductCategory.to_s ], + ] + def select_color_for_category if @category.top_level? labelled_form_field(_('Display at the menu?'), select('category', 'display_color', CategoriesHelper::COLORS.map {|item| [gettext(item[0]), item[1]] })) @@ -27,4 +32,9 @@ module CategoriesHelper end end + def select_category_type(field) + value = params[field] + labelled_form_field(_('Type of category'), select_tag('type', options_for_select(CategoriesHelper::TYPES, value))) + end + end diff --git a/app/models/category.rb b/app/models/category.rb index 3e6995e..b4c5bbf 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -8,8 +8,16 @@ class Category < ActiveRecord::Base validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ] validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.') + def validate + if self.parent && (self.class != self.parent.class) + self.errors.add(:type, _("%{fn} must be the same as the parents'")) + end + end + acts_as_tree :order => 'name' + + def full_name(sep = '/') self.parent ? (self.parent.full_name(sep) + sep + self.name) : (self.name) end diff --git a/app/models/product_category.rb b/app/models/product_category.rb new file mode 100644 index 0000000..077ebe8 --- /dev/null +++ b/app/models/product_category.rb @@ -0,0 +1,2 @@ +class ProductCategory < Category +end diff --git a/app/views/categories/_form.rhtml b/app/views/categories/_form.rhtml index 3a99803..6346c75 100644 --- a/app/views/categories/_form.rhtml +++ b/app/views/categories/_form.rhtml @@ -1,8 +1,11 @@ <%= error_messages_for 'category' %> <% labelled_form_for 'category' do |f| %> - <% if @category.new_record? && @category.parent %> - <%= hidden_field_tag('parent_id', @category.parent.id) %> + <% if @category.new_record? %> + <% if @category.parent %> + <%= hidden_field_tag('parent_id', @category.parent.id) %> + <% end %> + <%= select_category_type :type %> <% end %> <%= select_color_for_category %> diff --git a/test/functional/categories_controller_test.rb b/test/functional/categories_controller_test.rb index e8bf29f..80bcdbb 100644 --- a/test/functional/categories_controller_test.rb +++ b/test/functional/categories_controller_test.rb @@ -39,12 +39,18 @@ class CategoriesControllerTest < Test::Unit::TestCase assert_equal 'new name for category', Category.find(cat1.id).name end - def test_new + def test_new_category cat = Category.new Category.expects(:new).returns(cat) get :new end + def test_new_product_category + cat = ProductCategory.new + ProductCategory.expects(:new).returns(cat) + get :new, :type => 'ProductCategory' + end + def test_new_save assert_difference Category, :count do post :new, :category => { :name => 'a new category' } diff --git a/test/unit/product_category_test.rb b/test/unit/product_category_test.rb new file mode 100644 index 0000000..817369d --- /dev/null +++ b/test/unit/product_category_test.rb @@ -0,0 +1,20 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProductCategoryTest < Test::Unit::TestCase + # TODO: please write tests here when ProductCategory has something + def test_require_same_class_for_children + c1 = ProductCategory.new(:name => 'Some Product Type', :environment_id => 1) + c1.save! + + c2 = Category.new(:name => 'wrong', :environment_id => 1) + c1.children << c2 + + assert !c2.valid? + assert c2.errors.invalid?(:type) + + c3 = ProductCategory.new(:name => 'right', :environment_id => 1) + c1.children << c3 + assert c3.valid? + assert !c3.errors.invalid?(:type) + end +end -- libgit2 0.21.2