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().
- #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? ⇒ Boolean
- #self_and_ancestors ⇒ Object (also: #and_ancestors)
- #self_and_children ⇒ Object (also: #and_children)
- #self_and_descendents ⇒ Object (also: #and_descendents)
- #self_and_siblings ⇒ Object (also: #and_siblings)
- #siblings ⇒ Object
Instance Method Details
#ancestors ⇒ Object
149 150 151 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 149 def ancestors ltree_scope.where("#{ltree_path_column} @> ? AND #{ltree_fragment_column} != ?", ltree_path, ltree_fragment) end |
#assign_path ⇒ Object
107 108 109 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 107 def assign_path self.send("#{ltree_path_column}=", compute_path) end |
#cascade_path_change ⇒ Object
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 |
#children ⇒ Object
176 177 178 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 176 def children ltree_scope.where(ltree_parent_fragment_column => ltree_fragment) end |
#commit_path ⇒ Object
111 112 113 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 111 def commit_path update_column(ltree_path_column, compute_path) end |
#compute_path ⇒ Object
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 |
#depth ⇒ Object
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 |
#descendents ⇒ Object
167 168 169 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 167 def descendents ltree_scope.where("#{ltree_path_column} <@ ? AND #{ltree_fragment_column} != ?", ltree_path, ltree_fragment) end |
#leaf? ⇒ Boolean
135 136 137 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 135 def leaf? !children.any? end |
#leaves ⇒ Object
185 186 187 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 185 def leaves descendents.leaves end |
#ltree_fragment ⇒ Object
65 66 67 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 65 def ltree_fragment send(self.ltree_fragment_column) end |
#ltree_fragment_column ⇒ Object
61 62 63 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 61 def ltree_fragment_column self.class.ltree_fragment_column end |
#ltree_parent_fragment ⇒ Object
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
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_column ⇒ Object
69 70 71 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 69 def ltree_parent_fragment_column self.class.ltree_parent_fragment_column end |
#ltree_path ⇒ Object
85 86 87 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 85 def ltree_path send(ltree_path_column) end |
#ltree_path_column ⇒ Object
81 82 83 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 81 def ltree_path_column self.class.ltree_path_column end |
#ltree_path_was ⇒ Object
89 90 91 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 89 def ltree_path_was send("#{ltree_path_column}_was") end |
#ltree_scope ⇒ Object
57 58 59 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 57 def ltree_scope self.class.base_class end |
#prevent_circular_paths ⇒ Object
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 |
#root? ⇒ 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_ancestors ⇒ Object Also known as: and_ancestors
153 154 155 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 153 def self_and_ancestors ltree_scope.where("#{ltree_path_column} @> ?", ltree_path) end |
#self_and_children ⇒ Object Also known as: and_children
180 181 182 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 180 def self_and_children ltree_scope.where("#{ltree_fragment_column} = :id OR #{ltree_parent_fragment_column} = :id", :id => ltree_fragment) end |
#self_and_descendents ⇒ Object Also known as: and_descendents
171 172 173 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 171 def self_and_descendents ltree_scope.where("#{ltree_path_column} <@ ?", ltree_path) end |
#self_and_siblings ⇒ Object Also known as: and_siblings
162 163 164 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 162 def self_and_siblings ltree_scope.where(ltree_parent_fragment_column => ltree_parent_fragment) end |
#siblings ⇒ Object
158 159 160 |
# File 'lib/ltree_hierarchy/hierarchy.rb', line 158 def siblings ltree_scope.where("#{ltree_parent_fragment_column} = ? AND #{ltree_fragment_column} != ?", ltree_parent_fragment, ltree_fragment) end |