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/actions_field.rb,
lib/ecoportal/api/v2/page/component/mailbox_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, ActionsField, ChartField, ChartFrField, ChecklistField, ChecklistItem, ContractorEntitiesField, DateField, File, FilesField, GaugeField, GaugeStop, GeoCoordinates, GeoField, Image, ImagesField, Law, LawField, MailboxField, 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, 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

.get_class(doc) ⇒ Object

rubocop:disable Metrics/AbcSize



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
88
89
90
91
92
# File 'lib/ecoportal/api/v2/page/component.rb', line 44

def get_class(doc) # rubocop:disable Metrics/AbcSize
  return nil unless 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 "actions_list"
    actions_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 "mailbox"
    mailbox_field_class
  when "chart"
    chart_field_class
  when "frequency_rate_chart"
    chart_fr_field_class
  else
    self
  end
end

.new_doc(type: nil) ⇒ Object



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

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.



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

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:



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

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



161
162
163
# File 'lib/ecoportal/api/v2/page/component.rb', line 161

def bindings?
  forces.count&.positive?
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


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ecoportal/api/v2/page/component.rb', line 203

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
      self.global_binding = cnf[:global] if cnf.key?(:global)
    else
      unused.push(cnf)
    end
  end.tap do |unused|
    next if unused.empty?

    raise "Unsupported configuration options '#{unused}' for #{self.class}"
  end
end

#delete!Object



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

def delete!
  bindings.each(&:delete!)
  unattach!
  _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:



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

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

#indexable_label(any_length: false) ⇒ Object



192
193
194
# File 'lib/ecoportal/api/v2/page/component.rb', line 192

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



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

def move(section:, before: nil, after: nil, side: nil)
  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.



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

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

#oozeObject



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

def ooze
  _parent.ooze
end

#ref(any_length: false) ⇒ Object



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

def ref(any_length: false)
  digest = self.class.hash_label(label, any_length: any_length)
  return unless digest

  [type, digest].join(".")
end

#ref_backendObject



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

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



167
168
169
170
171
172
173
174
175
# File 'lib/ecoportal/api/v2/page/component.rb', line 167

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

#replace_bindings(fld) ⇒ Object



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

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:



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

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



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

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