Module: Mongoid::Tree::RationalNumbering::ClassMethods

Defined in:
lib/mongoid/tree/rational_numbering.rb

Overview

included do

Instance Method Summary collapse

Instance Method Details

#rekey_all!Object

Force all rational number keys to update

Can be used to remove any “gaps” that are in a tree

For large collections, this uses a lot of resources, and should probably be used in a backround job on production sites. As a rational tree works just fine even if there are missing items, this shouldn’t be necessary to do that often.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mongoid/tree/rational_numbering.rb', line 67

def rekey_all!
  # rekey keys for each root. will do children
  _pos = 1
  root_rational = RationalNumber.new
  self.roots.each do |root|
    new_rational = root_rational.child_from_position(_pos)
    if new_rational != root.rational_number
      root.move_to_rational_number(new_rational.nv, new_rational.dv, {:force => true})
      root.save_with_force_rational_numbers!
      # root.reload # Should caller be responsible for reloading?
    end
    root.rekey_children
    _pos += 1
  end
end