Class: MetadataPresenter::PageAnswers
- Inherits:
-
Object
- Object
- MetadataPresenter::PageAnswers
- Includes:
- ActionView::Helpers, ActiveModel::Model, ActiveModel::Validations
- Defined in:
- app/models/metadata_presenter/page_answers.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
readonly
Returns the value of attribute answers.
-
#autocomplete_items ⇒ Object
readonly
Returns the value of attribute autocomplete_items.
-
#count ⇒ Object
Returns the value of attribute count.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#uploaded_files ⇒ Object
readonly
Returns the value of attribute uploaded_files.
Instance Method Summary collapse
- #date_answer(component_id) ⇒ Object
-
#initialize(page, answers, autocomplete_items = nil) ⇒ PageAnswers
constructor
A new instance of PageAnswers.
- #method_missing(method_name, *_args) ⇒ Object
- #multiupload_answer(component_id, _count) ⇒ Object
- #raw_date_answer(component_id) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
- #upload_answer(component_id, _count) ⇒ Object
- #validate_answers ⇒ Object
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 39 40 |
# 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 == 'multiupload' answer_object = multiupload_answer(component.id, count) answer_object.to_h if answer_object.present? elsif component && component.type == 'checkboxes' answers[method_name.to_s].to_a else sanitize(answers[method_name.to_s]) end end |
Instance Attribute Details
#answers ⇒ Object (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_items ⇒ Object (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 |
#count ⇒ Object
Returns the value of attribute count.
7 8 9 |
# File 'app/models/metadata_presenter/page_answers.rb', line 7 def count @count end |
#page ⇒ Object (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_files ⇒ Object (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
114 115 116 117 118 |
# File 'app/models/metadata_presenter/page_answers.rb', line 114 def date_answer(component_id) date = raw_date_answer(component_id) MetadataPresenter::DateField.new(day: date[0], month: date[1], year: date[2]) end |
#multiupload_answer(component_id, _count) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/metadata_presenter/page_answers.rb', line 58 def multiupload_answer(component_id, _count) file_details = answers[component_id.to_s] unless answers.is_a?(MetadataPresenter::MultiUploadAnswer) return nil if file_details.nil? && answers.nil? if file_details.is_a?(Hash) # when referencing a single previous answer but no incoming new answer presentable = MetadataPresenter::MultiUploadAnswer.new presentable.key = component_id.to_s presentable.previous_answers = [file_details] return presentable end if file_details.is_a?(Array) # when referencing multiple previous answers but no incoming new answer presentable = MetadataPresenter::MultiUploadAnswer.new presentable.key = component_id.to_s presentable.previous_answers = file_details.reject { |f| f['original_filename'].blank? } return presentable end if answers.blank? return nil end if answers.is_a?(Hash) # rendering only existing answers return if answers[component_id].blank? if answers[component_id].is_a?(Array) answers[component_id].each { |answer| answer['original_filename'] = sanitize(filename(update_filename(answer['original_filename']))) } end answers[component_id] = answers[component_id].reject { |a| a['original_filename'].blank? } return answers end # uploading a new answer, this method will be called during multiple render operations if answers.incoming_answer.present? && answers.incoming_answer.is_a?(ActionController::Parameters) answers.incoming_answer[component_id].original_filename = sanitize(filename(update_filename(answers.incoming_answer[component_id].original_filename))) end if answers.incoming_answer.present? && answers.incoming_answer.is_a?(Hash) answers.incoming_answer['original_filename'] = sanitize(filename(update_filename(answers.incoming_answer['original_filename']))) end if answers.incoming_answer.present? && answers.incoming_answer[component_id].is_a?(ActionDispatch::Http::UploadedFile) answers.incoming_answer = { 'original_filename' => sanitize(filename(update_filename(answers.incoming_answer[component_id].original_filename))), 'content_type' => answers.incoming_answer[component_id].content_type, 'tempfile' => answers.incoming_answer[component_id].tempfile.path.to_s, 'uuid' => SecureRandom.uuid } end answers end |
#raw_date_answer(component_id) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'app/models/metadata_presenter/page_answers.rb', line 120 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
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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/metadata_presenter/page_answers.rb', line 42 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(update_filename(file_details['original_filename'])))) else { 'original_filename' => sanitize(filename(update_filename(file_details.original_filename))), 'content_type' => file_details.content_type, 'tempfile' => file_details.tempfile.path.to_s } end end |
#validate_answers ⇒ Object
16 17 18 |
# File 'app/models/metadata_presenter/page_answers.rb', line 16 def validate_answers ValidateAnswers.new(self, components:, autocomplete_items:).valid? end |