Class: Ecoportal::API::V2::Page::Sections
- Inherits:
-
Common::Content::CollectionModel
- Object
- Common::BaseModel
- Common::Content::DoubleModel
- Common::Content::CollectionModel
- Ecoportal::API::V2::Page::Sections
- 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
Instance Method Summary collapse
-
#add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
Creates a new
section. -
#any_shared? ⇒ Boolean
trueif there is one or more sections shared,falseotherwise. -
#between(sec1, sec2, included: false) ⇒ Array<Ecoportal::API::V2::Page::Section>
Gets all the sections between
sec1andsec2. - #get_by_heading(heading, mild: false) ⇒ Array<Ecoportal::API::V2::Page::Section>
- #get_by_id(id) ⇒ Ecoportal::API::V2::Page::Section
- #get_by_type(type) ⇒ Array<Ecoportal::API::V2::Page::Section>
-
#move(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object
Moves an existing
section. - #ooze ⇒ Object
-
#ordered ⇒ Array<Ecoportal::API::V2::Page::Section>
Gets the sections ordered by
weight(as they appear in the page). -
#unattached ⇒ Array<Ecoportal::API::V2::Page::Section>
Sections not attached to any stage.
Methods inherited from Common::Content::CollectionModel
#[], #_doc_key, #_doc_pos, #_items, #clear, #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, #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
- 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
24 25 26 27 28 29 30 31 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 24 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.
15 16 17 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 15 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
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 66 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>
57 58 59 60 61 62 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 57 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
42 43 44 45 46 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 42 def get_by_id(id) self.find do |sec| sec.id == id end end |
#get_by_type(type) ⇒ Array<Ecoportal::API::V2::Page::Section>
49 50 51 52 53 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 49 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
34 35 36 37 38 39 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 34 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 |
#ooze ⇒ Object
10 11 12 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 10 def ooze self._parent.ooze end |
#ordered ⇒ Array<Ecoportal::API::V2::Page::Section>
Gets the sections ordered by weight (as they appear in the page)
82 83 84 85 86 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 82 def ordered self.sort_by.with_index do |section, index| [section.weight, index] end end |
#unattached ⇒ Array<Ecoportal::API::V2::Page::Section>
Returns sections not attached to any stage.
89 90 91 |
# File 'lib/ecoportal/api/v2/page/sections.rb', line 89 def unattached select {|sec| !sec.attached?} end |