Module: BulkOps::InterpretRelationshipsBehavior

Extended by:
ActiveSupport::Concern
Included in:
Parser
Defined in:
lib/concerns/interpret_relationships_behavior.rb

Instance Method Summary collapse

Instance Method Details

#interpret_relationship_fieldsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
# File 'lib/concerns/interpret_relationships_behavior.rb', line 4

def interpret_relationship_fields
  @raw_row.each do |field,value|
    next if value.blank?  or field.blank? or value == field

    #the default identifier type is the reference identifier of the proxy
    id_type = reference_identifier

    # Correctly interpret the notation "parent:id", "parent id" etc in a column header
    if (split = field.split(/[:_\-\s]/)).count == 2
      id_type = split.last
      field = split.first
    end
    
    # skip to next field unless it's a known relationship field
    next unless (relationship_type = self.class.normalize_relationship_field_name(field))

    case relationship_type
    when "order"
      # If the field specifies the object's order among siblings 
      @proxy.update(order: value.to_f)
      next
    when "collection"
      # If the field specifies the name or ID of a collection,
      # find or create the collection and update the metadata to match
      col = find_or_create_collection(value)
      ( @metadata[:member_of_collection_ids] ||= [] ) << col.id if col
      next
    when "parent"
      # Correctly interpret the notation "row:349", "id:s8df4j32w" etc in a cell
      if (split = value.split(/[:_\\s]/)).count == 2
        id_type = split.first
        value = split.last
      end      
      parent = find_parent_proxy(value, field, id_type)
      proxy_updates =  { parent_id: parent.id}
      siblings = parent.ordered_children
      if siblings.present? && @proxy.previous_sibling_id.nil?
        proxy_updates[:previous_sibling_id] = siblings.last.id
      end
      @proxy.update(proxy_updates)  
    end
  end
end