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

Inherits:
Common::Content::CollectionModel show all
Defined in:
lib/ecoportal/api/v2/page/sections.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(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
33
34
35
# 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?
  any?(&:shared?)
end

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

Gets all the sections between sec1 and sec2



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

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:



61
62
63
64
65
66
# File 'lib/ecoportal/api/v2/page/sections.rb', line 61

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



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

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

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



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

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



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

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
  _parent.ooze
end

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

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

Returns:



86
87
88
89
90
# File 'lib/ecoportal/api/v2/page/sections.rb', line 86

def ordered
  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:



93
94
95
# File 'lib/ecoportal/api/v2/page/sections.rb', line 93

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