diff --git a/lib/acts_as_filesystem.rb b/lib/acts_as_filesystem.rb index b320082..45e577c 100644 --- a/lib/acts_as_filesystem.rb +++ b/lib/acts_as_filesystem.rb @@ -61,6 +61,15 @@ module ActsAsFileSystem self.hierarchy.map {|item| item.name || '?' }.join(sep) end + # gets the name without leading parents. Usefull when dividing categories + # in top-level groups and full names must not include the top-level + # category which is already a emphasized label + def full_name_without_leading(count, sep = '/') + parts = self.full_name(sep).split(sep) + count.times { parts.shift } + parts.join(sep) + end + # calculates the level of the category in the category hierarchy. Top-level # categories have level 0; the children of the top-level categories have # level 1; the children of categories with level 1 have level 2, and so on. diff --git a/test/unit/acts_as_filesystem_test.rb b/test/unit/acts_as_filesystem_test.rb index f814528..aa51829 100644 --- a/test/unit/acts_as_filesystem_test.rb +++ b/test/unit/acts_as_filesystem_test.rb @@ -54,4 +54,12 @@ class ActsAsFilesystemTest < Test::Unit::TestCase assert_equal 'test.txt', Article.new(:name => 'test.txt').slug end + should 'provide name without leading parents' do + a = Article.new + a.expects(:full_name).with('/').returns('a/b/c/d').times(3) + assert_equal 'b/c/d', a.full_name_without_leading(1) + assert_equal 'c/d', a.full_name_without_leading(2) + assert_equal 'd', a.full_name_without_leading(3) + end + end -- libgit2 0.21.2