Module: ComponentsAddChildren

Included in:
ArchivalObject, DigitalObject, DigitalObjectComponent, Resource
Defined in:
backend/app/model/mixins/components_add_children.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
# File 'backend/app/model/mixins/components_add_children.rb', line 5

def self.included(base)
  if not base.included_modules.include?(TreeNodes)
    base.extend(ClassMethods)
  end
end

Instance Method Details

#add_children(children) ⇒ Object



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
# File 'backend/app/model/mixins/components_add_children.rb', line 11

def add_children(children)
  children.children.each do |child|
    obj = JSONModel(self.class.node_record_type.intern).from_hash(child)

    root_model = Kernel.const_get(self.class.root_record_type.camelize)
    node_model = Kernel.const_get(self.class.node_record_type.camelize)


    if root_model == self.class
      obj[self.class.root_record_type] = {
        'ref' => self.uri
      }
    else
      obj[self.class.root_record_type] = {
        'ref' => self.class.uri_for(self.class.root_record_type, self[:root_record_id])
      }

      obj['parent'] = {
        'ref' => self.uri
      }
    end

    begin
      node_model.create_from_json(obj)
    rescue Sequel::ValidationFailed => e
      # We've run into something that the DB doesnt like.
      # since we are dealing with a batch, we can add the
      # offending value to the error message in an attempt
      # to enlighten our  user
      e.errors.keys.each do |key|
        next unless obj[key]
        e.errors[key].map! { |msg| msg << " ( #{key}: #{obj[key]} )"}
      end
      raise e
    end
  end
end