Module: Ltree::Hierarchy::InstanceMethods
- Defined in:
- lib/ltree_hierarchy/hierarchy.rb
Instance Method Summary collapse
- #ancestors ⇒ Object
- #assign_path ⇒ Object
- #cascade_path_change ⇒ Object
- #children ⇒ Object
- #commit_path ⇒ Object
- #compute_path ⇒ Object
-
#depth ⇒ Object
1-based, for compatibility with ltree’s NLEVEL().
- #descendants ⇒ Object
- #descendents ⇒ Object
- #leaf? ⇒ Boolean
- #leaves ⇒ Object
- #ltree_fragment ⇒ Object
- #ltree_fragment_column ⇒ Object
- #ltree_parent_fragment ⇒ Object
- #ltree_parent_fragment_changed? ⇒ Boolean
- #ltree_parent_fragment_column ⇒ Object
- #ltree_path ⇒ Object
- #ltree_path_column ⇒ Object
- #ltree_path_was ⇒ Object
- #ltree_scope ⇒ Object
- #prevent_circular_paths ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
- #self_and_ancestors ⇒ Object (also: #and_ancestors)
- #self_and_children ⇒ Object (also: #and_children)
- #self_and_descendants ⇒ Object (also: #and_descendants)
- #self_and_descendents ⇒ Object (also: #and_descendents)
- #self_and_siblings ⇒ Object (also: #and_siblings)
- #siblings ⇒ Object
Instance Method Details
#ancestors ⇒ Object
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_path ⇒ Object
116 117 118 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 116 def assign_path self.send("#{ltree_path_column}=", compute_path) end |
#cascade_path_change ⇒ Object
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 |
#children ⇒ Object
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_path ⇒ Object
120 121 122 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 120 def commit_path update_column(ltree_path_column, compute_path) end |
#compute_path ⇒ Object
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 |
#depth ⇒ Object
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 |
#descendants ⇒ Object
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 |
#descendents ⇒ Object
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
144 145 146 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 144 def leaf? !children.exists? end |
#leaves ⇒ Object
212 213 214 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 212 def leaves descendants.leaves end |
#ltree_fragment ⇒ Object
74 75 76 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 74 def ltree_fragment send(ltree_fragment_column) end |
#ltree_fragment_column ⇒ Object
70 71 72 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 70 def ltree_fragment_column self.class.ltree_fragment_column end |
#ltree_parent_fragment ⇒ Object
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
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_column ⇒ Object
78 79 80 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 78 def ltree_parent_fragment_column self.class.ltree_parent_fragment_column end |
#ltree_path ⇒ Object
94 95 96 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 94 def ltree_path send(ltree_path_column) end |
#ltree_path_column ⇒ Object
90 91 92 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 90 def ltree_path_column self.class.ltree_path_column end |
#ltree_path_was ⇒ Object
98 99 100 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 98 def ltree_path_was send("#{ltree_path_column}_was") end |
#ltree_scope ⇒ Object
66 67 68 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 66 def ltree_scope self.class.base_class end |
#prevent_circular_paths ⇒ Object
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 |
#root ⇒ Object
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
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_ancestors ⇒ Object 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_children ⇒ Object 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_descendants ⇒ Object 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_descendents ⇒ Object 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_siblings ⇒ Object 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 |
#siblings ⇒ Object
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 |