Class: Ecoportal::API::V2::Page::Component

Inherits:
Common::Content::DoubleModel show all
Extended by:
Common::Content::StringDigest
Defined in:
lib/ecoportal/api/v2/page/component.rb,
lib/ecoportal/api/v2/page/component/law.rb,
lib/ecoportal/api/v2/page/component/file.rb,
lib/ecoportal/api/v2/page/component/image.rb,
lib/ecoportal/api/v2/page/component/action.rb,
lib/ecoportal/api/v2/page/component/geo_field.rb,
lib/ecoportal/api/v2/page/component/law_field.rb,
lib/ecoportal/api/v2/page/component/tag_field.rb,
lib/ecoportal/api/v2/page/component/date_field.rb,
lib/ecoportal/api/v2/page/component/gauge_stop.rb,
lib/ecoportal/api/v2/page/component/chart_field.rb,
lib/ecoportal/api/v2/page/component/files_field.rb,
lib/ecoportal/api/v2/page/component/gauge_field.rb,
lib/ecoportal/api/v2/page/component/action_field.rb,
lib/ecoportal/api/v2/page/component/images_field.rb,
lib/ecoportal/api/v2/page/component/number_field.rb,
lib/ecoportal/api/v2/page/component/people_field.rb,
lib/ecoportal/api/v2/page/component/chart_fr_field.rb,
lib/ecoportal/api/v2/page/component/checklist_item.rb,
lib/ecoportal/api/v2/page/component/checklist_field.rb,
lib/ecoportal/api/v2/page/component/geo_coordinates.rb,
lib/ecoportal/api/v2/page/component/reference_field.rb,
lib/ecoportal/api/v2/page/component/rich_text_field.rb,
lib/ecoportal/api/v2/page/component/selection_field.rb,
lib/ecoportal/api/v2/page/component/signature_field.rb,
lib/ecoportal/api/v2/page/component/plain_text_field.rb,
lib/ecoportal/api/v2/page/component/selection_option.rb,
lib/ecoportal/api/v2/page/component/chart_field/serie.rb,
lib/ecoportal/api/v2/page/component/selection_options.rb,
lib/ecoportal/api/v2/page/component/chart_field/config.rb,
lib/ecoportal/api/v2/page/component/chart_field/sankey.rb,
lib/ecoportal/api/v2/page/component/chart_field/heatmap.rb,
lib/ecoportal/api/v2/page/component/chart_field/benchmark.rb,
lib/ecoportal/api/v2/page/component/chart_field/frequency.rb,
lib/ecoportal/api/v2/page/component/chart_field/indicator.rb,
lib/ecoportal/api/v2/page/component/people_viewable_field.rb,
lib/ecoportal/api/v2/page/component/chart_field/multiseries.rb,
lib/ecoportal/api/v2/page/component/chart_field/series_config.rb,
lib/ecoportal/api/v2/page/component/contractor_entities_field.rb

Defined Under Namespace

Classes: Action, ActionField, ChartField, ChartFrField, ChecklistField, ChecklistItem, ContractorEntitiesField, DateField, File, FilesField, GaugeField, GaugeStop, GeoCoordinates, GeoField, Image, ImagesField, Law, LawField, NumberField, PeopleField, PeopleViewableField, PlainTextField, ReferenceField, RichTextField, SelectionField, SelectionOption, SelectionOptions, SignatureField, TagField

Constant Summary

Constants included from Common::Content::StringDigest

Common::Content::StringDigest::MAX_HASH_LABEL

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 included from Common::Content::StringDigest

hash_label

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

Class Method Details

.get_class(doc) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ecoportal/api/v2/page/component.rb', line 42

def get_class(doc)
  if doc.is_a?(Hash)
    case doc["type"]
    when "tag_field"
      tag_field_class
    when "geo"
      geo_field_class
    when "select"
      selection_field_class
    when "date"
      date_field_class
    when "number"
      number_field_class
    when "gauge"
      gauge_field_class
    when "plain_text"
      plain_text_field_class
    when "rich_text"
      rich_text_field_class
    when "people"
      people_field_class
    when "contractor_entities"
      contractor_entities_field_class
    when "checklist"
      checklist_field_class
    when "page_action","checklist_task"
      action_field_class
    when "file"
      files_field_class
    when "image_gallery"
      images_field_class
    when "signature"
      signature_field_class
    when "cross_reference"
      reference_field_class
    when "law"
      law_field_class
    when "chart"
      chart_field_class
    when "frequency_rate_chart"
      chart_fr_field_class
    else
      self
    end
  end
end

