Module: Material

Defined in:
lib/material.rb

Overview

Methods for handling the “bulk” accession of collection objects

Defined Under Namespace

Classes: QuickVerbatimObject, QuickVerbatimResponse

Class Method Summary (collapse)

Class Method Details

+ (QuickVerbatimResponse instance) create_quick_verbatim(options = {})

Returns the data are not yet saved

Returns:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/material.rb', line 6

def self.create_quick_verbatim(options  = {}) 
  # We could refactor this to use nested attributes, but it's not that much cleaner 
  opts = {
    'collection_objects' => {},
    'note' => nil,
    'biocuration_classes' => [],
    'repository' => {'id' => nil},
    'collection_object' => {}
  }.merge(options)

  response = QuickVerbatimResponse.new(opts)

  objects = {}
  opts['collection_objects'].keys.each do |k|
    objects.merge!(k => opts['collection_objects'][k]) if !opts['collection_objects'][k]['total'].blank?
  end

  stub_object_attributes = CollectionObject::BiologicalCollectionObject.new(opts['collection_object'].merge('repository_id' => opts['repository']['id']))

  if opts['identifier'] && !opts['identifier']['namespace_id'].blank? && !opts['identifier']['identifier'].blank?
    identifier = Identifier::Local::CatalogNumber.new(
      namespace_id: opts['identifier']['namespace_id'],
      identifier: opts['identifier']['identifier']) 
  end

  container = Container::Virtual.new if objects.keys.count > 1 
  container.identifiers << identifier if container && identifier
  
  note = Note.new(opts['note']) if opts['note'] && !opts['note']['text'].blank? 
  repository = Repository.find(opts['repository']['id']) if opts['repository'] && !opts['repository']['id'].blank?

  objects.keys.each do |o|
    object = stub_object_attributes.dup
    object.total = objects[o]['total']

    if objects[o]['biocuration_classes'] 
      objects[o]['biocuration_classes'].keys.each do |k|
        object.biocuration_classifications.build(biocuration_class: BiocurationClass.find(k)) 
      end
    end

    object.notes << note.dup if note

    object.contained_in = container if container # = container if container
    object.identifiers << identifier if identifier && !container 
    response.collection_objects.push(object)
    object = nil
  end

  # Cache the values for next use !! test
  response.note = note if note
  response.identifier = identifier if identifier
  response.repository = repository if repository

  response 
end