Module: MetadataPresenter::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

#default_item_title(component_type) ⇒ Object



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

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

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

#default_page_title(type) ⇒ Object



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

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

#default_text(property) ⇒ Object



20
21
22
# File 'app/helpers/metadata_presenter/application_helper.rb', line 20

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

#default_title(component_type) ⇒ Object



24
25
26
27
# File 'app/helpers/metadata_presenter/application_helper.rb', line 24

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

#files_to_renderObject



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

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



5
6
7
8
9
# File 'app/helpers/metadata_presenter/application_helper.rb', line 5

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

#multiupload_files_remainingObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/metadata_presenter/application_helper.rb', line 45

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_multiupload_componentObject



97
98
99
# File 'app/helpers/metadata_presenter/application_helper.rb', line 97

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

#timeout_fallback(time) ⇒ Object



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

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' %>


16
17
18
# File 'app/helpers/metadata_presenter/application_helper.rb', line 16

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

#uploads_countObject



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

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



67
68
69
70
71
72
73
74
# File 'app/helpers/metadata_presenter/application_helper.rb', line 67

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