Module: CollectiveIdea::Acts::NestedSet::Base::ClassMethods

Defined in:
lib/nested_set/base.rb

Instance Method Summary collapse

Instance Method Details

#after_move(*args, &block) ⇒ Object



275
276
277
# File 'lib/nested_set/base.rb', line 275

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.

Returns:

  • (Boolean)


194
195
196
197
198
199
200
201
202
# File 'lib/nested_set/base.rb', line 194

def all_roots_valid?
  if acts_as_nested_set_options[: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

#arrangeObject

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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/nested_set/base.rb', line 147

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



271
272
273
# File 'lib/nested_set/base.rb', line 271

def before_move(*args, &block)
  set_callback :move, :before, *args, &block
end

#each_root_valid?(roots_to_validate) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
211
212
# File 'lib/nested_set/base.rb', line 204

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|


250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/nested_set/base.rb', line 250

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

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/nested_set/base.rb', line 165

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



263
264
265
266
267
268
269
# File 'lib/nested_set/base.rb', line 263

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

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/nested_set/base.rb', line 179

def no_duplicates_for_columns?
  scope_string = scope_column_names.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.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/nested_set/base.rb', line 215

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

#rootObject

Returns the first root



120
121
122
# File 'lib/nested_set/base.rb', line 120

def root
  roots.first
end

#valid?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/nested_set/base.rb', line 161

def valid?
  left_and_rights_valid? && no_duplicates_for_columns? && all_roots_valid?
end