Commit 652d181cc5ad8664b26503068937613b3f9a5a33
1 parent
86a8b05b
Exists in
master
and in
29 other branches
ActionItem70: referring to top ancestor of a category
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@543 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
14 additions
and
0 deletions
Show diff stats
app/models/category.rb
@@ -77,4 +77,8 @@ class Category < ActiveRecord::Base | @@ -77,4 +77,8 @@ class Category < ActiveRecord::Base | ||
77 | cat.recalculate_path = false | 77 | cat.recalculate_path = false |
78 | end | 78 | end |
79 | 79 | ||
80 | + def top_ancestor | ||
81 | + self.top_level? ? self : self.parent.top_ancestor | ||
82 | + end | ||
83 | + | ||
80 | end | 84 | end |
test/unit/category_test.rb
@@ -185,4 +185,14 @@ class CategoryTest < Test::Unit::TestCase | @@ -185,4 +185,14 @@ class CategoryTest < Test::Unit::TestCase | ||
185 | 185 | ||
186 | end | 186 | end |
187 | 187 | ||
188 | + should 'be able to get top ancestor' do | ||
189 | + c1 = Category.create!(:name => 'test category', :environment_id => @env.id) | ||
190 | + c2 = Category.create!(:name => 'test category', :environment_id => @env.id, :parent_id => c1.id) | ||
191 | + c3 = Category.create!(:name => 'test category', :environment_id => @env.id, :parent_id => c2.id) | ||
192 | + | ||
193 | + assert_equal c1, c1.top_ancestor | ||
194 | + assert_equal c1, c2.top_ancestor | ||
195 | + assert_equal c1, c3.top_ancestor | ||
196 | + end | ||
197 | + | ||
188 | end | 198 | end |