module Ltree::Hierarchy::InstanceMethods

Public Instance Methods

ancestors() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 162
def ancestors
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} @> ? AND #{ltree_scope.table_name}.#{ltree_fragment_column} != ?", ltree_path, ltree_fragment)
end
and_ancestors()
Alias for: self_and_ancestors
and_children()
Alias for: self_and_children
and_descendants()
and_descendents()
and_siblings()
Alias for: self_and_siblings
assign_path() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 116
def assign_path
  self.send("#{ltree_path_column}=", compute_path)
end
cascade_path_change() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 124
def cascade_path_change
  # Typically equivalent to:
  #  UPDATE whatever
  #  SET    path = NEW.path || subpath(path, nlevel(OLD.path))
  #  WHERE  path <@ OLD.path AND id != NEW.id;
  ltree_scope.where(
    ["#{ltree_scope.table_name}.#{ltree_path_column} <@ :old_path AND #{ltree_scope.table_name}.#{ltree_fragment_column} != :id", old_path: ltree_path_was, id: ltree_fragment]
  ).update_all(
    ["#{ltree_path_column} = :new_path || subpath(#{ltree_path_column}, nlevel(:old_path))", new_path: ltree_path, old_path: ltree_path_was]
  )
end
children() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 203
def children
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_parent_fragment_column}" => ltree_fragment)
end
commit_path() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 120
def commit_path
  update_column(ltree_path_column, compute_path)
end
compute_path() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 108
def compute_path
  if parent
    "#{parent.ltree_path}.#{ltree_fragment}"
  else
    ltree_fragment.to_s
  end
end
depth() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 148
def depth # 1-based, for compatibility with ltree's NLEVEL().
  if root?
    1
  elsif ltree_path
    ltree_path.split(".").length
  elsif parent
    parent.depth + 1
  end
end
descendants() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 183
def descendants
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} <@ ? AND #{ltree_scope.table_name}.#{ltree_fragment_column} != ?", ltree_path, ltree_fragment)
end
descendents() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 187
def descendents
  warn 'This method has been deprecated. Use #descendants instead'
  descendants
end
leaf?() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 144
def leaf?
  !children.exists?
end
leaves() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 212
def leaves
  descendants.leaves
end
ltree_fragment() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 74
def ltree_fragment
  send(ltree_fragment_column)
end
ltree_fragment_column() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 70
def ltree_fragment_column
  self.class.ltree_fragment_column
end
ltree_parent_fragment() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 82
def ltree_parent_fragment
  send(ltree_parent_fragment_column)
end
ltree_parent_fragment_changed?() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 86
def ltree_parent_fragment_changed?
  changed_attributes.key?(ltree_parent_fragment_column.to_s)
end
ltree_parent_fragment_column() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 78
def ltree_parent_fragment_column
  self.class.ltree_parent_fragment_column
end
ltree_path() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 94
def ltree_path
  send(ltree_path_column)
end
ltree_path_column() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 90
def ltree_path_column
  self.class.ltree_path_column
end
ltree_path_was() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 98
def ltree_path_was
  send("#{ltree_path_column}_was")
end
ltree_scope() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 66
def ltree_scope
  self.class.base_class
end
prevent_circular_paths() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 102
def prevent_circular_paths
  if parent && parent.ltree_path.split(".").include?(ltree_fragment.to_s)
    errors.add(ltree_parent_fragment_column, :invalid)
  end
end
root() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 158
def root
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} = SUBPATH(?, 0, 1)", ltree_path).first
end
root?() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 136
def root?
  if ltree_parent_fragment
    false
  else
    parent.nil?
  end
end
self_and_ancestors() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 166
def self_and_ancestors
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} @> ?", ltree_path)
end
Also aliased as: and_ancestors
self_and_children() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 207
def self_and_children
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_fragment_column} = :id OR #{ltree_scope.table_name}.#{ltree_parent_fragment_column} = :id", id: ltree_fragment)
end
Also aliased as: and_children
self_and_descendants() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 192
def self_and_descendants
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} <@ ?", ltree_path)
end
Also aliased as: and_descendants
self_and_descendents() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 197
def self_and_descendents
  warn 'This method has been deprecated. Use #self_and_descendants instead'
  self_and_descendants
end
Also aliased as: and_descendents
self_and_siblings() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 178
def self_and_siblings
  ltree_scope.where("#{ltree_scope.table_name}.#{ltree_parent_fragment_column}" => ltree_parent_fragment)
end
Also aliased as: and_siblings
siblings() click to toggle source
# File lib/ltree_hierarchy/hierarchy.rb, line 171
def siblings
  ltree_scope.where(
    "#{ltree_scope.table_name}.#{ltree_parent_fragment_column} = ? AND #{ltree_scope.table_name}.#{ltree_fragment_column} != ?",
    ltree_parent_fragment, ltree_fragment
  )
end