From 652d181cc5ad8664b26503068937613b3f9a5a33 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Tue, 25 Sep 2007 19:21:24 +0000 Subject: [PATCH] ActionItem70: referring to top ancestor of a category --- app/models/category.rb | 4 ++++ test/unit/category_test.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 01bd36b..939e888 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -77,4 +77,8 @@ class Category < ActiveRecord::Base cat.recalculate_path = false end + def top_ancestor + self.top_level? ? self : self.parent.top_ancestor + end + end diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index a2bd987..d4216c1 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -185,4 +185,14 @@ class CategoryTest < Test::Unit::TestCase end + should 'be able to get top ancestor' do + c1 = Category.create!(:name => 'test category', :environment_id => @env.id) + c2 = Category.create!(:name => 'test category', :environment_id => @env.id, :parent_id => c1.id) + c3 = Category.create!(:name => 'test category', :environment_id => @env.id, :parent_id => c2.id) + + assert_equal c1, c1.top_ancestor + assert_equal c1, c2.top_ancestor + assert_equal c1, c3.top_ancestor + end + end -- libgit2 0.21.2