diff --git a/app/models/category.rb b/app/models/category.rb index 4f860ce..c6691c2 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,6 +1,7 @@ class Category < ActiveRecord::Base validates_presence_of :name, :environment_id + validates_uniqueness_of :slug,:scope => [ :environment_id, :parent_id ], :message => N_('%{fn} is already being used by another category.') belongs_to :environment acts_as_tree :order => 'name' diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 1cb9503..e2a8c02 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -118,7 +118,7 @@ class CategoryTest < Test::Unit::TestCase assert_equal 'parent/child', c2.path end - def test_save_ + def test_should_set_path_correctly_before_saving c1 = Category.create!(:name => 'parent', :environment_id => @env.id) c2 = Category.new(:name => 'child', :environment_id => @env.id) @@ -128,4 +128,13 @@ class CategoryTest < Test::Unit::TestCase assert_equal 'parent/child', c2.path end + def test_should_refuse_to_duplicate_slug_under_the_same_parent + c1 = Category.create!(:name => 'test category', :environment_id => @env.id) + c2 = Category.new(:name => 'Test: Category', :environment_id => @env.id) + + assert !c2.valid? + assert c2.errors.invalid?(:slug) + + end + end -- libgit2 0.21.2