Module: Ltree::Hierarchy::InstanceMethods

Defined in:
lib/ltree_hierarchy/hierarchy.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



162
163
164
# 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

#assign_pathObject



116
117
118
# File 'lib/ltree_hierarchy/hierarchy.rb', line 116

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

#cascade_path_changeObject



124
125
126
127
128
129
130
131
132
133
134
# 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

#childrenObject



203
204
205
# 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_pathObject



120
121
122
# File 'lib/ltree_hierarchy/hierarchy.rb', line 120

def commit_path
  update_column(ltree_path_column, compute_path)
end

#compute_pathObject



108
109
110
111
112
113
114
# 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

#depthObject

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



148
149
150
151
152
153
154
155
156
# 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

#descendantsObject



183
184
185
# 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

#descendentsObject



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

def descendents
  warn 'This method has been deprecated. Use #descendants instead'
  descendants
end

#leaf?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/ltree_hierarchy/hierarchy.rb', line 144

def leaf?
  !children.exists?
end

#leavesObject



212
213
214
# File 'lib/ltree_hierarchy/hierarchy.rb', line 212

def leaves
  descendants.leaves
end

#ltree_fragmentObject



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

def ltree_fragment
  send(ltree_fragment_column)
end

#ltree_fragment_columnObject



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

def ltree_fragment_column
  self.class.ltree_fragment_column
end

#ltree_parent_fragmentObject



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

def ltree_parent_fragment
  send(ltree_parent_fragment_column)
end

#ltree_parent_fragment_changed?Boolean

Returns:

  • (Boolean)


86
87
88
# 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_columnObject



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

def ltree_parent_fragment_column
  self.class.ltree_parent_fragment_column
end

#ltree_pathObject



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

def ltree_path
  send(ltree_path_column)
end

#ltree_path_columnObject



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

def ltree_path_column
  self.class.ltree_path_column
end

#ltree_path_wasObject



98
99
100
# File 'lib/ltree_hierarchy/hierarchy.rb', line 98

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

#ltree_scopeObject



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

def ltree_scope
  self.class.base_class
end

#prevent_circular_pathsObject



102
103
104
105
106
# 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

#rootObject



158
159
160
# 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?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
# File 'lib/ltree_hierarchy/hierarchy.rb', line 136

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

#self_and_ancestorsObject Also known as: and_ancestors



166
167
168
# 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

#self_and_childrenObject Also known as: and_children



207
208
209
# 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

#self_and_descendantsObject Also known as: and_descendants



192
193
194
# 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

#self_and_descendentsObject Also known as: and_descendents



197
198
199
200
# 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

#self_and_siblingsObject Also known as: and_siblings



178
179
180
# 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

#siblingsObject



171
172
173
174
175
176
# 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