Class: InOrder::Create

Inherits:
Object
  • Object
show all
Includes:
Aux::CreateElement, Aux::VarKeys
Defined in:
app/models/in_order/create.rb

Overview

Creates a list of elements for a given Owner and/or Scope, which make up the key. This list links a number of arbitrary ActiveRecords together, retaining the given order (sequence). Owner and Record are any ActiveRecord model (linked polymorphically). The Scope is a string with an application-dependent value. At least one of Owner and Scope must be given. If a list of elememts with the same Owner/Scope combo already exists, then the new elements will be appended to them, unless the ‘append’ parameter is given as false, in which case, they’ll be prepended.

Instance Method Summary collapse

Methods included from Aux::VarKeys

included, #initialize

Instance Method Details

#call(models, append: true) ⇒ Object Also known as: elements

models are an Araay of ActiveRecord models to be linked



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/in_order/create.rb', line 19

def call(models, append: true)
  InOrder::Element.transaction do
    if append
      previous_last_element = InOrder::Element.last_element(keys)
    else
      previous_first_element = InOrder::Element.first_element(keys)
    end

    add_elements(models.dup).tap do |created_elements|
      if created_elements.any?
        if previous_first_element
          InOrder::Element.link_elements created_elements.last,
                                         previous_first_element
        elsif previous_last_element
          InOrder::Element.link_elements previous_last_element,
                                         created_elements.first
        end
      end
    end
  end
end

#subjects(*args) ⇒ Object Also known as: records



42
43
44
# File 'app/models/in_order/create.rb', line 42

def subjects(*args)
  call(*args).map &:subject
end