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

+ (Object) included(base)



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

- (Object) add_children(children)



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
# 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

    node_model.create_from_json(obj)
  end
end