Commit f185c568fad2fe11e2d47e86d593a4f94c7ebe09
1 parent
7787940b
Exists in
master
and in
29 other branches
ActionItem70: don't allow duplicated slugs
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@522 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
11 additions
and
1 deletions
Show diff stats
app/models/category.rb
1 | class Category < ActiveRecord::Base | 1 | class Category < ActiveRecord::Base |
2 | 2 | ||
3 | validates_presence_of :name, :environment_id | 3 | validates_presence_of :name, :environment_id |
4 | + validates_uniqueness_of :slug,:scope => [ :environment_id, :parent_id ], :message => N_('%{fn} is already being used by another category.') | ||
4 | belongs_to :environment | 5 | belongs_to :environment |
5 | 6 | ||
6 | acts_as_tree :order => 'name' | 7 | acts_as_tree :order => 'name' |
test/unit/category_test.rb
@@ -118,7 +118,7 @@ class CategoryTest < Test::Unit::TestCase | @@ -118,7 +118,7 @@ class CategoryTest < Test::Unit::TestCase | ||
118 | assert_equal 'parent/child', c2.path | 118 | assert_equal 'parent/child', c2.path |
119 | end | 119 | end |
120 | 120 | ||
121 | - def test_save_ | 121 | + def test_should_set_path_correctly_before_saving |
122 | c1 = Category.create!(:name => 'parent', :environment_id => @env.id) | 122 | c1 = Category.create!(:name => 'parent', :environment_id => @env.id) |
123 | 123 | ||
124 | c2 = Category.new(:name => 'child', :environment_id => @env.id) | 124 | c2 = Category.new(:name => 'child', :environment_id => @env.id) |
@@ -128,4 +128,13 @@ class CategoryTest < Test::Unit::TestCase | @@ -128,4 +128,13 @@ class CategoryTest < Test::Unit::TestCase | ||
128 | assert_equal 'parent/child', c2.path | 128 | assert_equal 'parent/child', c2.path |
129 | end | 129 | end |
130 | 130 | ||
131 | + def test_should_refuse_to_duplicate_slug_under_the_same_parent | ||
132 | + c1 = Category.create!(:name => 'test category', :environment_id => @env.id) | ||
133 | + c2 = Category.new(:name => 'Test: Category', :environment_id => @env.id) | ||
134 | + | ||
135 | + assert !c2.valid? | ||
136 | + assert c2.errors.invalid?(:slug) | ||
137 | + | ||
138 | + end | ||
139 | + | ||
131 | end | 140 | end |