Commit 9b0501137307deaf73efe7f3403f91c1e70436c9

Authored by AntonioTerceiro
1 parent 06b4d17a

ActionItem102: fixing bug in category + doc work in category



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@656 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/category.rb
@@ -16,21 +16,47 @@ class Category < ActiveRecord::Base @@ -16,21 +16,47 @@ class Category < ActiveRecord::Base
16 16
17 acts_as_tree :order => 'name' 17 acts_as_tree :order => 'name'
18 18
19 -  
20 - 19 + # calculates the full name of a category by accessing the name of all its
  20 + # ancestors.
  21 + #
  22 + # If you have this category hierarchy:
  23 + # Category "A"
  24 + # Category "B"
  25 + # Category "C"
  26 + #
  27 + # Then Category "C" will have "A/B/C" as its full name.
21 def full_name(sep = '/') 28 def full_name(sep = '/')
22 - self.parent ? (self.parent.full_name(sep) + sep + self.name) : (self.name) 29 + my_name = self.name ? self.name : '?'
  30 + self.parent ? (self.parent.full_name(sep) + sep + my_name) : (my_name)
23 end 31 end
  32 +
  33 + # calculates the level of the category in the category hierarchy. Top-level
  34 + # categories have level 0; the children of the top-level categories have
  35 + # level 1; the children of categories with level 1 have level 2, and so on.
  36 + #
  37 + # A level 0
  38 + # / \
  39 + # B C level 1
  40 + # / \ / \
  41 + # E F G H level 2
  42 + # ...
24 def level 43 def level
25 self.parent ? (self.parent.level + 1) : 0 44 self.parent ? (self.parent.level + 1) : 0
26 end 45 end
  46 +
  47 + # Is this category a top-level category?
27 def top_level? 48 def top_level?
28 self.parent.nil? 49 self.parent.nil?
29 end 50 end
  51 +
  52 + # Is this category a leaf in the hierarchy tree of categories?
  53 + #
  54 + # Being a leaf means that this category has no subcategories.
30 def leaf? 55 def leaf?
31 self.children.empty? 56 self.children.empty?
32 end 57 end
33 58
  59 + # Finds all top level categories for a given environment.
34 def self.top_level_for(environment) 60 def self.top_level_for(environment)
35 self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ]) 61 self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ])
36 end 62 end
@@ -38,6 +64,7 @@ class Category < ActiveRecord::Base @@ -38,6 +64,7 @@ class Category < ActiveRecord::Base
38 # used to know when to trigger batch renaming 64 # used to know when to trigger batch renaming
39 attr_accessor :recalculate_path 65 attr_accessor :recalculate_path
40 66
  67 + # sets the name of the category. Also sets #slug accordingly.
41 def name=(value) 68 def name=(value)
42 if self.name != value 69 if self.name != value
43 self.recalculate_path = true 70 self.recalculate_path = true
@@ -49,6 +76,7 @@ class Category < ActiveRecord::Base @@ -49,6 +76,7 @@ class Category < ActiveRecord::Base
49 end 76 end
50 end 77 end
51 78
  79 + # sets the slug of the category. Also sets the path with the new slug value.
52 def slug=(value) 80 def slug=(value)
53 self[:slug] = value 81 self[:slug] = value
54 unless self.slug.blank? 82 unless self.slug.blank?
test/unit/category_test.rb
@@ -49,6 +49,13 @@ class CategoryTest < Test::Unit::TestCase @@ -49,6 +49,13 @@ class CategoryTest < Test::Unit::TestCase
49 assert_equal 'category_name/subcategory_name', sub_cat.full_name 49 assert_equal 'category_name/subcategory_name', sub_cat.full_name
50 end 50 end
51 51
  52 + should 'cope with nil name when calculating full_name' do
  53 + cat = Category.new(:name => 'toplevel')
  54 + sub = Category.new
  55 + sub.parent = cat
  56 + assert_equal 'toplevel/?', sub.full_name
  57 + end
  58 +
52 def test_category_level 59 def test_category_level
53 cat = Category.new(:name => 'category_name') 60 cat = Category.new(:name => 'category_name')
54 assert_equal 0, cat.level 61 assert_equal 0, cat.level