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

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

Overview

included do

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auto_tree_timestampingObject



82
83
84
# File 'lib/mongoid/tree/rational_numbering.rb', line 82

def auto_tree_timestamping
  @auto_tree_timestamping.nil? ? true : @auto_tree_timestamping
end

Instance Method Details

#rational_number_options(opts) ⇒ Object

Set options for rational numbers

:auto_tree_timestamping (true/false) Per default timestamps are only updated on the a node that is changed, and not siblings that are moved/shifted due to changes on a given node. Usually the tree position of a document does not give information about changes to the content of the document. This behaviour can be changed through the option ‘:auto_tree_timestamping’.

Parameters:

  • opts (Hash)

    a hash of options



97
98
99
100
101
102
103
# File 'lib/mongoid/tree/rational_numbering.rb', line 97

def rational_number_options(opts)
  if !opts[:auto_tree_timestamping].nil?
    @auto_tree_timestamping = !!opts[:auto_tree_timestamping]
  else
    @auto_tree_timestamping = true
  end
end

#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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mongoid/tree/rational_numbering.rb', line 116

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