Module: MetadataPresenter::ApplicationHelper

Defined in:
app/helpers/metadata_presenter/application_helper.rb

Constant Summary collapse

OLD_BODY_DEFAULT_CONTENT =
'Body section'.freeze

Instance Method Summary collapse

Instance Method Details

#default_item_title(component_type) ⇒ Object



40
41
42
43
44
# File 'app/helpers/metadata_presenter/application_helper.rb', line 40

def default_item_title(component_type)
  return unless %w[checkboxes radios].include?(component_type)

  MetadataPresenter::DefaultMetadata["component.#{component_type}"]['items']&.first&.[]('label')
end

#default_page_title(type) ⇒ Object



46
47
48
# File 'app/helpers/metadata_presenter/application_helper.rb', line 46

def default_page_title(type)
  MetadataPresenter::DefaultMetadata[type.to_s]&.[]('heading')
end

#default_text(property) ⇒ Object



22
23
24
# File 'app/helpers/metadata_presenter/application_helper.rb', line 22

def default_text(property)
  MetadataPresenter::DefaultText[property]
end

#default_title(component_type) ⇒ Object



35
36
37
38
# File 'app/helpers/metadata_presenter/application_helper.rb', line 35

def default_title(component_type)
  MetadataPresenter::DefaultMetadata["component.#{component_type}"]&.[]('label') ||
    MetadataPresenter::DefaultMetadata["component.#{component_type}"]&.[]('legend')
end

#files_to_renderObject



96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/metadata_presenter/application_helper.rb', line 96

def files_to_render
  component = page_multiupload_component

  error_file = @page_answers.uploaded_files.select { |file| file.errors.any? }.first

  if error_file.present?
    @page_answers.send(component.id)[component.id].compact.reject { |file| file[error_file.file['original_filename'] == 'original_filename'] }
  else
    @page_answers.send(component.id)[component.id].compact
  end
end

#main_title(component:, tag: :h1, classes: 'govuk-heading-xl') ⇒ Object



7
8
9
10
11
# File 'app/helpers/metadata_presenter/application_helper.rb', line 7

def main_title(component:, tag: :h1, classes: 'govuk-heading-xl')
  (tag, class: classes) do
    component.humanised_title
  end
end

#multiupload_files_remainingObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/metadata_presenter/application_helper.rb', line 56

def multiupload_files_remaining
  component = page_multiupload_component
  answers = @user_data.keys.include?(component.id) ? @user_data.find(component.id).first : []
  max_files = component.validation['max_files'].to_i

  if uploads_remaining.zero?
    I18n.t('presenter.questions.multiupload.none')
  elsif max_files == 1
    I18n.t('presenter.questions.multiupload.single_upload')
  elsif uploads_remaining == 1
    if answers.present?
      I18n.t('presenter.questions.multiupload.answered_singular')
    else
      I18n.t('presenter.questions.multiupload.singular')
    end
  elsif answers.present?
    I18n.t('presenter.questions.multiupload.answered_plural', num: uploads_remaining)
  else
    I18n.t('presenter.questions.multiupload.plural', num: uploads_remaining)
  end
end

#page_body_contentObject

we used to have some placeholder text in the body key, which was always replaced we since exposed the body key in an optional content area for certain pages, so forms created before that need to have their old default content ignored



29
30
31
32
33
# File 'app/helpers/metadata_presenter/application_helper.rb', line 29

def page_body_content
  if @page.body
    @page.body == OLD_BODY_DEFAULT_CONTENT ? '' : @page.body
  end
end

#page_multiupload_componentObject



108
109
110
# File 'app/helpers/metadata_presenter/application_helper.rb', line 108

def page_multiupload_component
  @page.components.select { |c| c.type == 'multiupload' }.first
end

#timeout_fallback(time) ⇒ Object



50
51
52
53
54
# File 'app/helpers/metadata_presenter/application_helper.rb', line 50

def timeout_fallback(time)
  time.strftime('%l:%M %p')
rescue NoMethodError
  time
end

#to_html(text) ⇒ Object

Renders html given markdown.

Examples:

<%=to_html '# Some markdown' %>


18
19
20
# File 'app/helpers/metadata_presenter/application_helper.rb', line 18

def to_html(text)
  Govspeak::Document.new(text).to_html.html_safe
end

#uploads_countObject



87
88
89
90
91
92
93
94
# File 'app/helpers/metadata_presenter/application_helper.rb', line 87

def uploads_count
  component = page_multiupload_component
  answers = @user_data.keys.include?(component.id) ? @user_data[component.id] : []

  return 0 if answers.is_a?(ActionDispatch::Http::UploadedFile)

  answers.count == 1 ? I18n.t('presenter.questions.multiupload.answered_count_singular') : I18n.t('presenter.questions.multiupload.answered_count_plural', num: answers.count)
end

#uploads_remainingObject



78
79
80
81
82
83
84
85
# File 'app/helpers/metadata_presenter/application_helper.rb', line 78

def uploads_remaining
  component = page_multiupload_component
  max_files = component.validation['max_files'].to_i
  answers = @user_data.keys.include?(component.id) ? @user_data[component.id] : []
  return 0 if answers.is_a?(ActionDispatch::Http::UploadedFile)

  max_files - answers.count
end