From 0ce0492c12a5944772e6fd2a54003520a0863dea Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Tue, 25 Sep 2007 18:29:15 +0000 Subject: [PATCH] ActionItem70: actually assigning categories to the menu --- app/models/category.rb | 3 +++ app/views/categories/_form.rhtml | 13 +++++++++++++ app/views/shared/categories_menu.rhtml | 66 ++++++++++-------------------------------------------------------- db/migrate/016_create_categories.rb | 2 ++ test/unit/category_test.rb | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 56 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index e537d58..01bd36b 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -5,6 +5,9 @@ class Category < ActiveRecord::Base validates_uniqueness_of :slug,:scope => [ :environment_id, :parent_id ], :message => N_('%{fn} is already being used by another category.') belongs_to :environment + 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.') + acts_as_tree :order => 'name' def full_name(sep = '/') diff --git a/app/views/categories/_form.rhtml b/app/views/categories/_form.rhtml index c99f7de..e8629e0 100644 --- a/app/views/categories/_form.rhtml +++ b/app/views/categories/_form.rhtml @@ -5,6 +5,19 @@ <%= hidden_field_tag('parent_id', @category.parent.id) %> <% end %> + <% if @category.top_level? %> + <% + options = [ + [ _('Do not display at the menu'), nil ], + [ _('Blue'), 1 ], + [ _('Red'), 2 ], + [ _('Green'), 3 ], + [ _('Orange'), 4 ], + ] + %> + <%= labelled_form_field(_('Display at the menu?'), f.select('display_color', options)) %> + <% end %> + <%= f.text_field 'name' %> <%= submit_tag _('Save') %> <% end %> diff --git a/app/views/shared/categories_menu.rhtml b/app/views/shared/categories_menu.rhtml index b663fa2..d0f40ae 100644 --- a/app/views/shared/categories_menu.rhtml +++ b/app/views/shared/categories_menu.rhtml @@ -1,58 +1,12 @@ diff --git a/db/migrate/016_create_categories.rb b/db/migrate/016_create_categories.rb index f3a3eec..9dc8b37 100644 --- a/db/migrate/016_create_categories.rb +++ b/db/migrate/016_create_categories.rb @@ -5,6 +5,8 @@ class CreateCategories < ActiveRecord::Migration t.column :slug, :string t.column :path, :text, :default => '' + t.column :display_color, :integer + t.column :environment_id, :integer t.column :parent_id, :integer t.column :type, :string diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 5502bc9..a2bd987 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -151,4 +151,38 @@ class CategoryTest < Test::Unit::TestCase end + should "limit the possibile display colors" do + c = Category.new(:name => 'test category', :environment_id => @env.id) + + + c.display_color = 10 + c.valid? + assert c.errors.invalid?(:display_color) + + valid = %w[ 1 2 3 4 ].map { |item| item.to_i } + valid.each do |item| + c.display_color = item + c.valid? + assert !c.errors.invalid?(:display_color) + end + + end + + should 'avoid duplicated display colors' do + + @env.categories.destroy_all + + c1 = Category.create!(:name => 'test category', :environment_id => @env.id, :display_color => 1) + + c = Category.new(:name => 'lalala', :environment_id => @env.id) + c.display_color = 1 + assert !c.valid? + assert c.errors.invalid?(:display_color) + + c.display_color = 2 + c.valid? + assert !c.errors.invalid?(:display_color) + + end + end -- libgit2 0.21.2