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.
-
#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
- #raw_date_answer(component_id) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
- #upload_answer(component_id) ⇒ Object
- #validate_answers ⇒ Object
Constructor Details
#initialize(page, answers, autocomplete_items = nil) ⇒ PageAnswers
Returns a new instance of PageAnswers.
8 9 10 11 12 13 |
# File 'app/models/metadata_presenter/page_answers.rb', line 8 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
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/metadata_presenter/page_answers.rb', line 25 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) 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 |
#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
55 56 57 58 59 |
# File 'app/models/metadata_presenter/page_answers.rb', line 55 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
61 62 63 64 65 66 67 68 69 |
# File 'app/models/metadata_presenter/page_answers.rb', line 61 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
21 22 23 |
# File 'app/models/metadata_presenter/page_answers.rb', line 21 def respond_to_missing?(method_name, _include_private = false) method_name.to_s.in?(components.map(&:id)) end |
#upload_answer(component_id) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/metadata_presenter/page_answers.rb', line 39 def upload_answer(component_id) 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(file_details['original_filename'])) else { 'original_filename' => sanitize(file_details.original_filename), 'content_type' => file_details.content_type, 'tempfile' => file_details.tempfile.path.to_s } end end |
#validate_answers ⇒ Object
15 16 17 |
# File 'app/models/metadata_presenter/page_answers.rb', line 15 def validate_answers ValidateAnswers.new(self, components:, autocomplete_items:).valid? end |