.new_doc(type: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ecoportal/api/v2/page/component.rb', line 29

def new_doc(type: nil)
  {
    "id"         => new_uuid
  }.tap do |base_doc|
    if type
      base_doc.merge!({"type" => type})
      if klass = get_class(base_doc)
        base_doc.merge!(klass.new_doc || {})
      end
    end
  end
end

Instance Method Details

#attached?Boolean

Returns whether or not this field is attached to any section.

Returns:

  • (Boolean)

    whether or not this field is attached to any section.



120
121
122
# File 'lib/ecoportal/api/v2/page/component.rb', line 120

def attached?
  !!section
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:



141
142
143
144
145
# File 'lib/ecoportal/api/v2/page/component.rb', line 141

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 component is bound to any force, false otherwise.

Returns:

  • (Boolean)

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



155
156
157
# File 'lib/ecoportal/api/v2/page/component.rb', line 155

def bindings?
  forces.count > 0
end

#configure(*conf) ⇒ Object

Quick config helper

Parameters:

  • conf (Symbol, Array<Symbol>)
    • :required
    • :hide_view to hide in view mode
    • :hide_mobile to hide in mobile app
    • :hide_reports to hide in reports
    • :global to define a global binding


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ecoportal/api/v2/page/component.rb', line 197

def configure(*conf)
  conf.each_with_object([]) do |cnf, unused|
    case cnf
    when :required
      self.required  = true
    when :hide_view
      self.hide_view = true
    when :hide_mobile
      self.hidden_on_mobile = true
    when :hide_reports
      self.hidden_on_reports = true
    when Hash
      if cnf.key?(:global)
        self.global_binding = cnf[:global]
      end
    else
      unused.push(cnf)
    end
  end.tap do |unused|
    raise "Unsupported configuration options '#{unused}' for #{self.class}" unless unused.empty?
  end
end

#delete!Object



175
176
177
178
179
# File 'lib/ecoportal/api/v2/page/component.rb', line 175

def delete!
  bindings.each {|b| b.delete!}
  self.unattach!
  self._parent.delete!(self)
end

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

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

Returns:



148
149
150
151
152
# File 'lib/ecoportal/api/v2/page/component.rb', line 148

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

#indexable_label(any_length: false) ⇒ Object



186
187
188
# File 'lib/ecoportal/api/v2/page/component.rb', line 186

def indexable_label(any_length: false)
  self.class.indexable_label(label, any_length: any_length)
end

#move(section:, before: nil, after: nil, side: nil) ⇒ Object



181
182
183
184
# File 'lib/ecoportal/api/v2/page/component.rb', line 181

def move(section:, before: nil, after: nil, side: nil)
  self.unattach!
  section.add_component(self, before: before, after: after, side: side)
end

#multi_section?Boolean

Returns whether or not the component has been attached to more than one section.

Returns:

  • (Boolean)

    whether or not the component has been attached to more than one section.



135
136
137
138
# File 'lib/ecoportal/api/v2/page/component.rb', line 135

def multi_section?
  secs = ooze.sections.select {|sec| sec.component?(id)}
  secs.length > 1
end

#oozeObject



99
100
101
# File 'lib/ecoportal/api/v2/page/component.rb', line 99

def ooze
  self._parent.ooze
end

#ref(any_length: false) ⇒ Object



107
108
109
110
111
# File 'lib/ecoportal/api/v2/page/component.rb', line 107

def ref(any_length: false)
  if digest = self.class.hash_label(label, any_length: any_length)
    [type, digest].join(".")
  end
end

#ref_backendObject



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

def ref_backend
  refs.first
end

#replace(fld) ⇒ Object

Note:

careful with this, depending what's done in the force, this may brake the force.

If the field has bindings they are replaced by this new field



161
162
163
164
165
166
167
168
169
# File 'lib/ecoportal/api/v2/page/component.rb', line 161

def replace(fld)
  if fld.section
    fld.move(section: self.section, before: self)
  else
    self.section.add(fld, before: self)
  end
  replace_bindings(fld)
  self.delete!
end

#replace_bindings(fld) ⇒ Object



171
172
173
# File 'lib/ecoportal/api/v2/page/component.rb', line 171

def replace_bindings(fld)
  bindings.map {|b| b.replace(fld)}
end

#sectionEcoportal::API::V2::Page::Section?

Looks up the section that this component belongs to.

Returns:



115
116
117
# File 'lib/ecoportal/api/v2/page/component.rb', line 115

def section
  ooze.sections.find {|sec| sec.component?(id)}
end

#unattach!Object

Note:

please make sure to reattach or delete! the field before save

Unattaches the field from any section



126
127
128
129
130
131
132
# File 'lib/ecoportal/api/v2/page/component.rb', line 126

def unattach!
  ooze.sections.select do |sec|
    sec.component?(id)
  end.each do |sec|
    sec.remove_component!(self)
  end
end