Module: CollectiveIdea::Acts::NestedSet::Base::ClassMethods
- Defined in:
- lib/nested_set/base.rb
Instance Method Summary collapse
- #after_move(*args, &block) ⇒ Object
-
#all_roots_valid? ⇒ Boolean
Wrapper for each_root_valid? that can deal with scope.
-
#arrange ⇒ Object
Returns arranged nodes hash.
- #before_move(*args, &block) ⇒ Object
- #each_root_valid?(roots_to_validate) ⇒ Boolean
-
#each_with_level(objects = nil) ⇒ Object
Iterates over tree elements and determines the current level in the tree.
- #left_and_rights_valid? ⇒ Boolean
- #map_with_level(objects = nil) ⇒ Object
- #no_duplicates_for_columns? ⇒ Boolean
-
#rebuild! ⇒ Object
Rebuilds the left & rights if unset or invalid.
-
#root ⇒ Object
Returns the first root.
- #valid? ⇒ Boolean
Instance Method Details
#after_move(*args, &block) ⇒ Object
271 272 273 |
# File 'lib/nested_set/base.rb', line 271 def after_move(*args, &block) set_callback :move, :after, *args, &block end |
#all_roots_valid? ⇒ Boolean
Wrapper for each_root_valid? that can deal with scope.
190 191 192 193 194 195 196 197 198 |
# File 'lib/nested_set/base.rb', line 190 def all_roots_valid? if [:scope] roots.group_by{|record| scope_column_names.collect{|col| record.send(col.to_sym)}}.all? do |scope, grouped_roots| each_root_valid?(grouped_roots) end else each_root_valid?(roots) end end |
#arrange ⇒ Object
Returns arranged nodes hash. I.e. you have this tree:
1
2
3
4
5
6
7
Hash will looks like:
{1 => {2 => {}, 3 => {4 => {5 => {}}, 6 => {}}}, 7 => {}}
Usage:
Categories.arrange
Categories.find(42).children.arrange
Categories.find(42).descendants.arrange
Categories.find(42).self_and_descendants.arrange
This arranged hash can be rendered with recursive render_tree helper
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/nested_set/base.rb', line 143 def arrange arranged = ActiveSupport::OrderedHash.new insertion_points = [arranged] depth = 0 order("#{quoted_table_name}.#{quoted_left_column_name}").each_with_level do |node, level| next if level > depth && insertion_points.last.keys.last && node.parent_id != insertion_points.last.keys.last.id insertion_points.push insertion_points.last.values.last if level > depth (depth - level).times { insertion_points.pop } if level < depth insertion_points.last.merge! node => ActiveSupport::OrderedHash.new depth = level end arranged end |
#before_move(*args, &block) ⇒ Object
267 268 269 |
# File 'lib/nested_set/base.rb', line 267 def before_move(*args, &block) set_callback :move, :before, *args, &block end |
#each_root_valid?(roots_to_validate) ⇒ Boolean
200 201 202 203 204 205 206 207 208 |
# File 'lib/nested_set/base.rb', line 200 def each_root_valid?(roots_to_validate) left = right = 0 roots_to_validate.all? do |root| (root.left > left && root.right > right).tap do left = root.left right = root.right end end end |
#each_with_level(objects = nil) ⇒ Object
Iterates over tree elements and determines the current level in the tree. Only accepts default ordering, odering by an other column than lft does not work. This method is much more efficent than calling level because it doesn’t require any additional database queries.
Example:
Category.each_with_level(Category.root.self_and_descendants) do |o, level|
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/nested_set/base.rb', line 246 def each_with_level(objects = nil) levels = [] (objects || scoped).each do |i| if level = levels.index(i.parent_id) levels.slice!((level + 1)..-1) else levels << i.parent_id level = levels.size - 1 end yield(i, level) end end |
#left_and_rights_valid? ⇒ Boolean
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/nested_set/base.rb', line 161 def left_and_rights_valid? joins("LEFT OUTER JOIN #{quoted_table_name} AS parent ON " + "#{quoted_table_name}.#{quoted_parent_column_name} = parent.#{primary_key}"). where( "#{quoted_table_name}.#{quoted_left_column_name} IS NULL OR " + "#{quoted_table_name}.#{quoted_right_column_name} IS NULL OR " + "#{quoted_table_name}.#{quoted_left_column_name} >= " + "#{quoted_table_name}.#{quoted_right_column_name} OR " + "(#{quoted_table_name}.#{quoted_parent_column_name} IS NOT NULL AND " + "(#{quoted_table_name}.#{quoted_left_column_name} <= parent.#{quoted_left_column_name} OR " + "#{quoted_table_name}.#{quoted_right_column_name} >= parent.#{quoted_right_column_name}))" ).exists? == false end |
#map_with_level(objects = nil) ⇒ Object
259 260 261 262 263 264 265 |
# File 'lib/nested_set/base.rb', line 259 def map_with_level(objects = nil) result = [] each_with_level objects do |object, level| result << yield(object, level) end result end |
#no_duplicates_for_columns? ⇒ Boolean
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/nested_set/base.rb', line 175 def no_duplicates_for_columns? scope_string = Array([:scope]).map do |c| connection.quote_column_name(c) end.push(nil).join(", ") [quoted_left_column_name, quoted_right_column_name].all? do |column| # No duplicates unscoped.first( :select => "#{scope_string}#{column}, COUNT(#{column})", :group => "#{scope_string}#{column}", :having => "COUNT(#{column}) > 1" ).nil? end end |
#rebuild! ⇒ Object
Rebuilds the left & rights if unset or invalid. Also very useful for converting from acts_as_tree.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/nested_set/base.rb', line 211 def rebuild! # Don't rebuild a valid tree. return true if valid? indices = {} set_left_and_rights = lambda do |node| node_scope = scope_for_rebuild(node) # set left node[left_column_name] = indices[node_scope] += 1 # find nodes_for_rebuild(node, node_scope).each{ |n| set_left_and_rights.call(n) } # set right node[right_column_name] = indices[node_scope] += 1 node.save(:validate => false) end # Find root node(s) root_nodes_for_rebuild.each do |root_node| node_scope = scope_for_rebuild(root_node) # setup index for this scope indices[node_scope] ||= 0 set_left_and_rights.call(root_node) end end |
#root ⇒ Object
Returns the first root
116 117 118 |
# File 'lib/nested_set/base.rb', line 116 def root roots.first end |
#valid? ⇒ Boolean
157 158 159 |
# File 'lib/nested_set/base.rb', line 157 def valid? left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid? end |