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, #_read_only

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ecoportal/api/v2/page/section.rb', line 182

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
    if before_fld = to_component(before, side: side)
      side ||= component_side(before_fld)
    end
  elsif after
    if after_fld  = to_component(after, side: side)
      side ||= component_side(after_fld)
    end
  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



81
82
83
84
# File 'lib/ecoportal/api/v2/page/section.rb', line 81

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



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

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

#bindingsArray<Ecoportal::API::V2::Page::Force::Binding] the force bindings if any.

Returns Array<Ecoportal::API::V2::Page::Force::Binding] the force bindings if any.

Returns:



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

def bindings
  ooze.forces.each_with_object([]) do |force, out|
    out.push(*force.bindings.get_by_reference(self))
  end
end

#bindings?Boolean

Returns true if the section is bound to any force, false otherwise.

Returns:

  • (Boolean)

    true if the section is bound to any force, false otherwise



66
67
68
# File 'lib/ecoportal/api/v2/page/section.rb', line 66

def bindings?
  force.count > 0
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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ecoportal/api/v2/page/section.rb', line 110

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

#component_side(com_or_id) ⇒ Object

@return[Symbol, Nil] might be :right, maybe :left, or nil



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ecoportal/api/v2/page/section.rb', line 123

def component_side(com_or_id)
  return nil unless split?
  case com_or_id
  when Ecoportal::API::V2::Page::Component
    component_side(com_or_id.id)
  when String
    if left_component_ids.include?(com_or_id)
      :left
    elsif right_component_ids.include?(com_or_id)
      :right
    end
  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.



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ecoportal/api/v2/page/section.rb', line 142

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

#forcesArray<Ecoportal::API::V2::Page::Force] the forces this section is bound to if any.

Returns Array<Ecoportal::API::V2::Page::Force] the forces this section is bound to if any.

Returns:



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

def forces
  ooze.forces.select do |force|
    force.bindings.reference?(self)
  end
end

#left_componentsArray<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



158
159
160
161
# File 'lib/ecoportal/api/v2/page/section.rb', line 158

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

#oozeObject



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

def ooze
  self._parent.ooze
end

#remove_component!(*comps_or_ids) ⇒ Object

Note:

when removing a component, please make sure to either add it to another section or to delete! it before save



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ecoportal/api/v2/page/section.rb', line 88

def remove_component!(*comps_or_ids)
  comps_or_ids.each do |com_or_id|
    case com_or_id
    when Ecoportal::API::V2::Page::Component
      remove_component!(com_or_id.id)
    when String
      if split?
        left_component_ids.delete!(com_or_id)
        right_component_ids.delete!(com_or_id)
      else
        component_ids.delete!(com_or_id)
      end
    else
      msg = "Missuse: com_or_id should be a Component or a String. Given: #{com_or_id.class}"
      raise ArgumentError.new(msg)
    end
  end
end

#right_componentsArray<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



166
167
168
169
# File 'lib/ecoportal/api/v2/page/section.rb', line 166

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

#shared?Boolean

Returns true if the section belongs to more than 1 stage, false otherwise.

Returns:

  • (Boolean)

    true if the section belongs to more than 1 stage, false otherwise



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

def shared?
  stages.count > 1
end

#split?Boolean

Returns whether or not this section is a split section.

Returns:

  • (Boolean)

    whether or not this section is a split section



76
77
78
# File 'lib/ecoportal/api/v2/page/section.rb', line 76

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