Class: ChronicTree::Operation::ChangeParent

Inherits:
Object
  • Object
show all
Defined in:
lib/chronic_tree/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, new_parent_object) ⇒ ChangeParent

Returns a new instance of ChangeParent.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chronic_tree/operation.rb', line 9

def initialize(source, new_parent_object)
  @source = source

  @ready_to_move_elements = source.
    descendants_relation(source.current_time_at,
                         source.current_scope_name).map do |el|

    OpenStruct.new(child_id: el.child_id, distance: el.distance + 1)
  end
  @ready_to_move_elements << OpenStruct.new(child_id: source.id, distance: 1)

  @source_root_id = source.root.id

  @new_ancestors = new_parent_object.ancestors
  @new_ancestors.unshift(new_parent_object) unless new_parent_object.id == @source_root_id
end

Instance Attribute Details

#new_ancestorsObject (readonly)

Returns the value of attribute new_ancestors.



7
8
9
# File 'lib/chronic_tree/operation.rb', line 7

def new_ancestors
  @new_ancestors
end

#ready_to_move_elementsObject (readonly)

Returns the value of attribute ready_to_move_elements.



7
8
9
# File 'lib/chronic_tree/operation.rb', line 7

def ready_to_move_elements
  @ready_to_move_elements
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/chronic_tree/operation.rb', line 7

def source
  @source
end

#source_root_idObject (readonly)

Returns the value of attribute source_root_id.



7
8
9
# File 'lib/chronic_tree/operation.rb', line 7

def source_root_id
  @source_root_id
end

Instance Method Details

#actObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chronic_tree/operation.rb', line 26

def act
  ::ActiveRecord::Base.transaction do
    ChronicTree::Command::RemoveChildElementsFromAncestors.new(
      source,
      source.ancestors.map(&:id),
      ready_to_move_elements.map(&:child_id)).do

    ChronicTree::Command::AddChildElementsToNewAncestors.new(
      source,
      source_root_id,
      new_ancestors,
      ready_to_move_elements).do
  end

  source
end