Skip to content
16 changes: 16 additions & 0 deletions app/presenters/api/v2/plan_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ def identifier
Identifier.new(value: Rails.application.routes.url_helpers.api_v2_plan_url(@plan))
end

# Fetch all questions and answers from a plan, regardless of theme
def fetch_all_q_and_a(plan:) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
return [] unless plan&.questions.present?

plan.questions.filter_map do |q|
a = plan.answers.find { |ans| ans.question_id == q.id }
next unless a.present? && !a.blank?

{
title: "Question #{q.number || q.id}",
question: q.text.to_s,
answer: a.text.to_s
}
end
end

private

# Retrieve the answers that have the Budget theme
Expand Down
16 changes: 0 additions & 16 deletions app/presenters/api/v2/research_output_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,6 @@ def fetch_q_and_a(themes:)
end
ret.select { |item| item[:description].present? }
end

# Fetch all questions and answers from a plan, regardless of theme
def fetch_all_q_and_a(plan:)
return [] unless plan&.questions.present?

plan.questions.filter_map do |q|
a = plan.answers.find { |ans| ans.question_id == q.id }
next unless a.present? && !a.blank?

{
title: "Question #{q.number || q.id}",
question: q.text.to_s,
answer: a.text.to_s
}
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
end
end
Expand Down
35 changes: 17 additions & 18 deletions app/views/api/v2/plans/_show.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ unless @minimal

outputs = plan.research_outputs.any? ? plan.research_outputs : [plan]

if @question_and_answer
json.questions_and_answers do
outputs.each do |output|
presenter = Api::V2::ResearchOutputPresenter.new(output: output)
q_and_a = presenter.send(:fetch_all_q_and_a, plan: plan)
next if q_and_a.blank?

json.set! output.id.to_s do
json.array! q_and_a do |item|
json.title item[:title]
json.question item[:question]
json.answer item[:answer]
end
end
end
end
end

json.dataset outputs do |output|
json.partial! "api/v2/datasets/show", output: output
end
Expand All @@ -86,5 +68,22 @@ unless @minimal
json.title template.title
end
end

if @question_and_answer
json.questions_and_answers do
outputs.each do |output|
q_and_a = presenter.send(:fetch_all_q_and_a, plan: plan)
next if q_and_a.blank?

json.set! output.id.to_s do
json.array! q_and_a do |item|
json.title item[:title]
json.question item[:question]
json.answer item[:answer]
end
end
end
end
end
end
end