Class: MetadataPresenter::PageAnswers

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers, ActiveModel::Model, ActiveModel::Validations
Defined in:
app/models/metadata_presenter/page_answers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, answers, autocomplete_items = nil) ⇒ PageAnswers

Returns a new instance of PageAnswers.



9
10
11
12
13
14
# File 'app/models/metadata_presenter/page_answers.rb', line 9

def initialize(page, answers, autocomplete_items = nil)
  @page = page
  @answers = answers
  @autocomplete_items = autocomplete_items
  @uploaded_files = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/metadata_presenter/page_answers.rb', line 26

def method_missing(method_name, *_args)
  component = components.find { |c| c.id == method_name.to_s }

  if component && component.type == 'date'
    date_answer(component.id)
  elsif component && component.type == 'upload'
    upload_answer(component.id, count)
  elsif component && component.type == 'checkboxes'
    answers[method_name.to_s].to_a
  else
    sanitize(answers[method_name.to_s])
  end
end

Instance Attribute Details

#answersObject (readonly)

Returns the value of attribute answers.



6
7
8
# File 'app/models/metadata_presenter/page_answers.rb', line 6

def answers
  @answers
end

#autocomplete_itemsObject (readonly)

Returns the value of attribute autocomplete_items.



6
7
8
# File 'app/models/metadata_presenter/page_answers.rb', line 6

def autocomplete_items
  @autocomplete_items
end

#countObject

Returns the value of attribute count.



7
8
9
# File 'app/models/metadata_presenter/page_answers.rb', line 7

def count
  @count
end

#pageObject (readonly)

Returns the value of attribute page.



6
7
8
# File 'app/models/metadata_presenter/page_answers.rb', line 6

def page
  @page
end

#uploaded_filesObject (readonly)

Returns the value of attribute uploaded_files.



6
7
8
# File 'app/models/metadata_presenter/page_answers.rb', line 6

def uploaded_files
  @uploaded_files
end

Instance Method Details

#date_answer(component_id) ⇒ Object



56
57
58
59
60
# File 'app/models/metadata_presenter/page_answers.rb', line 56

def date_answer(component_id)
  date = raw_date_answer(component_id)

  MetadataPresenter::DateField.new(day: date[0], month: date[1], year: date[2])
end

#raw_date_answer(component_id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'app/models/metadata_presenter/page_answers.rb', line 62

def raw_date_answer(component_id)
  [
    GOVUKDesignSystemFormBuilder::Elements::Date::SEGMENTS[:day],
    GOVUKDesignSystemFormBuilder::Elements::Date::SEGMENTS[:month],
    GOVUKDesignSystemFormBuilder::Elements::Date::SEGMENTS[:year]
  ].map do |segment|
    sanitize(answers["#{component_id}(#{segment})"])
  end
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/metadata_presenter/page_answers.rb', line 22

def respond_to_missing?(method_name, _include_private = false)
  method_name.to_s.in?(components.map(&:id))
end

#upload_answer(component_id, _count) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/metadata_presenter/page_answers.rb', line 40

def upload_answer(component_id, _count)
  file_details = answers[component_id.to_s]

  return {} unless file_details

  if file_details.is_a?(Hash) || file_details.is_a?(ActionController::Parameters)
    file_details.merge('original_filename' => sanitize(filename(file_details['original_filename'])))
  else
    {
      'original_filename' => sanitize(filename(file_details.original_filename)),
      'content_type' => file_details.content_type,
      'tempfile' => file_details.tempfile.path.to_s
    }
  end
end

#validate_answersObject



16
17
18
# File 'app/models/metadata_presenter/page_answers.rb', line 16

def validate_answers
  ValidateAnswers.new(self, components:, autocomplete_items:).valid?
end