Commit 1abaaf1a477fb080e8748f3e1c137bbde060fcd7
1 parent
60c3a8e0
Exists in
master
and in
28 other branches
ActionItem33: implementing ProductCategory class and making possible for the adm…
…inistrator to create different types of categories git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@609 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
7 changed files
with
58 additions
and
4 deletions
Show diff stats
app/controllers/environment_admin/categories_controller.rb
@@ -6,9 +6,14 @@ class CategoriesController < EnvironmentAdminController | @@ -6,9 +6,14 @@ class CategoriesController < EnvironmentAdminController | ||
6 | @categories = environment.top_level_categories | 6 | @categories = environment.top_level_categories |
7 | end | 7 | end |
8 | 8 | ||
9 | + ALLOWED_TYPES = CategoriesHelper::TYPES.map {|item| item[1] } | ||
10 | + | ||
9 | # posts back | 11 | # posts back |
10 | def new | 12 | def new |
11 | - @category = Category.new(params[:category]) | 13 | + type = (params[:type] || 'Category') |
14 | + raise 'Type not allowed' unless ALLOWED_TYPES.include?(type) | ||
15 | + | ||
16 | + @category = type.constantize.new(params[:category]) | ||
12 | @category.environment = environment | 17 | @category.environment = environment |
13 | if params[:parent_id] | 18 | if params[:parent_id] |
14 | @category.parent = environment.categories.find(params[:parent_id]) | 19 | @category.parent = environment.categories.find(params[:parent_id]) |
app/helpers/categories_helper.rb
@@ -10,6 +10,11 @@ module CategoriesHelper | @@ -10,6 +10,11 @@ module CategoriesHelper | ||
10 | [ N_('Orange'), 4 ], | 10 | [ N_('Orange'), 4 ], |
11 | ] | 11 | ] |
12 | 12 | ||
13 | + TYPES = [ | ||
14 | + [ _('Categoria Geral'), Category.to_s ], | ||
15 | + [ _('Product Category'), ProductCategory.to_s ], | ||
16 | + ] | ||
17 | + | ||
13 | def select_color_for_category | 18 | def select_color_for_category |
14 | if @category.top_level? | 19 | if @category.top_level? |
15 | labelled_form_field(_('Display at the menu?'), select('category', 'display_color', CategoriesHelper::COLORS.map {|item| [gettext(item[0]), item[1]] })) | 20 | 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 | @@ -27,4 +32,9 @@ module CategoriesHelper | ||
27 | end | 32 | end |
28 | end | 33 | end |
29 | 34 | ||
35 | + def select_category_type(field) | ||
36 | + value = params[field] | ||
37 | + labelled_form_field(_('Type of category'), select_tag('type', options_for_select(CategoriesHelper::TYPES, value))) | ||
38 | + end | ||
39 | + | ||
30 | end | 40 | end |
app/models/category.rb
@@ -8,8 +8,16 @@ class Category < ActiveRecord::Base | @@ -8,8 +8,16 @@ class Category < ActiveRecord::Base | ||
8 | validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ] | 8 | validates_inclusion_of :display_color, :in => [ 1, 2, 3, 4, nil ] |
9 | validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.') | 9 | validates_uniqueness_of :display_color, :scope => :environment_id, :if => (lambda { |cat| ! cat.display_color.nil? }), :message => N_('%{fn} was already assigned to another category.') |
10 | 10 | ||
11 | + def validate | ||
12 | + if self.parent && (self.class != self.parent.class) | ||
13 | + self.errors.add(:type, _("%{fn} must be the same as the parents'")) | ||
14 | + end | ||
15 | + end | ||
16 | + | ||
11 | acts_as_tree :order => 'name' | 17 | acts_as_tree :order => 'name' |
12 | 18 | ||
19 | + | ||
20 | + | ||
13 | def full_name(sep = '/') | 21 | def full_name(sep = '/') |
14 | self.parent ? (self.parent.full_name(sep) + sep + self.name) : (self.name) | 22 | self.parent ? (self.parent.full_name(sep) + sep + self.name) : (self.name) |
15 | end | 23 | end |
app/views/categories/_form.rhtml
1 | <%= error_messages_for 'category' %> | 1 | <%= error_messages_for 'category' %> |
2 | 2 | ||
3 | <% labelled_form_for 'category' do |f| %> | 3 | <% labelled_form_for 'category' do |f| %> |
4 | - <% if @category.new_record? && @category.parent %> | ||
5 | - <%= hidden_field_tag('parent_id', @category.parent.id) %> | 4 | + <% if @category.new_record? %> |
5 | + <% if @category.parent %> | ||
6 | + <%= hidden_field_tag('parent_id', @category.parent.id) %> | ||
7 | + <% end %> | ||
8 | + <%= select_category_type :type %> | ||
6 | <% end %> | 9 | <% end %> |
7 | 10 | ||
8 | <%= select_color_for_category %> | 11 | <%= select_color_for_category %> |
test/functional/categories_controller_test.rb
@@ -39,12 +39,18 @@ class CategoriesControllerTest < Test::Unit::TestCase | @@ -39,12 +39,18 @@ class CategoriesControllerTest < Test::Unit::TestCase | ||
39 | assert_equal 'new name for category', Category.find(cat1.id).name | 39 | assert_equal 'new name for category', Category.find(cat1.id).name |
40 | end | 40 | end |
41 | 41 | ||
42 | - def test_new | 42 | + def test_new_category |
43 | cat = Category.new | 43 | cat = Category.new |
44 | Category.expects(:new).returns(cat) | 44 | Category.expects(:new).returns(cat) |
45 | get :new | 45 | get :new |
46 | end | 46 | end |
47 | 47 | ||
48 | + def test_new_product_category | ||
49 | + cat = ProductCategory.new | ||
50 | + ProductCategory.expects(:new).returns(cat) | ||
51 | + get :new, :type => 'ProductCategory' | ||
52 | + end | ||
53 | + | ||
48 | def test_new_save | 54 | def test_new_save |
49 | assert_difference Category, :count do | 55 | assert_difference Category, :count do |
50 | post :new, :category => { :name => 'a new category' } | 56 | post :new, :category => { :name => 'a new category' } |
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class ProductCategoryTest < Test::Unit::TestCase | ||
4 | + # TODO: please write tests here when ProductCategory has something | ||
5 | + def test_require_same_class_for_children | ||
6 | + c1 = ProductCategory.new(:name => 'Some Product Type', :environment_id => 1) | ||
7 | + c1.save! | ||
8 | + | ||
9 | + c2 = Category.new(:name => 'wrong', :environment_id => 1) | ||
10 | + c1.children << c2 | ||
11 | + | ||
12 | + assert !c2.valid? | ||
13 | + assert c2.errors.invalid?(:type) | ||
14 | + | ||
15 | + c3 = ProductCategory.new(:name => 'right', :environment_id => 1) | ||
16 | + c1.children << c3 | ||
17 | + assert c3.valid? | ||
18 | + assert !c3.errors.invalid?(:type) | ||
19 | + end | ||
20 | +end |