Class: InOrder::Insert

Inherits:
Aux::PositionBase show all
Extended by:
Aux::CreateElement, Aux::GetElement
Defined in:
app/models/in_order/insert.rb

Overview

Puts a new element anywhere in a pre-exising list.

Instance Attribute Summary

Attributes inherited from Aux::PositionBase

#adjacency, #marker, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Aux::PositionBase

#initialize

Constructor Details

This class inherits a constructor from InOrder::Aux::PositionBase

Class Method Details

.call(record, marker, adjacency, keys: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/in_order/insert.rb', line 27

def self.call(record, marker, adjacency, keys: nil)
  keys = InOrder::Aux::Keys.new(*keys) if Array === keys

  unless ActiveRecord::Base === record
    record = InOrder::Aux::PolyFind.new(record).call
  end

  marker = get_element(marker)

  element = create_element(record, keys || marker.to_keys)

  new(element, marker, adjacency).call
end

Instance Method Details

#callObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/in_order/insert.rb', line 8

def call
  InOrder::Element.transaction do
    if marker
      if after?
        target.update element_id: marker.element_id

        marker.update element_id: target.id
      else
        if previous = InOrder::Element.find_by(element_id: marker.id)
          previous.update element_id: target.id
        end

        target.update element_id: marker.id
      end
    end
    target
  end
end