Module: Parentry::Navigation

Defined in:
lib/parentry/navigation.rb

Instance Method Summary collapse

Instance Method Details

#ancestor_idsObject



25
26
27
# File 'lib/parentry/navigation.rb', line 25

def ancestor_ids
  path_ids[0..-2]
end

#ancestors(scopes = {}) ⇒ Object



29
30
31
32
33
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/parentry/navigation.rb', line 40

def children?
  !leaf?
end

#depthObject



80
81
82
83
84
85
# 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 = {}) ⇒ Object



70
71
72
# File 'lib/parentry/navigation.rb', line 70

def descendant_ids(scopes = {})
  descendants(scopes).pluck(:id)
end

#descendants(scopes = {}) ⇒ Object



74
75
76
77
78
# File 'lib/parentry/navigation.rb', line 74

def descendants(scopes = {})
  with_depth_scopes(scopes) do
    subtree.where.not(id:)
  end
end

#leaf?Boolean Also known as: childless?

Returns:

  • (Boolean)


35
36
37
# File 'lib/parentry/navigation.rb', line 35

def leaf?
  children.empty?
end

#only_child?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/parentry/navigation.rb', line 56

def only_child?
  !siblings?
end

#path(scopes = {}) ⇒ Object



19
20
21
22
23
# 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_idsObject



15
16
17
# File 'lib/parentry/navigation.rb', line 15

def path_ids
  parse_parentry
end

#rootObject



7
8
9
# File 'lib/parentry/navigation.rb', line 7

def root
  unscoped_find(root_id)
end

#root?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/parentry/navigation.rb', line 11

def root?
  parent_id.nil?
end

#root_idObject



3
4
5
# File 'lib/parentry/navigation.rb', line 3

def root_id
  path_ids[0]
end

#sibling_idsObject



44
45
46
# File 'lib/parentry/navigation.rb', line 44

def sibling_ids
  siblings.pluck(:id)
end

#siblingsObject



48
49
50
# File 'lib/parentry/navigation.rb', line 48

def siblings
  parentry_scope.where.not(id:).where.not(parent_id: nil).where(parent_id:)
end

#siblings?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/parentry/navigation.rb', line 52

def siblings?
  siblings.size.positive?
end

#subtree(scopes = {}) ⇒ Object



64
65
66
67
68
# 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 = {}) ⇒ Object



60
61
62
# File 'lib/parentry/navigation.rb', line 60

def subtree_ids(scopes = {})
  subtree(scopes).pluck(:id)
end