Module: Ltree::Hierarchy::InstanceMethods

Defined in:
lib/ltree_hierarchy/hierarchy.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



153
154
155
# File 'lib/ltree_hierarchy/hierarchy.rb', line 153

def ancestors
  ltree_scope.where("#{ltree_path_column} @> ? AND #{ltree_fragment_column} != ?", ltree_path, ltree_fragment)
end

#assign_pathObject



107
108
109
# File 'lib/ltree_hierarchy/hierarchy.rb', line 107

def assign_path
  self.send("#{ltree_path_column}=", compute_path)
end

#cascade_path_changeObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ltree_hierarchy/hierarchy.rb', line 115

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_path_column} <@ :old_path AND #{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

#childrenObject



180
181
182
# File 'lib/ltree_hierarchy/hierarchy.rb', line 180

def children
  ltree_scope.where(ltree_parent_fragment_column => ltree_fragment)
end

#commit_pathObject



111
112
113
# File 'lib/ltree_hierarchy/hierarchy.rb', line 111

def commit_path
  update_column(ltree_path_column, compute_path)
end

#compute_pathObject



99
100
101
102
103
104
105
# File 'lib/ltree_hierarchy/hierarchy.rb', line 99

def compute_path
  if parent
    "#{parent.ltree_path}.#{ltree_fragment}"
  else
    ltree_fragment.to_s
  end
end

#depthObject

1-based, for compatibility with ltree’s nlevel().



139
140
141
142
143
144
145
146
147
# File 'lib/ltree_hierarchy/hierarchy.rb', line 139

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

#descendentsObject



171
172
173
# File 'lib/ltree_hierarchy/hierarchy.rb', line 171

def descendents
  ltree_scope.where("#{ltree_path_column} <@ ? AND #{ltree_fragment_column} != ?", ltree_path, ltree_fragment)
end

#leaf?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/ltree_hierarchy/hierarchy.rb', line 135

def leaf?
  !children.exists?
end

#leavesObject



189
190
191
# File 'lib/ltree_hierarchy/hierarchy.rb', line 189

def leaves
  descendents.leaves
end

#ltree_fragmentObject



65
66
67
# File 'lib/ltree_hierarchy/hierarchy.rb', line 65

def ltree_fragment
  send(self.ltree_fragment_column)
end

#ltree_fragment_columnObject



61
62
63
# File 'lib/ltree_hierarchy/hierarchy.rb', line 61

def ltree_fragment_column
  self.class.ltree_fragment_column
end

#ltree_parent_fragmentObject



73
74
75
# File 'lib/ltree_hierarchy/hierarchy.rb', line 73

def ltree_parent_fragment
  send(ltree_parent_fragment_column)
end

#ltree_parent_fragment_changed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/ltree_hierarchy/hierarchy.rb', line 77

def ltree_parent_fragment_changed?
  changed_attributes.key?(ltree_parent_fragment_column.to_s)
end

#ltree_parent_fragment_columnObject



69
70
71
# File 'lib/ltree_hierarchy/hierarchy.rb', line 69

def ltree_parent_fragment_column
  self.class.ltree_parent_fragment_column
end

#ltree_pathObject



85
86
87
# File 'lib/ltree_hierarchy/hierarchy.rb', line 85

def ltree_path
  send(ltree_path_column)
end

#ltree_path_columnObject



81
82
83
# File 'lib/ltree_hierarchy/hierarchy.rb', line 81

def ltree_path_column
  self.class.ltree_path_column
end

#ltree_path_wasObject



89
90
91
# File 'lib/ltree_hierarchy/hierarchy.rb', line 89

def ltree_path_was
  send("#{ltree_path_column}_was")
end

#ltree_scopeObject



57
58
59
# File 'lib/ltree_hierarchy/hierarchy.rb', line 57

def ltree_scope
  self.class.base_class
end

#prevent_circular_pathsObject



93
94
95
96
97
# File 'lib/ltree_hierarchy/hierarchy.rb', line 93

def prevent_circular_paths
  if parent && parent.ltree_path.split('.').include?(ltree_fragment.to_s)
    errors.add(ltree_parent_fragment_column, :invalid)
  end
end

#rootObject



149
150
151
# File 'lib/ltree_hierarchy/hierarchy.rb', line 149

def root
  ltree_scope.where("#{ltree_path_column} = subpath(?, 0, 1)", ltree_path).first
end

#root?Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/ltree_hierarchy/hierarchy.rb', line 127

def root?
  if self.ltree_parent_fragment
    false
  else
    parent.nil?
  end
end

#self_and_ancestorsObject Also known as: and_ancestors



157
158
159
# File 'lib/ltree_hierarchy/hierarchy.rb', line 157

def self_and_ancestors
  ltree_scope.where("#{ltree_path_column} @> ?", ltree_path)
end

#self_and_childrenObject Also known as: and_children



184
185
186
# File 'lib/ltree_hierarchy/hierarchy.rb', line 184

def self_and_children
  ltree_scope.where("#{ltree_fragment_column} = :id OR #{ltree_parent_fragment_column} = :id", :id => ltree_fragment)
end

#self_and_descendentsObject Also known as: and_descendents



175
176
177
# File 'lib/ltree_hierarchy/hierarchy.rb', line 175

def self_and_descendents
  ltree_scope.where("#{ltree_path_column} <@ ?", ltree_path)
end

#self_and_siblingsObject Also known as: and_siblings



166
167
168
# File 'lib/ltree_hierarchy/hierarchy.rb', line 166

def self_and_siblings
  ltree_scope.where(ltree_parent_fragment_column => ltree_parent_fragment)
end

#siblingsObject



162
163
164
# File 'lib/ltree_hierarchy/hierarchy.rb', line 162

def siblings
  ltree_scope.where("#{ltree_parent_fragment_column} = ? AND #{ltree_fragment_column} != ?", ltree_parent_fragment, ltree_fragment)
end