Module: EvenBetterNestedSet::NestedSet
- Defined in:
- lib/eb_nested_set.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #ancestors(force_reload = false) ⇒ Object
- #bounds ⇒ Object
-
#cache_children(*nodes) ⇒ Object
:nodoc:.
- #cache_nested_set ⇒ Object
-
#cache_parent(parent) ⇒ Object
:nodoc:.
- #children ⇒ Object
- #children? ⇒ Boolean
- #descendant_of?(node) ⇒ Boolean
- #descendants ⇒ Object
- #family ⇒ Object
- #family_ids(force_reload = false) ⇒ Object
- #generation ⇒ Object
- #kin ⇒ Object
- #left ⇒ Object
- #level ⇒ Object
- #lineage(force_reload = false) ⇒ Object
- #recalculate_nested_set(left) ⇒ Object
- #right ⇒ Object
- #root ⇒ Object (also: #patriarch)
- #root? ⇒ Boolean
- #siblings ⇒ Object
Class Method Details
.included(base) ⇒ Object
13 14 15 16 |
# File 'lib/eb_nested_set.rb', line 13 def self.included(base) super base.extend ClassMethods end |
Instance Method Details
#ancestors(force_reload = false) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/eb_nested_set.rb', line 109 def ancestors(force_reload=false) @ancestors = nil if force_reload @ancestors ||= base_class.find( :all,:conditions => ["#{nested_set_column(:left)} < ? AND #{nested_set_column(:right)} > ?", left, right], :order => "#{nested_set_column(:left)} DESC" ) end |
#bounds ⇒ Object
167 168 169 |
# File 'lib/eb_nested_set.rb', line 167 def bounds left..right end |
#cache_children(*nodes) ⇒ Object
:nodoc:
183 184 185 186 |
# File 'lib/eb_nested_set.rb', line 183 def cache_children(*nodes) #:nodoc: @cached_children ||= [] @cached_children.push(*nodes) end |
#cache_nested_set ⇒ Object
129 130 131 |
# File 'lib/eb_nested_set.rb', line 129 def cache_nested_set @cached_children || base_class.sort_nodes_to_nested_set(family) end |
#cache_parent(parent) ⇒ Object
:nodoc:
179 180 181 |
# File 'lib/eb_nested_set.rb', line 179 def cache_parent(parent) #:nodoc: self.parent = parent end |
#children ⇒ Object
171 172 173 |
# File 'lib/eb_nested_set.rb', line 171 def children @cached_children || uncached_children end |
#children? ⇒ Boolean
175 176 177 |
# File 'lib/eb_nested_set.rb', line 175 def children? children.empty? end |
#descendant_of?(node) ⇒ Boolean
96 97 98 |
# File 'lib/eb_nested_set.rb', line 96 def descendant_of?(node) node.left < self.left && self.right < node.right end |
#descendants ⇒ Object
125 126 127 |
# File 'lib/eb_nested_set.rb', line 125 def descendants base_class.descendants(self) end |
#family ⇒ Object
133 134 135 |
# File 'lib/eb_nested_set.rb', line 133 def family [self, *descendants] end |
#family_ids(force_reload = false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/eb_nested_set.rb', line 137 def family_ids(force_reload=false) return @family_ids unless @family_ids.nil? or force_reload transaction do reload_boundaries query = "SELECT id FROM #{self.class.quote_db_property(base_class.table_name)} " + "WHERE #{nested_set_column(:left)} >= #{left} AND #{nested_set_column(:right)} <= #{right} " + "ORDER BY #{nested_set_column(:left)}" @family_ids = base_class.connection.select_values(query).map(&:to_i) end end |
#generation ⇒ Object
149 150 151 |
# File 'lib/eb_nested_set.rb', line 149 def generation root? ? base_class.roots : parent.children end |
#kin ⇒ Object
121 122 123 |
# File 'lib/eb_nested_set.rb', line 121 def kin patriarch.family end |
#left ⇒ Object
188 189 190 |
# File 'lib/eb_nested_set.rb', line 188 def left read_attribute(self.class.[:left]) end |
#level ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/eb_nested_set.rb', line 157 def level if root? 0 elsif @ancestors @ancestors.size else base_class.count :conditions => ["#{nested_set_column(:left)} < ? AND #{nested_set_column(:right)} > ?", left, right] end end |
#lineage(force_reload = false) ⇒ Object
117 118 119 |
# File 'lib/eb_nested_set.rb', line 117 def lineage(force_reload=false) [self, *ancestors(force_reload)] end |
#recalculate_nested_set(left) ⇒ Object
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/eb_nested_set.rb', line 196 def recalculate_nested_set(left) child_left = left + 1 children.each do |child| child_left = child.recalculate_nested_set(child_left) end set_boundaries(left, child_left) save_without_validation! right + 1 end |
#right ⇒ Object
192 193 194 |
# File 'lib/eb_nested_set.rb', line 192 def right read_attribute(self.class.[:right]) end |
#root ⇒ Object Also known as: patriarch
100 101 102 103 104 105 |
# File 'lib/eb_nested_set.rb', line 100 def root transaction do reload_boundaries @root ||= base_class.roots.find(:first, :conditions => ["#{nested_set_column(:left)} <= ? AND #{nested_set_column(:right)} >= ?", left, right]) end end |
#root? ⇒ Boolean
92 93 94 |
# File 'lib/eb_nested_set.rb', line 92 def root? not parent_id? end |
#siblings ⇒ Object
153 154 155 |
# File 'lib/eb_nested_set.rb', line 153 def siblings generation - [self] end |