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 16  
17 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 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 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 43 def level
25 44 self.parent ? (self.parent.level + 1) : 0
26 45 end
  46 +
  47 + # Is this category a top-level category?
27 48 def top_level?
28 49 self.parent.nil?
29 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 55 def leaf?
31 56 self.children.empty?
32 57 end
33 58  
  59 + # Finds all top level categories for a given environment.
34 60 def self.top_level_for(environment)
35 61 self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ])
36 62 end
... ... @@ -38,6 +64,7 @@ class Category < ActiveRecord::Base
38 64 # used to know when to trigger batch renaming
39 65 attr_accessor :recalculate_path
40 66  
  67 + # sets the name of the category. Also sets #slug accordingly.
41 68 def name=(value)
42 69 if self.name != value
43 70 self.recalculate_path = true
... ... @@ -49,6 +76,7 @@ class Category < ActiveRecord::Base
49 76 end
50 77 end
51 78  
  79 + # sets the slug of the category. Also sets the path with the new slug value.
52 80 def slug=(value)
53 81 self[:slug] = value
54 82 unless self.slug.blank?
... ...
test/unit/category_test.rb
... ... @@ -49,6 +49,13 @@ class CategoryTest < Test::Unit::TestCase
49 49 assert_equal 'category_name/subcategory_name', sub_cat.full_name
50 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 59 def test_category_level
53 60 cat = Category.new(:name => 'category_name')
54 61 assert_equal 0, cat.level
... ...