module Parentry::Navigation
Public Instance Methods
ancestor_ids()
click to toggle source
# File lib/parentry/navigation.rb, line 25 def ancestor_ids path_ids[0..-2] end
ancestors(scopes = {})
click to toggle source
# File lib/parentry/navigation.rb, line 29 def ancestors(scopes = {}) with_depth_scopes(scopes) do parentry_scope.where(id: ancestor_ids).order_by_parentry end end
children?()
click to toggle source
# File lib/parentry/navigation.rb, line 40 def children? !leaf? end
depth()
click to toggle source
# File lib/parentry/navigation.rb, line 80 def depth return depth_offset if root? return parent.depth + 1 if changes[:parent_id].present? path_ids.size - 1 + depth_offset end
descendant_ids(scopes = {})
click to toggle source
# File lib/parentry/navigation.rb, line 70 def descendant_ids(scopes = {}) descendants(scopes).pluck(:id) end
descendants(scopes = {})
click to toggle source
# File lib/parentry/navigation.rb, line 74 def descendants(scopes = {}) with_depth_scopes(scopes) do subtree.where.not(id: id) end end
leaf?()
click to toggle source
# File lib/parentry/navigation.rb, line 35 def leaf? children.size.zero? end
Also aliased as: childless?
only_child?()
click to toggle source
# File lib/parentry/navigation.rb, line 56 def only_child? !siblings? end
path(scopes = {})
click to toggle source
# File lib/parentry/navigation.rb, line 19 def path(scopes = {}) with_depth_scopes(scopes) do parentry_scope.where(id: path_ids).order_by_parentry end end
path_ids()
click to toggle source
# File lib/parentry/navigation.rb, line 15 def path_ids parse_parentry end
root()
click to toggle source
# File lib/parentry/navigation.rb, line 7 def root unscoped_find(root_id) end
root?()
click to toggle source
# File lib/parentry/navigation.rb, line 11 def root? parent_id.nil? end
root_id()
click to toggle source
# File lib/parentry/navigation.rb, line 3 def root_id path_ids[0] end
sibling_ids()
click to toggle source
# File lib/parentry/navigation.rb, line 44 def sibling_ids siblings.pluck(:id) end
siblings()
click to toggle source
# File lib/parentry/navigation.rb, line 48 def siblings parentry_scope.where.not(id: id).where.not(parent_id: nil).where(parent_id: parent_id) end
siblings?()
click to toggle source
# File lib/parentry/navigation.rb, line 52 def siblings? siblings.size.positive? end
subtree(scopes = {})
click to toggle source
# File lib/parentry/navigation.rb, line 64 def subtree(scopes = {}) with_depth_scopes(scopes) do parentry_scope.where(subtree_conditions) end end
subtree_ids(scopes = {})
click to toggle source
# File lib/parentry/navigation.rb, line 60 def subtree_ids(scopes = {}) subtree(scopes).pluck(:id) end
Private Instance Methods
unscoped_find(id)
click to toggle source
# File lib/parentry/navigation.rb, line 89 def unscoped_find(id) parentry_scope.unscoped { parentry_scope.find(id) } end
with_depth_scopes(scopes) { || ... }
click to toggle source
# File lib/parentry/navigation.rb, line 93 def with_depth_scopes(scopes) scopes.reduce(yield) do |memo, (scope, depth)| memo.send(scope, depth) end end