Class: Ecoportal::API::V2::Page::Components

Inherits:
Common::Content::CollectionModel show all
Defined in:
lib/ecoportal/api/v2/page/components.rb

Constant Summary

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 Common::Content::CollectionModel

#initialize

Methods included from Common::Content::Includer

#include_missing

Methods inherited from Common::Content::DoubleModel

#initialize, new_uuid

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

#as_update, #dirty?

Constructor Details

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

Instance Method Details

#add(doc: nil, label: doc && doc['label'], type: doc && doc['type']) {|field| ... } ⇒ Ecoportal::API::V2::Page::Component

Note:
  • You can use either type and label or doc

It creates a new component

Parameters:

  • label (String, nil) (defaults to: doc && doc['label'])
  • type (String) (defaults to: doc && doc['type'])

    the type of the field

  • doc (Hash) (defaults to: nil)

    to copy another field model

Yields:

  • (field)

    do some stuff with field

Yield Parameters:

Returns:



53
54
55
56
57
58
59
# File 'lib/ecoportal/api/v2/page/components.rb', line 53

def add(doc: nil, label: doc && doc['label'], type: doc && doc['type'])
  fld_doc = doc ? JSON.parse(doc.to_json) : component_class.new_doc(type: type)
  upsert!(fld_doc) do |fld|
    fld.label = label unless doc
    yield(fld)        if block_given?
  end
end

#get_by_id(id) ⇒ Ecoportal::API::V2::Page::Component

Returns the field with id.

Returns:



21
22
23
24
25
# File 'lib/ecoportal/api/v2/page/components.rb', line 21

def get_by_id(id)
  find do |comp|
    comp.id == id
  end
end

#get_by_name(name, type: nil) ⇒ Array<Ecoportal::API::V2::Page::Component>

Returns the fields that match name.

Parameters:

  • name (String, Regexp)

    the name to search the field based on their label

  • type (String) (defaults to: nil)

    the type of the fields to be included

Returns:



37
38
39
40
41
42
# File 'lib/ecoportal/api/v2/page/components.rb', line 37

def get_by_name(name, type: nil)
  pool = type ? get_by_type(type) : self
  pool.select do |comp|
    same_string?(comp.label, name)
  end
end

#get_by_type(type) ⇒ Array<Ecoportal::API::V2::Page::Component>

Returns the fields of that type.

Returns:



28
29
30
31
32
# File 'lib/ecoportal/api/v2/page/components.rb', line 28

def get_by_type(type)
  select do |comp|
    comp.type.downcase == type.to_s.strip.downcase
  end
end

#multi_sectionArray<Ecoportal::API::V2::Page::Component>

Returns fields belonging to more than one section.

Returns:



67
68
69
# File 'lib/ecoportal/api/v2/page/components.rb', line 67

def multi_section
  select(&:multi_section?)
end

#oozeObject

self.order_matters = true



16
17
18
# File 'lib/ecoportal/api/v2/page/components.rb', line 16

def ooze
  _parent.ooze
end

#unattachedArray<Ecoportal::API::V2::Page::Component>

Returns orphaned fields (with no section).

Returns:



62
63
64
# File 'lib/ecoportal/api/v2/page/components.rb', line 62

def unattached
  reject(&:attached?)
end