Commit 4d52e142e4b7790e3f2f8a74ed3c468a8ddfff6d

Authored by AntonioTerceiro
1 parent a07338d6

ActionItem132: asking for name without leading parents



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1160 3f533792-8f58-4932-b0fe-aaf55b0a4547
lib/acts_as_filesystem.rb
... ... @@ -61,6 +61,15 @@ module ActsAsFileSystem
61 61 self.hierarchy.map {|item| item.name || '?' }.join(sep)
62 62 end
63 63  
  64 + # gets the name without leading parents. Usefull when dividing categories
  65 + # in top-level groups and full names must not include the top-level
  66 + # category which is already a emphasized label
  67 + def full_name_without_leading(count, sep = '/')
  68 + parts = self.full_name(sep).split(sep)
  69 + count.times { parts.shift }
  70 + parts.join(sep)
  71 + end
  72 +
64 73 # calculates the level of the category in the category hierarchy. Top-level
65 74 # categories have level 0; the children of the top-level categories have
66 75 # level 1; the children of categories with level 1 have level 2, and so on.
... ...
test/unit/acts_as_filesystem_test.rb
... ... @@ -54,4 +54,12 @@ class ActsAsFilesystemTest < Test::Unit::TestCase
54 54 assert_equal 'test.txt', Article.new(:name => 'test.txt').slug
55 55 end
56 56  
  57 + should 'provide name without leading parents' do
  58 + a = Article.new
  59 + a.expects(:full_name).with('/').returns('a/b/c/d').times(3)
  60 + assert_equal 'b/c/d', a.full_name_without_leading(1)
  61 + assert_equal 'c/d', a.full_name_without_leading(2)
  62 + assert_equal 'd', a.full_name_without_leading(3)
  63 + end
  64 +
57 65 end
... ...