Class: Ecoportal::API::V2::Page::Section

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

Constant Summary collapse

INITIAL_WEIGHT =
9999

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

Class Method Summary collapse

Instance Method Summary collapse

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::DoubleModel

Class Method Details

.new_doc(split: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ecoportal/api/v2/page/section.rb', line 9

def new_doc(split: false)
  {
    "id"         => new_uuid,
    "type"       => split ? "split" : "content",
    "weight"     => INITIAL_WEIGHT
  }.tap do |out|
    component_ids = if split
        {
          "left_component_ids" => [],
          "right_component_ids" => []
        }
      else
        {
          "component_ids" => []
        }
      end
    out.merge!(component_ids)
  end
end

Instance Method Details

#add_component(field, before: nil, after: nil, side: nil) ⇒ Object

Note:
  • To the specified side, when split section (default :left)
  • To the end if after is not specified
  • If after is specified, it searches field
    • On the specific side, if specified (and split section)
    • And adds the field after it, when found, or at the end if after is not found

Adds field to the section

Parameters:

Raises:

  • (ArgumentError)

    if field is not a Component

  • (Exception)

    if the field does not belong to ooze.components

  • (Exception)

    if the field belongs to another section



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ecoportal/api/v2/page/section.rb', line 119

def add_component(field, before: nil, after: nil, side: nil)
  unless field.is_a?(Ecoportal::API::V2::Page::Component)
    msg = "Expected Ecoportal::API::V2::Page::Component. Given: #{field.class}"
    raise ArgumentError.new(msg)
  end
  unless ooze.components.include?(field)
    msg  = "The field '#{field.label}' (#{field.id}) is not present in ooze.components.\n"
    msg += "Review your script (i.e. @var where you store previous ooze runs)."
    raise msg
  end
  # IMPORTANT NOTE: The code below creates objects, because field.section does a search on section.component_ids
  if field.section == self
    puts "Field with id '#{field.id}' already belongs to this section"
  elsif sec = field.section
    # Field belongs to another section
    raise "Field with id '#{field.id}' belongs to section '#{sec.heading || "Unnamed"}' (id: '#{sec.id}')"
  end

  if before
    before_fld = to_component(before, side: side)
  elsif after
    after_fld  = to_component(after, side: side)
  end

  if split?
    ids_ary = side == :right ? right_component_ids : left_component_ids
  else
    ids_ary = component_ids
  end
  ids_ary.insert_one(field.id, before: before_fld&.id, after: after_fld&.id)
  self
end

#all_component_idsArray<String>

Returns all the ids of the components/fields in this section.

Returns:

  • (Array<String>)

    all the ids of the components/fields in this section



57
58
59
60
# File 'lib/ecoportal/api/v2/page/section.rb', line 57

def all_component_ids
  return component_ids.to_a unless split?
  left_component_ids.to_a | right_component_ids.to_a
end

#attached?Boolean

Returns whether or not the section appears in an ooze instance.

Returns:

  • (Boolean)

    whether or not the section appears in an ooze instance



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

def attached?
  !ooze.stages? || stages.any? {|stg| stg.section?(self)}
end

#component?(com_or_id) ⇒ Boolean

Returns whether or not a component/field belongs to this section.

Parameters:

Returns:

  • (Boolean)

    whether or not a component/field belongs to this section.

Raises:

  • (ArgumentError)

    if com_or_id is not of any of the allowed types



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ecoportal/api/v2/page/section.rb', line 65

def component?(com_or_id)
  case com_or_id
  when Ecoportal::API::V2::Page::Component
    component?(com_or_id.id)
  when String
    all_component_ids.include?(com_or_id)
  else
    msg = "Missuse: com_or_id should be a Component or a String. Given: #{com_or_id.class}"
    raise ArgumentError.new(msg)
  end
end

#components(side: nil) ⇒ Array<Ecoportal::API::V2::Page::Component>

It looks up the components that belong to this section.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ecoportal/api/v2/page/section.rb', line 79

def components(side: nil)
  case side
  when :right
    components_right
  when :left
    components_left
  when NilClass
    components_by_id(*all_component_ids)
  else
    raise "Side should be one of [nil, :right, :left]. Given: #{side}"
  end
end

#components_leftArray<Ecoportal::API::V2::Page::Component>

It looks up the components that belong to the left side of this section.

Returns:

Raises:

  • (Exception)

    if this is not a split section



95
96
97
98
# File 'lib/ecoportal/api/v2/page/section.rb', line 95

def components_left
  raise "You are trying to retrieve side components in a Split Section" unless split?
  components_by_id(*left_component_ids)
end

#components_rightArray<Ecoportal::API::V2::Page::Component>

It looks up the components that belong to the right side of this section.

Returns:

Raises:

  • (Exception)

    if this is not a split section



103
104
105
106
# File 'lib/ecoportal/api/v2/page/section.rb', line 103

def components_right
  raise "You are trying to retrieve side components in a Split Section" unless split?
  components_by_id(*right_component_ids)
end

#oozeObject



37
38
39
# File 'lib/ecoportal/api/v2/page/section.rb', line 37

def ooze
  self._parent.ooze
end

#split?Boolean

Returns whether or not this section is a split section.

Returns:

  • (Boolean)

    whether or not this section is a split section



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

def split?
  doc && doc["type"] == "split"
end

#stagesArray<Ecoportal::API::V2::Page::Stage>

Returns the stage(s) this section belongs to.

Returns:



42
43
44
# File 'lib/ecoportal/api/v2/page/section.rb', line 42

def stages
  ooze.stages.select {|stg| stg.section?(self)}
end