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

Commit

Permalink
add is_only_child? instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes committed Sep 28, 2014
1 parent 808ee2d commit 53d5d56
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ActsAsCategory
[![Build Status](https://secure.travis-ci.org/mbrookes/acts_as_category.svg?branch=master)](http://travis-ci.org/mbrookes/acts_as_tree)
[![Build Status](https://secure.travis-ci.org/mbrookes/acts_as_category.svg?branch=master)](http://travis-ci.org/mbrookes/acts_as_category)
[![Gem Version](https://badge.fury.io/rb/acts_as_category.svg)](http://badge.fury.io/rb/acts_as_category)

acts_as_category (Version 2.0 beta)
Expand Down Expand Up @@ -228,8 +228,9 @@ RDoc.
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 true as child1 has children
child1.child_ids # Returns the same array, but ids instead of categories [3, 4]
child1.has_children? # Returns true as child1 has children
child1.is_childless? # Returns false 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]
Expand All @@ -241,6 +242,7 @@ RDoc.

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

Expand Down
15 changes: 11 additions & 4 deletions lib/acts_as_category/acts_as_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,16 @@ def children_ids
self.children.each { |child| children_ids << child.id if child.permitted? } unless self.children.empty?
children_ids
end
alias_method :child_ids, :children_ids

def has_children?
!self.children.empty?
end

def is_childless?
!has_children?
end

# Returns list of ancestors, disregarding any permissions
def ancestors
node, nodes = self, []
Expand Down Expand Up @@ -353,10 +358,7 @@ def root
def root?
self.parent ? false : true
end

def is_root?
root?
end
alias_method :is_root?, :root?

# Returns all siblings of the current node, respecting permitted/hidden categories
def siblings
Expand All @@ -369,6 +371,11 @@ def has_siblings?
!siblings.empty?
end

def is_only_child?
!has_siblings?
end
alias_method :only_child?, :is_only_child?

# Returns ids of all siblings of the current node, respecting permitted/hidden categories
def siblings_ids
self_and_siblings_ids - [self.id]
Expand Down
22 changes: 22 additions & 0 deletions test/category_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ def test_has_children?
refute @n221.has_children?
end

def test_is_childless?
refute @n22.is_childless?
assert @n221.is_childless?
end

def test_has_siblings_permissions
assert @n221.update_attribute('my_hidden', true)
refute @n22.has_children?
Expand Down Expand Up @@ -254,6 +259,18 @@ def test_children_ids
assert_equal [], @n221.children_ids
end

def test_child_ids
assert_equal [@n11.id], @n1.child_ids
assert_equal [@n21.id, @n22.id], @n2.child_ids
assert_equal [], @n3.child_ids
assert_equal [@n111.id], @n11.child_ids
assert_equal [], @n111.children_ids
assert_equal [@n211.id], @n21.child_ids
assert_equal [@n221.id], @n22.child_ids
assert_equal [], @n211.child_ids
assert_equal [], @n221.child_ids
end

def test_children_ids_with_permissions
assert @n22.update_attribute('my_hidden', true)
assert_equal [@n11.id], @n1.children_ids
Expand Down Expand Up @@ -464,6 +481,11 @@ def test_has_siblings?
assert @n21.has_siblings?
end

def test_only_child?
assert @n11.only_child?
refute @n21.only_child?
end

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

0 comments on commit 53d5d56

Please sign in to comment.