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
277 278 279 |
# File 'lib/nested_set/base.rb', line 277 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.
196 197 198 199 200 201 202 203 204 |
# File 'lib/nested_set/base.rb', line 196 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
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/nested_set/base.rb', line 149 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
273 274 275 |
# File 'lib/nested_set/base.rb', line 273 def before_move(*args, &block) set_callback :move, :before, *args, &block end |
#each_root_valid?(roots_to_validate) ⇒ Boolean
206 207 208 209 210 211 212 213 214 |
# File 'lib/nested_set/base.rb', line 206 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|
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/nested_set/base.rb', line 252 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
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/nested_set/base.rb', line 167 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
265 266 267 268 269 270 271 |
# File 'lib/nested_set/base.rb', line 265 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
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/nested_set/base.rb', line 181 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.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/nested_set/base.rb', line 217 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! 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
122 123 124 |
# File 'lib/nested_set/base.rb', line 122 def root roots.first end |
#valid? ⇒ Boolean
163 164 165 |
# File 'lib/nested_set/base.rb', line 163 def valid? left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid? end |