Skip to content

Commit ccc7699

Browse files
committed
Reorganize files and functions
- Moved 'fetch_all_q_and_a' from research_output_present to plan_presenter - Moved q_and_a json to be inside extensions
1 parent 8f3beaf commit ccc7699

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

app/presenters/api/v2/plan_presenter.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ def identifier
3535
Identifier.new(value: Rails.application.routes.url_helpers.api_v2_plan_url(@plan))
3636
end
3737

38+
# Fetch all questions and answers from a plan, regardless of theme
39+
def fetch_all_q_and_a(plan:) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
40+
return [] unless plan&.questions.present?
41+
42+
plan.questions.filter_map do |q|
43+
a = plan.answers.find { |ans| ans.question_id == q.id }
44+
next unless a.present? && !a.blank?
45+
46+
{
47+
title: "Question #{q.number || q.id}",
48+
question: q.text.to_s,
49+
answer: a.text.to_s
50+
}
51+
end
52+
end
53+
3854
private
3955

4056
# Retrieve the answers that have the Budget theme

app/presenters/api/v2/research_output_presenter.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,6 @@ def fetch_q_and_a(themes:)
7171
end
7272
ret.select { |item| item[:description].present? }
7373
end
74-
75-
# Fetch all questions and answers from a plan, regardless of theme
76-
def fetch_all_q_and_a(plan:)
77-
return [] unless plan&.questions.present?
78-
79-
plan.questions.filter_map do |q|
80-
a = plan.answers.find { |ans| ans.question_id == q.id }
81-
next unless a.present? && !a.blank?
82-
83-
{
84-
title: "Question #{q.number || q.id}",
85-
question: q.text.to_s,
86-
answer: a.text.to_s
87-
}
88-
end
89-
end
9074
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
9175
end
9276
end

app/views/api/v2/plans/_show.json.jbuilder

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,6 @@ unless @minimal
5757

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

60-
if @question_and_answer
61-
json.questions_and_answers do
62-
outputs.each do |output|
63-
presenter = Api::V2::ResearchOutputPresenter.new(output: output)
64-
q_and_a = presenter.send(:fetch_all_q_and_a, plan: plan)
65-
next if q_and_a.blank?
66-
67-
json.set! output.id.to_s do
68-
json.array! q_and_a do |item|
69-
json.title item[:title]
70-
json.question item[:question]
71-
json.answer item[:answer]
72-
end
73-
end
74-
end
75-
end
76-
end
77-
7860
json.dataset outputs do |output|
7961
json.partial! "api/v2/datasets/show", output: output
8062
end
@@ -86,5 +68,22 @@ unless @minimal
8668
json.title template.title
8769
end
8870
end
71+
72+
if @question_and_answer
73+
json.questions_and_answers do
74+
outputs.each do |output|
75+
q_and_a = presenter.send(:fetch_all_q_and_a, plan: plan)
76+
next if q_and_a.blank?
77+
78+
json.set! output.id.to_s do
79+
json.array! q_and_a do |item|
80+
json.title item[:title]
81+
json.question item[:question]
82+
json.answer item[:answer]
83+
end
84+
end
85+
end
86+
end
87+
end
8988
end
9089
end

0 commit comments

Comments
 (0)