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

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

Constant Summary

Constants inherited from Common::Content::DoubleModel

Common::Content::DoubleModel::NOT_USED

Constants included from Common::Content::ClassHelpers

Common::Content::ClassHelpers::NOT_USED

Instance Attribute Summary

Attributes inherited from Common::Content::DoubleModel

#_key, #_parent, #_read_only

Instance Method Summary collapse

Methods inherited from Common::Content::CollectionModel

#[], #_doc_key, #_doc_pos, #_items, #clear, #delete!, #each, #empty?, #include?, #initialize, #items_class, items_key, items_key=, klass?, #length, new_item, new_item_class_based?, #present?, #upsert!, #values_at

Methods inherited from Common::Content::DoubleModel

#_doc_key, #as_json, #as_update, #consolidate!, #dirty?, #doc, embeds_many, embeds_one, enforce!, #initialize, #key, #key=, key?, new_uuid, #original_doc, pass_reader, pass_writer, passarray, passboolean, passdate, passforced, passkey, passthrough, #print_pretty, read_only!, read_only?, #read_only?, #replace_doc, #reset!, #resolved_doc_key, #root, #to_json

Methods included from Common::Content::ClassHelpers

#inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #uid, #used_param?

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