Module: ClosureTree::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/closure_tree/model.rb
Instance Method Summary collapse
-
#_ct ⇒ Object
Delegate to the Support instance on the class:.
- #_ct_id ⇒ Object
- #_ct_parent_id ⇒ Object
- #_ct_quoted_id ⇒ Object
- #_ct_quoted_parent_id ⇒ Object
-
#add_child(child_node) ⇒ Object
Alias for appending to the children collection.
- #ancestor_ids ⇒ Object
-
#ancestor_of?(node) ⇒ Boolean
node’s ancestors include this record.
-
#ancestors ⇒ Object
enumerable of ancestors, immediate parent is first, root is last.
-
#ancestry_path(to_s_column = _ct.name_column) ⇒ Object
Returns an array, root first, of self_and_ancestors’ values of the
to_s_column
, which defaults to thename_column
. -
#child? ⇒ Boolean
Returns true if this node has a parent, and is not a root.
- #child_ids ⇒ Object
-
#child_of?(node) ⇒ Boolean
node is record’s parent.
- #depth ⇒ Object (also: #level)
- #descendant_ids ⇒ Object
-
#descendant_of?(node) ⇒ Boolean
node is record’s ancestor.
- #descendants ⇒ Object
-
#family_of?(node) ⇒ Boolean
node and record have a same root.
-
#leaf? ⇒ Boolean
Returns true if this node has no children.
- #leaves ⇒ Object
-
#parent_of?(node) ⇒ Boolean
node’s parent is this record.
-
#root ⇒ Object
Returns the farthest ancestor, or self if
root?
. -
#root? ⇒ Boolean
Returns true if this node has no parents.
-
#root_of?(node) ⇒ Boolean
node’s root is this record.
- #self_and_ancestors_ids ⇒ Object
- #self_and_descendant_ids ⇒ Object
- #self_and_siblings ⇒ Object
- #sibling_ids ⇒ Object
- #siblings ⇒ Object
Instance Method Details
#_ct ⇒ Object
Delegate to the Support instance on the class:
53 54 55 |
# File 'lib/closure_tree/model.rb', line 53 def _ct self.class._ct end |
#_ct_id ⇒ Object
182 183 184 |
# File 'lib/closure_tree/model.rb', line 182 def _ct_id read_attribute(_ct.model_class.primary_key) end |
#_ct_parent_id ⇒ Object
174 175 176 |
# File 'lib/closure_tree/model.rb', line 174 def _ct_parent_id read_attribute(_ct.parent_column_sym) end |
#_ct_quoted_id ⇒ Object
186 187 188 |
# File 'lib/closure_tree/model.rb', line 186 def _ct_quoted_id _ct.quoted_value(_ct_id) end |
#_ct_quoted_parent_id ⇒ Object
178 179 180 |
# File 'lib/closure_tree/model.rb', line 178 def _ct_quoted_parent_id _ct.quoted_value(_ct_parent_id) end |
#add_child(child_node) ⇒ Object
Alias for appending to the children collection. You can also add directly to the children collection, if you’d prefer.
169 170 171 172 |
# File 'lib/closure_tree/model.rb', line 169 def add_child(child_node) children << child_node child_node end |
#ancestor_ids ⇒ Object
94 95 96 |
# File 'lib/closure_tree/model.rb', line 94 def ancestor_ids _ct.ids_from(ancestors) end |
#ancestor_of?(node) ⇒ Boolean
node’s ancestors include this record
148 149 150 |
# File 'lib/closure_tree/model.rb', line 148 def ancestor_of?(node) node.ancestors.include? self end |
#ancestors ⇒ Object
enumerable of ancestors, immediate parent is first, root is last.
90 91 92 |
# File 'lib/closure_tree/model.rb', line 90 def ancestors without_self(self_and_ancestors) end |
#ancestry_path(to_s_column = _ct.name_column) ⇒ Object
Returns an array, root first, of self_and_ancestors’ values of the to_s_column
, which defaults to the name_column
. (so child.ancestry_path == %w{grandparent parent child}
105 106 107 |
# File 'lib/closure_tree/model.rb', line 105 def ancestry_path(to_s_column = _ct.name_column) self_and_ancestors.map { |n| n.send to_s_column.to_sym }.reverse end |
#child? ⇒ Boolean
Returns true if this node has a parent, and is not a root.
65 66 67 |
# File 'lib/closure_tree/model.rb', line 65 def child? !root? end |
#child_ids ⇒ Object
109 110 111 |
# File 'lib/closure_tree/model.rb', line 109 def child_ids _ct.ids_from(children) end |
#child_of?(node) ⇒ Boolean
node is record’s parent
158 159 160 |
# File 'lib/closure_tree/model.rb', line 158 def child_of?(node) self.parent == node end |
#depth ⇒ Object Also known as: level
83 84 85 |
# File 'lib/closure_tree/model.rb', line 83 def depth ancestors.size end |
#descendant_ids ⇒ Object
121 122 123 |
# File 'lib/closure_tree/model.rb', line 121 def descendant_ids _ct.ids_from(descendants) end |
#descendant_of?(node) ⇒ Boolean
node is record’s ancestor
153 154 155 |
# File 'lib/closure_tree/model.rb', line 153 def descendant_of?(node) self.ancestors.include? node end |
#descendants ⇒ Object
113 114 115 |
# File 'lib/closure_tree/model.rb', line 113 def descendants without_self(self_and_descendants) end |
#family_of?(node) ⇒ Boolean
node and record have a same root
163 164 165 |
# File 'lib/closure_tree/model.rb', line 163 def family_of?(node) self.root == node.root end |
#leaf? ⇒ Boolean
Returns true if this node has no children.
70 71 72 |
# File 'lib/closure_tree/model.rb', line 70 def leaf? children.empty? end |
#leaves ⇒ Object
79 80 81 |
# File 'lib/closure_tree/model.rb', line 79 def leaves self_and_descendants.leaves end |
#parent_of?(node) ⇒ Boolean
node’s parent is this record
138 139 140 |
# File 'lib/closure_tree/model.rb', line 138 def parent_of?(node) self == node.parent end |
#root ⇒ Object
Returns the farthest ancestor, or self if root?
75 76 77 |
# File 'lib/closure_tree/model.rb', line 75 def root self_and_ancestors.where(_ct.parent_column_name.to_sym => nil).first end |
#root? ⇒ Boolean
Returns true if this node has no parents.
58 59 60 61 62 |
# File 'lib/closure_tree/model.rb', line 58 def root? # Accessing the parent will fetch that row from the database, # so if we are persisted, just check that the parent_id column is nil. persisted? ? _ct_parent_id.nil? : parent.nil? end |
#root_of?(node) ⇒ Boolean
node’s root is this record
143 144 145 |
# File 'lib/closure_tree/model.rb', line 143 def root_of?(node) self == node.root end |
#self_and_ancestors_ids ⇒ Object
98 99 100 |
# File 'lib/closure_tree/model.rb', line 98 def self_and_ancestors_ids _ct.ids_from(self_and_ancestors) end |
#self_and_descendant_ids ⇒ Object
117 118 119 |
# File 'lib/closure_tree/model.rb', line 117 def self_and_descendant_ids _ct.ids_from(self_and_descendants) end |
#self_and_siblings ⇒ Object
125 126 127 |
# File 'lib/closure_tree/model.rb', line 125 def self_and_siblings _ct.scope_with_order(_ct.base_class.where(_ct.parent_column_sym => _ct_parent_id)) end |
#sibling_ids ⇒ Object
133 134 135 |
# File 'lib/closure_tree/model.rb', line 133 def sibling_ids _ct.ids_from(siblings) end |
#siblings ⇒ Object
129 130 131 |
# File 'lib/closure_tree/model.rb', line 129 def siblings without_self(self_and_siblings) end |