Class: MetadataPresenter::Component

Inherits:
Metadata
  • Object
show all
Defined in:
app/models/metadata_presenter/component.rb

Constant Summary collapse

VALIDATION_BUNDLES =
{
  'date' => 'date',
  'number' => 'number',
  'text' => 'string',
  'textarea' => 'string'
}.freeze
VALIDATION_STRING_LENGTH_THRESHOLD =

Used for max_length and max_word validations. Threshold percentage at which the remaining count is shown

75
SUPPORTS_BRANCHING =
%w[radios checkboxes].freeze

Instance Attribute Summary

Attributes inherited from Metadata

#metadata

Instance Method Summary collapse

Methods inherited from Metadata

#==, #editor?, #id, #initialize, #method_missing, #respond_to_missing?, #type, #uuid

Constructor Details

This class inherits a constructor from MetadataPresenter::Metadata

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MetadataPresenter::Metadata

Instance Method Details

#autocomplete?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/metadata_presenter/component.rb', line 48

def autocomplete?
  type == 'autocomplete'
end

#content?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/metadata_presenter/component.rb', line 52

def content?
  type == 'content'
end

#find_item_by_uuid(uuid) ⇒ Object



60
61
62
# File 'app/models/metadata_presenter/component.rb', line 60

def find_item_by_uuid(uuid)
  items.find { |item| item.uuid == uuid }
end

#humanised_titleObject



28
29
30
# File 'app/models/metadata_presenter/component.rb', line 28

def humanised_title
  label || legend
end

#item_klassObject



38
39
40
# File 'app/models/metadata_presenter/component.rb', line 38

def item_klass
  autocomplete? ? MetadataPresenter::AutocompleteItem : MetadataPresenter::Item
end

#itemsObject



32
33
34
35
36
# File 'app/models/metadata_presenter/component.rb', line 32

def items
  Array(.items).map do |item|
    item_klass.new(item, editor: editor?)
  end
end

#supported_validationsObject



64
65
66
67
68
69
70
# File 'app/models/metadata_presenter/component.rb', line 64

def supported_validations
  return [] if validation_bundle_key.nil?

  JSON::Validator.schema_for_uri(validation_bundle_key)
                 .schema['properties']['validation']['properties']
                 .keys
end

#supports_branching?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/metadata_presenter/component.rb', line 44

def supports_branching?
  type.in?(SUPPORTS_BRANCHING)
end

#to_json(*_args) ⇒ Object

Overriding here because autocomplete component’s items property is non interactable in the Editor therefore it does not need to exist in the data-fb-content-data attribute on the page. The data-fb-content-data attribute is what is used to update user interactable elements such as the component question or page section heading etc.



18
19
20
21
22
# File 'app/models/metadata_presenter/component.rb', line 18

def to_json(*_args)
  return super unless autocomplete?

  JSON.parse(super).to_json(except: 'items')
end

#to_partial_pathObject



24
25
26
# File 'app/models/metadata_presenter/component.rb', line 24

def to_partial_path
  "metadata_presenter/component/#{type}"
end

#upload?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/metadata_presenter/component.rb', line 56

def upload?
  type == 'upload'
end

#validation_thresholdObject



72
73
74
# File 'app/models/metadata_presenter/component.rb', line 72

def validation_threshold
  VALIDATION_STRING_LENGTH_THRESHOLD
end