Class: Ecoportal::API::V2::Page::Component::ReferenceField

Inherits:
Ecoportal::API::V2::Page::Component show all
Defined in:
lib/ecoportal/api/v2/page/component/reference_field.rb

Constant Summary

Constants included from Common::Content::StringDigest

Common::Content::StringDigest::MAX_HASH_LABEL

Constants included from Common::Content::DoubleModel::Diffable

Common::Content::DoubleModel::Diffable::DIFF_CLASS

Constants included from Common::Content::DoubleModel::Base

Common::Content::DoubleModel::Base::NOT_USED

Instance Attribute Summary

Attributes included from Common::Content::DoubleModel::Parented

#_parent, #_parent_key

Instance Method Summary collapse

Methods inherited from Ecoportal::API::V2::Page::Component

#attached?, #bindings, #bindings?, #delete!, #forces, get_class, #indexable_label, #move, #multi_section?, new_doc, #ooze, #ref, #ref_backend, #replace, #replace_bindings, #section, #unattach!

Methods included from Common::Content::StringDigest

#hash_label, #indexable_label

Methods inherited from Common::Content::DoubleModel

#initialize, new_uuid

Methods included from Common::Content::DoubleModel::Diffable

#as_update, #dirty?

Methods included from Common::Content::Includer

#include_missing

Constructor Details

This class inherits a constructor from Ecoportal::API::Common::Content::DoubleModel

Instance Method Details

#add(*ref_ids) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 23

def add(*ref_ids)
  doc['references'].tap do |refs|
    ref_ids.each do |ref_id|
      next if reference_ids.include?(ref_id)
      refs.push({
        'id'        => ref_id,
        'weight'    => 0,
        'patch_ver' => 0
      })
    end
  end
end

#clearObject



36
37
38
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 36

def clear
  delete(*reference_ids)
end

#configure(*conf) ⇒ Object

Quick config helper



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 69

def configure(*conf)
  conf.each_with_object([]) do |cnf, unused|
    case cnf
    when :show_fields
      self.display_fields = true
      self.display_fields_in_lookup = true
    when Hash
      supported = %i[create attach metadata]
      rest      = hash_except!(cnf.dup, *supported)

      unused.push(rest) unless rest.empty?

      self.hide_create   = !cnf[:create]   if cnf.key?(:create)
      self.hide_attach   = !cnf[:attach]   if cnf.key?(:attach)
      self. = !cnf[:metadata] if cnf.key?(:metadata)
    else
      unused.push(cnf)
    end
  end.then do |unused|
    super(*unused)
  end
end

#delete(*ref_ids) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 40

def delete(*ref_ids)
  ref_ids.each do |ref_id|
    doc_ref = doc['references'].find do |df|
      df['id'] == ref_id
    end

    next unless doc_ref

    ori_ref = original_doc['references'].find do |od|
      od['id'] == ref_id
    end

    # force patch version
    ori_ref['patch_ver'] ||= 0

    doc['references'].delete(doc_ref)
  end
end

#empty?Boolean



13
14
15
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 13

def empty?
  reference_ids.empty?
end

#reference_idsObject



17
18
19
20
21
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 17

def reference_ids
  references.map do |ref|
    ref['id']
  end
end

#to_s(delimiter: "\n") ⇒ Object



59
60
61
# File 'lib/ecoportal/api/v2/page/component/reference_field.rb', line 59

def to_s(delimiter: "\n")
  reference_ids.to_a.join(delimiter)
end