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, #_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(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 or there aren't shared sections
  • This means that it doesn't fix section weights on stages, as shared sections could change order in other stages

Creates a new section



25
26
27
28
29
30
31
32
# File 'lib/ecoportal/api/v2/page/sections.rb', line 25

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
    move(section, pos: pos, before: before, after: after)
    yield(section) if block_given?
  end
end

#any_shared?Boolean

Returns true if there is one or more sections shared, false otherwise.

Returns:

  • (Boolean)

    true if there is one or more sections shared, false otherwise



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

def any_shared?
  self.any? {|sec| sec.shared?}
end

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

Gets all the sections between sec1 and sec2



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ecoportal/api/v2/page/sections.rb', line 67

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, mild: false) ⇒ Array<Ecoportal::API::V2::Page::Section>

Parameters:

  • mild (Boolean) (defaults to: false)

    modifier to only compare alphabetic characters

Returns:



58
59
60
61
62
63
# File 'lib/ecoportal/api/v2/page/sections.rb', line 58

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

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



43
44
45
46
47
# File 'lib/ecoportal/api/v2/page/sections.rb', line 43

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

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



50
51
52
53
54
# File 'lib/ecoportal/api/v2/page/sections.rb', line 50

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

#move(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object

Moves an existing section



35
36
37
38
39
40
# File 'lib/ecoportal/api/v2/page/sections.rb', line 35

def move(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  if weight = scope_weight(section, pos: pos, before: before, after: after)
    section.weight = weight
  end
  fix_weights!
end

#oozeObject



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

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:



83
84
85
86
87
# File 'lib/ecoportal/api/v2/page/sections.rb', line 83

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:



90
91
92
# File 'lib/ecoportal/api/v2/page/sections.rb', line 90

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