Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Add depth alias to ancestors_count, tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes committed Sep 28, 2014
1 parent b0cf3db commit 808ee2d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 32 deletions.
43 changes: 25 additions & 18 deletions README.md
Expand Up @@ -218,34 +218,41 @@ RDoc.
(For the rest let’s assume that `root1 = Category.get(1)`, etc…)

```ruby
root1.root? # Returns true, because root is a root category
child1.root? # Returns false
root1.root? # Returns true, because root is a root category (Also aliased as .is_root?)
child1.root? # Returns false

subchild4.root # Returns root1 because root1 is the root category
root1.root # Returns root1 (itself) for the same reason
subchild4.root # Returns root1 because root1 is the root category
root1.root # Returns root1 (itself) for the same reason

child1.parent # Returns root
root1.parent # Returns nil, because root has no parent
child1.parent # Returns root
root1.parent # Returns nil, because root has no parent

child1.children # Returns an array with [subchild1, subchild2]
child1.children_ids # Returns the same array, but ids instead of categories [3, 4]
child1.children # Returns an array with [subchild1, subchild2]
child1.children_ids # Returns the same array, but ids instead of categories [3, 4]
child1.children? # Returns true as child1 has children

subchild1.ancestors # Returns an array with [child1, root1]
subchild1.ancestors_ids # Returns the same array, but ids instead of categories [2, 1]
root1.ancestors # Returns an empty array [], because root has none
subchild1.ancestors # Returns an array with [child1, root1]
subchild1.ancestors_ids # Returns the same array, but ids instead of categories [2, 1]
root1.ancestors # Returns an empty array [], because root has none

root1.descendants # Returns an array with [child1, subchild1, subchild2]
root1.descendants_ids # Returns the same array, but ids instead of categories [2, 3, 4]
subchild1.descendants # Returns an empty array [], because it has none
root1.descendants # Returns an array with [child1, subchild1, subchild2]
root1.descendants_ids # Returns the same array, but ids instead of categories [2, 3, 4]
subchild1.descendants # Returns an empty array [], because it has none

root1.siblings # Returns an array with all siblings [root2]
root1.has_siblings? # Returns true (Also .siblings? is an alias)
root1.siblings_ids # Returns an array with all siblings ids [5]
child1.siblings # Returns an empty array [], because it has no siblings
root1.siblings # Returns an array with all siblings [root2]
root1.has_siblings? # Returns true
root1.siblings_ids # Returns an array with all siblings ids [5]
child1.siblings # Returns an empty array [], because it has no siblings

subchild1.self_and_siblings # Returns an array [subchild1, subchild2], just like siblings, only with itself as well
subchild1.self_and_siblings_ids # Returns the same array, but ids instead of categories [3, 4]
child1.self_and_siblings # Returns an array with [child1], because it has no siblings

root1.children_count # Returns 1 as root has one immediate child
root1.descendants_count # Returns 3 as root has three descendants
subchild1.ancestors_count # Returns 2 as subchild1 has two ancestors - child1 and root
root.depth # Returns 0 - roots are at level 0
subchild2.depth # Returns 2
```

### Permissions
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails-3.0.gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "rails", "~> 3.0"
gem "acts_as_tree", path: "../"
gem "acts_as_category", path: "../"

gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails-3.1.gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "rails", "~> 3.1"
gem "acts_as_tree", path: "../"
gem "acts_as_category", path: "../"

gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails-3.2.gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "rails", "~> 3.2"
gem "acts_as_tree", path: "../"
gem "acts_as_category", path: "../"

gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails-4.0.gemfile
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "rails", "~> 4.0"
gem "acts_as_tree", path: "../"
gem "acts_as_category", path: "../"

gemspec path: "../"
16 changes: 12 additions & 4 deletions lib/acts_as_category/acts_as_category.rb
Expand Up @@ -296,6 +296,10 @@ def children_ids
children_ids
end

def has_children?
!self.children.empty?
end

# Returns list of ancestors, disregarding any permissions
def ancestors
node, nodes = self, []
Expand All @@ -313,6 +317,10 @@ def ancestors_ids
nodes
end

def depth
self.class.find(self.id).read_attribute(ancestors_count_column)
end

# Returns list of descendants, respecting permitted/hidden categories
def descendants
descendants = []
Expand Down Expand Up @@ -346,6 +354,10 @@ def root?
self.parent ? false : true
end

def is_root?
root?
end

# Returns all siblings of the current node, respecting permitted/hidden categories
def siblings
self_and_siblings - [self]
Expand All @@ -357,10 +369,6 @@ def has_siblings?
!siblings.empty?
end

def siblings?
has_siblings?
end

# Returns ids of all siblings of the current node, respecting permitted/hidden categories
def siblings_ids
self_and_siblings_ids - [self.id]
Expand Down
28 changes: 22 additions & 6 deletions test/category_test.rb
Expand Up @@ -217,6 +217,17 @@ def test_children
assert_equal [], @n221.children
end

def test_has_children?
assert @n22.has_children?
refute @n221.has_children?
end

def test_has_siblings_permissions
assert @n221.update_attribute('my_hidden', true)
refute @n22.has_children?
assert @n221.update_attribute('my_hidden', false)
end

def test_children_with_permissions
assert @n22.update_attribute('my_hidden', true)
assert_equal [@n21, @n22], @n2.orig_children
Expand Down Expand Up @@ -315,6 +326,10 @@ def test_ancestors_ids
assert_equal [@n22.id, @n2.id], @n221.ancestors_ids
end

def test_depth
assert_equal 2, @n111.depth
end

def test_descendants
assert_equal [@n11, @n111], @n1.descendants
assert_equal [@n21, @n211, @n22, @n221], @n2.descendants
Expand Down Expand Up @@ -389,7 +404,13 @@ def test_root_of_instance_with_permissions # should ignore permissions
def test_root?
assert @n1.root?
assert @n3.root?
assert !@n11.root?
refute @n11.root?
end

def test_is_root?
assert @n1.is_root?
assert @n3.is_root?
refute @n11.is_root?
end

def test_root_question_for_instance_with_permissions # should ignore permissions
Expand Down Expand Up @@ -443,11 +464,6 @@ def test_has_siblings?
assert @n21.has_siblings?
end

def test_siblings?
refute @n11.siblings?
assert @n21.siblings?
end

def test_has_siblings_permissions
assert @n2.update_attribute('my_hidden', true)
refute @n21.has_siblings?
Expand Down

0 comments on commit 808ee2d

Please sign in to comment.