Class: MetadataPresenter::PageAnswers

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

Instance Method Summary collapse

Constructor Details

#initialize(page, answers) ⇒ PageAnswers

Returns a new instance of PageAnswers.



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

def initialize(page, answers)
  @page = page
  @answers = answers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'app/models/metadata_presenter/page_answers.rb', line 23

def method_missing(method_name, *args, &block)
  component = components.find { |component| component.id == method_name.to_s }

  if component && component.type == 'date'
    date_answer(component.id)
  else
    answers[method_name.to_s]
  end
end

Instance Method Details

#componentsObject



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

def components
  page.components
end

#date_answer(component_id) ⇒ Object



33
34
35
36
37
# File 'app/models/metadata_presenter/page_answers.rb', line 33

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



39
40
41
42
43
44
45
46
47
# File 'app/models/metadata_presenter/page_answers.rb', line 39

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

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

Returns:

  • (Boolean)


19
20
21
# File 'app/models/metadata_presenter/page_answers.rb', line 19

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

#validate_answersObject



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

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