Module: SimpleNestedSet::Move::Protection

Included in:
ByAttributes, ToTarget
Defined in:
lib/simple_nested_set/move/protection.rb

Instance Method Summary collapse

Instance Method Details

#impossible_move!(message) ⇒ Object

Raises:



49
50
51
# File 'lib/simple_nested_set/move/protection.rb', line 49

def impossible_move!(message)
  raise Impossible, "Impossible move: #{message.split("\n").map! { |line| line.strip }.join}"
end

#inconsistent_move!(message) ⇒ Object

Raises:



45
46
47
# File 'lib/simple_nested_set/move/protection.rb', line 45

def inconsistent_move!(message)
  raise Inconsistent, "Impossible move: #{message.split("\n").map! { |line| line.strip }.join}"
end

#protect_impossible_move!Object



7
8
9
10
11
12
13
14
15
# File 'lib/simple_nested_set/move/protection.rb', line 7

def protect_impossible_move!
  positions = [:child, :left, :right, :root]
  impossible_move!("A new node can not be moved") if node.new_record?
  impossible_move!("Position must be one of #{positions.inspect} but is #{position.inspect}.") unless positions.include?(position)
  impossible_move!("A new node can not be moved") if node.new_record?
  impossible_move!("A node can't be moved to itself") if node == target
  impossible_move!("A node can't be moved to a descendant of itself.") if target && (node.lft..node.rgt).include?(target.lft) && (node.lft..node.rgt).include?(target.rgt)
  impossible_move!("A node can't be moved to a different scope") if target && !nested_set.same_scope?(target)
end

#protect_inconsistent_move!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simple_nested_set/move/protection.rb', line 17

def protect_inconsistent_move!
  left  = nested_set.find(left_id)  if left_id
  right = nested_set.find(right_id) if right_id
  p_id  = ( parent_id.to_s =~ /^\d+/ && parent_id.to_s.to_i )

  if left && right && (!left.right_sibling || left.right_sibling.id != right_id)
    inconsistent_move! <<-msg
      Both :left_id (#{left_id.inspect}) and :right_id (#{right_id.inspect}) were given but
      :right_id (#{right_id}) does not refer to the right_sibling (#{left.right_sibling.inspect})
      of the node referenced by :left_id (#{left.inspect})
    msg
  end

  if left && p_id && left.parent_id != p_id
    inconsistent_move! <<-msg
      Both :left_id (#{left_id.inspect}) and :parent_id (#{p_id.inspect}) were given but
      left.parent_id (#{left.parent_id.inspect}) does not equal parent_id
    msg
  end

  if right && p_id && right.parent_id != p_id
    inconsistent_move! <<-msg
      Both :right_id (#{right_id.inspect}) and :parent_id (#{p_id.inspect}) were given but
      right.parent_id (#{right.parent_id.inspect}) does not equal parent_id
    msg
  end
end