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',
  'multiupload' => 'file'
}.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)


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

def autocomplete?
  type == 'autocomplete'
end

#conditionalsObject



85
86
87
88
89
# File 'app/models/metadata_presenter/component.rb', line 85

def conditionals
  Array(['conditionals']).map do ||
    MetadataPresenter::Conditional.new()
  end
end

#content?Boolean

Returns:

  • (Boolean)


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

def content?
  type == 'content'
end

#find_item_by_uuid(uuid) ⇒ Object



65
66
67
# File 'app/models/metadata_presenter/component.rb', line 65

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

#humanised_titleObject



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

def humanised_title
  label || legend
end

#item_klassObject



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

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

#itemsObject



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

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

#max_filesObject



81
82
83
# File 'app/models/metadata_presenter/component.rb', line 81

def max_files
  .max_files.presence || '0'
end

#multiupload?Boolean

Returns:

  • (Boolean)


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

def multiupload?
  type == 'multiupload'
end

#supported_validationsObject



69
70
71
72
73
74
75
# File 'app/models/metadata_presenter/component.rb', line 69

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)


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

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.



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

def to_json(*_args)
  return super unless autocomplete?

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

#to_partial_pathObject



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

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

#upload?Boolean

Returns:

  • (Boolean)


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

def upload?
  type == 'upload'
end

#validation_thresholdObject



77
78
79
# File 'app/models/metadata_presenter/component.rb', line 77

def validation_threshold
  VALIDATION_STRING_LENGTH_THRESHOLD
end