Class: MetadataPresenter::Page

Inherits:
Metadata
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/metadata_presenter/page.rb

Constant Summary collapse

NOT_EDITABLE =
%i[
  _uuid
  _id
  _type
  add_component
  add_extra_component
].freeze
QUESTION_PAGES =
%w[page.singlequestion page.multiplequestions].freeze
USES_HEADING =
%w[
  page.content
  page.checkanswers
  page.confirmation
  page.multiplequestions
  page.exit
  save_and_return.resume_progress
].freeze
END_OF_ROUTE_PAGES =
%w[
  page.checkanswers
  page.confirmation
  page.exit
  save_and_return.resume_progress
].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?, #to_json, #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

#all_componentsObject



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

def all_components
  [components, extra_components].flatten.compact
end

#assign_autocomplete_items(items) ⇒ Object



118
119
120
121
122
123
# File 'app/models/metadata_presenter/page.rb', line 118

def assign_autocomplete_items(items)
  component_uuids = items.keys
  components.each do |component|
    component.items = items[component.uuid] if component.uuid.in?(component_uuids)
  end
end

#autocomplete_component_present?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/metadata_presenter/page.rb', line 114

def autocomplete_component_present?
  components.any?(&:autocomplete?)
end

#componentsObject



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

def components
  @components ||=
    to_components(.components, collection: :components)
end

#conditional_componentsObject



173
174
175
# File 'app/models/metadata_presenter/page.rb', line 173

def conditional_components
  conditionals_uuids_by_type('never', 'conditional')
end

#conditional_content_to_showObject



144
145
146
# File 'app/models/metadata_presenter/page.rb', line 144

def conditional_content_to_show
  @conditional_content_to_show || []
end

#conditionals_uuids_by_type(*display) ⇒ Object



165
166
167
# File 'app/models/metadata_presenter/page.rb', line 165

def conditionals_uuids_by_type(*display)
  content_components_by_type(*display).map { |component| component[:_uuid] }
end

#contains_placeholders?Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'app/models/metadata_presenter/page.rb', line 125

def contains_placeholders?
  return false if placeholders.empty?

  regex = /#{placeholders.map { |p| Regexp.escape(p) }.join('|')}/
  regex.match?(body)
end

#content_componentsObject



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

def content_components
  all_components.select(&:content?)
end

#content_components_by_type(*display) ⇒ Object



161
162
163
# File 'app/models/metadata_presenter/page.rb', line 161

def content_components_by_type(*display)
  content_components.filter { |component| display.include? component[:display] }
end

#editable_attributesObject



31
32
33
# File 'app/models/metadata_presenter/page.rb', line 31

def editable_attributes
  to_h.reject { |k, _| k.in?(NOT_EDITABLE) }
end

#end_of_route?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/metadata_presenter/page.rb', line 106

def end_of_route?
  type.in?(END_OF_ROUTE_PAGES)
end

#extra_componentsObject



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

def extra_components
  to_components(.extra_components, collection: :extra_components)
end

#find_component_by_uuid(uuid) ⇒ Object



35
36
37
# File 'app/models/metadata_presenter/page.rb', line 35

def find_component_by_uuid(uuid)
  all_components.find { |component| component.uuid == uuid }
end

#input_componentsObject



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

def input_components
  all_components.reject(&:content?)
end

#load_all_contentObject



169
170
171
# File 'app/models/metadata_presenter/page.rb', line 169

def load_all_content
  @conditional_content_to_show = content_components
end

#load_conditional_content(service, user_data) ⇒ Object



152
153
154
155
156
157
158
159
# File 'app/models/metadata_presenter/page.rb', line 152

def load_conditional_content(service, user_data)
  if content_component_present?
    evaluator = EvaluateContentConditionals.new(service:, user_data:)
    items = evaluator.evaluate_content_components(self)
    items << legacy_content_components
    assign_conditional_component(items.flatten)
  end
end

#multiple_questions?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/metadata_presenter/page.rb', line 110

def multiple_questions?
  type == 'page.multiplequestions'
end

#multiupload_componentsObject



88
89
90
# File 'app/models/metadata_presenter/page.rb', line 88

def multiupload_components
  components.select(&:multiupload?)
end

#placeholdersObject



132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/metadata_presenter/page.rb', line 132

def placeholders
  return [] unless standalone?

  key = id.gsub('page.', '')

  begin
    I18n.t("presenter.footer.#{key}.placeholders", raise: true)
  rescue I18n::MissingTranslationData
    []
  end
end

#question_page?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/models/metadata_presenter/page.rb', line 96

def question_page?
  type.in?(QUESTION_PAGES)
end

#show_conditional_component?(component_id) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/metadata_presenter/page.rb', line 148

def show_conditional_component?(component_id)
  conditional_content_to_show.include?(component_id)
end

#standalone?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/metadata_presenter/page.rb', line 92

def standalone?
  type == 'page.standalone'
end

#supported_components_by_type(type) ⇒ Object



60
61
62
63
64
65
66
# File 'app/models/metadata_presenter/page.rb', line 60

def supported_components_by_type(type)
  supported = supported_components(raw_type)[type]

  all_components.select do |component|
    supported.include?(component.type)
  end
end

#supported_content_componentsObject



80
81
82
# File 'app/models/metadata_presenter/page.rb', line 80

def supported_content_components
  supported_components(raw_type)[:content]
end

#supported_input_componentsObject



76
77
78
# File 'app/models/metadata_presenter/page.rb', line 76

def supported_input_components
  supported_components(raw_type)[:input]
end

#templateObject



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

def template
  "metadata_presenter/#{type.gsub('.', '/')}"
end

#titleObject



100
101
102
103
104
# File 'app/models/metadata_presenter/page.rb', line 100

def title
  return heading if heading?

  components.first.humanised_title
end

#to_partial_pathObject



68
69
70
# File 'app/models/metadata_presenter/page.rb', line 68

def to_partial_path
  type.gsub('.', '/')
end

#upload_componentsObject



84
85
86
# File 'app/models/metadata_presenter/page.rb', line 84

def upload_components
  components.select(&:upload?)
end