Class: MetadataPresenter::Component

Inherits:
Metadata
  • Object
show all
Includes:
Comparable
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

#<=>(other) ⇒ Object



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

def <=>(other)
  return nil unless other.is_a?(MetadataPresenter::Component)
  return 0 unless order && other.order

  order <=> other.order
end

#autocomplete?Boolean

Returns:

  • (Boolean)


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

def autocomplete?
  type == 'autocomplete'
end

#conditionalsObject



94
95
96
97
98
# File 'app/models/metadata_presenter/component.rb', line 94

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

#content?Boolean

Returns:

  • (Boolean)


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

def content?
  type == 'content'
end

#find_item_by_uuid(uuid) ⇒ Object



74
75
76
# File 'app/models/metadata_presenter/component.rb', line 74

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

#humanised_titleObject



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

def humanised_title
  label || legend
end

#item_klassObject



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

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

#itemsObject



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

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

#max_filesObject



90
91
92
# File 'app/models/metadata_presenter/component.rb', line 90

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

#multiupload?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/metadata_presenter/component.rb', line 70

def multiupload?
  type == 'multiupload'
end

#supported_validationsObject



78
79
80
81
82
83
84
# File 'app/models/metadata_presenter/component.rb', line 78

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)


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

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.



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

def to_json(*_args)
  return super unless autocomplete?

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

#to_partial_pathObject



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

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

#upload?Boolean

Returns:

  • (Boolean)


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

def upload?
  type == 'upload'
end

#validation_thresholdObject



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

def validation_threshold
  VALIDATION_STRING_LENGTH_THRESHOLD
end