Class: SimpleNestedSet::Move::ByAttributes

Inherits:
Object
  • Object
show all
Includes:
Protection
Defined in:
lib/simple_nested_set/move/by_attributes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protection

#impossible_move!, #inconsistent_move!, #protect_impossible_move!, #protect_inconsistent_move!

Constructor Details

#initialize(node, attributes) ⇒ ByAttributes

Returns a new instance of ByAttributes.



19
20
21
22
23
# File 'lib/simple_nested_set/move/by_attributes.rb', line 19

def initialize(node, attributes)
  @node, @attributes = node, attributes
  normalize_attributes!
  protect_inconsistent_move!
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



14
15
16
# File 'lib/simple_nested_set/move/by_attributes.rb', line 14

def attributes
  @attributes
end

#nodeObject (readonly)

Returns the value of attribute node.



14
15
16
# File 'lib/simple_nested_set/move/by_attributes.rb', line 14

def node
  @node
end

Class Method Details

.attribute_reader(*names) ⇒ Object



5
6
7
8
9
# File 'lib/simple_nested_set/move/by_attributes.rb', line 5

def attribute_reader(*names)
  names.each do |name|
    define_method(name) { attributes[name].blank? ? nil : attributes[name].to_i }
  end
end

Instance Method Details

#performObject



25
26
27
28
29
30
31
32
33
# File 'lib/simple_nested_set/move/by_attributes.rb', line 25

def perform
  if left_id && left_id != node.id
    node.move_to_right_of(left_id)
  elsif right_id && right_id != node.id
    node.move_to_left_of(right_id)
  elsif parent_id != node.parent_id
    node.move_to_child_of(parent_id)
  end
end