Class: Ecoportal::API::V2::Page::Sections

Inherits:
Common::Content::CollectionModel show all
Defined in:
lib/ecoportal/api/v2/page/sections.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

Instance Method Summary collapse

Methods inherited from Common::Content::CollectionModel

#[], #_doc_key, #_doc_pos, #_items, #delete!, doc_class, #each, #empty?, #include?, #initialize, #items_class, items_key, items_key=, klass?, #length, new_item, #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, #replace_doc, #reset!, #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, #used_param?

Constructor Details

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

Instance Method Details

#add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object

Note:
  • It won't fix weights unless all the sections of the ooze are present
  • This means that it doesn't fix section weights on stages, as shared sections could change order in other stages

Creates a new section



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ecoportal/api/v2/page/sections.rb', line 19

def add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  sec_doc = section_class.new_doc(split: split)
  upsert!(sec_doc) do |section| #, pos: pos, before: before, after: after) do |section|
    section.heading  = name
    if weight = scope_weight(section, pos: pos, before: before, after: after)
      section.weight = weight
    end
    fix_weights!
    yield(section) if block_given?
  end
end

#between(sec1, sec2, included: false) ⇒ Array<Ecoportal::API::V2::Page::Section>

Gets all the sections between sec1 and sec2



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ecoportal/api/v2/page/sections.rb', line 55

def between(sec1, sec2, included: false)
  sorted_secs = ordered
  pos1 = (sec1 = to_section(sec1)) && sorted_secs.index(sec1)
  pos2 = (sec2 = to_section(sec2)) && sorted_secs.index(sec2)
  if included
    pos1 = pos1 ? pos1 : 0
    pos2 = pos2 ? pos2 : -1
  else
    pos1 = pos1 ? (pos1 + 1) : 0
    pos2 = pos2 ? (pos2 - 1) : -1
  end
  sorted_secs[pos1..pos2]
end

#get_by_heading(heading) ⇒ Array<Ecoportal::API::V2::Page::Section>



46
47
48
49
50
51
# File 'lib/ecoportal/api/v2/page/sections.rb', line 46

def get_by_heading(heading)
  ordered.select do |sec|
    value = heading == :unnamed ? nil : heading
    same_string?(sec.heading, value)
  end
end

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



32
33
34
35
36
# File 'lib/ecoportal/api/v2/page/sections.rb', line 32

def get_by_id(id)
  self.find do |sec|
    sec.id == id
  end
end

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



39
40
41
42
43
# File 'lib/ecoportal/api/v2/page/sections.rb', line 39

def get_by_type(type)
  ordered.select do |sec|
    sec.type == type
  end
end

#oozeObject



10
11
12
# File 'lib/ecoportal/api/v2/page/sections.rb', line 10

def ooze
  self._parent.ooze
end

#orderedArray<Ecoportal::API::V2::Page::Section>

Gets the sections ordered by weight (as they appear in the page)

Returns:



71
72
73
74
75
# File 'lib/ecoportal/api/v2/page/sections.rb', line 71

def ordered
  self.sort_by.with_index do |section, index|
    [section.weight, index]
  end
end

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

Returns sections not attached to any stage.

Returns:



78
79
80
# File 'lib/ecoportal/api/v2/page/sections.rb', line 78

def unattached
  select {|sec| !sec.attached?}
end