Module: ClosureTree::DeterministicNumericOrdering

Defined in:
lib/closure_tree/acts_as_tree.rb

Overview

This module is only included if the order column is an integer.

Instance Method Summary collapse

Instance Method Details

#add_sibling(sibling_node, use_update_all = true, add_after = true) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/closure_tree/acts_as_tree.rb', line 475

def add_sibling(sibling_node, use_update_all = true, add_after = true)
  fail "can't add self as sibling" if self == sibling_node
  # issue 18: we need to set the order_value explicitly so subsequent orders will work.
  update_attribute(:order_value, 0) if self.order_value.nil?
  sibling_node.order_value = self.order_value.to_i + (add_after ? 1 : -1)
  # We need to incr the before_siblings to make room for sibling_node:
  if use_update_all
    col = quoted_order_column(false)
    ct_class.update_all(
      ["#{col} = #{col} #{add_after ? '+' : '-'} 1", "updated_at = now()"],
        ["#{quoted_parent_column_name} = ? AND #{col} #{add_after ? '>=' : '<='} ?",
          ct_parent_id,
          sibling_node.order_value])
  else
    last_value = sibling_node.order_value.to_i
    (add_after ? siblings_after : siblings_before.reverse).each do |ea|
      last_value += (add_after ? 1 : -1)
      ea.order_value = last_value
      ea.save!
    end
  end
  sibling_node.parent = self.parent
  sibling_node.save!
  sibling_node.reload
end

#append_sibling(sibling_node, use_update_all = true) ⇒ Object



467
468
469
# File 'lib/closure_tree/acts_as_tree.rb', line 467

def append_sibling(sibling_node, use_update_all = true)
  add_sibling(sibling_node, use_update_all, true)
end

#prepend_sibling(sibling_node, use_update_all = true) ⇒ Object



471
472
473
# File 'lib/closure_tree/acts_as_tree.rb', line 471

def prepend_sibling(sibling_node, use_update_all = true)
  add_sibling(sibling_node, use_update_all, false)
end