From bd71b234cff2ab41d7de0b0b857212676566c4a0 Mon Sep 17 00:00:00 2001 From: momo3404 Date: Fri, 6 Feb 2026 11:37:21 -0700 Subject: [PATCH 1/3] Add complete plan flag to `plans_controller` - Edit plan query to check for user role to prevent eager loading - Add set_complete_param function that sets complete flag according to its value if its set to true in the API call --- app/controllers/api/v2/plans_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v2/plans_controller.rb b/app/controllers/api/v2/plans_controller.rb index 7714e9c892..c82321551a 100644 --- a/app/controllers/api/v2/plans_controller.rb +++ b/app/controllers/api/v2/plans_controller.rb @@ -4,12 +4,13 @@ module Api module V2 class PlansController < BaseApiController # rubocop:todo Style/Documentation respond_to :json + before_action :set_complete_param, only: %i[show index] # GET /api/v2/plans/:id def show raise Pundit::NotAuthorizedError unless @scopes.include?('read') - @plan = Plan.find_by(id: params[:id]) + @plan = Plan.includes(roles: :user).find_by(id: params[:id]) raise Pundit::NotAuthorizedError unless @plan.present? @@ -28,6 +29,11 @@ def index @items = paginate_response(results: @plans) render '/api/v2/plans/index', status: :ok end + + # GET /api/v2/plans?complete=true and /api/v2/plans/:id?complete=true + def set_complete_param + @complete = params[:complete].to_s.downcase == 'true' + end end end end From bae162d15723cdd459ac4b19ca6b34c8f3ace64f Mon Sep 17 00:00:00 2001 From: momo3404 Date: Fri, 6 Feb 2026 11:39:02 -0700 Subject: [PATCH 2/3] Add complete plan flag to `plan_presenter` - Edit initialize to include complete flag, which is set to false as default - If flag is true in call, call fetch_all_q_and_a - Add fetch_all_q_and_a function that fetches questions and answers --- app/presenters/api/v2/plan_presenter.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/presenters/api/v2/plan_presenter.rb b/app/presenters/api/v2/plan_presenter.rb index c841844c6c..ec494558f7 100644 --- a/app/presenters/api/v2/plan_presenter.rb +++ b/app/presenters/api/v2/plan_presenter.rb @@ -4,9 +4,9 @@ module Api module V2 # Helper class for the API V2 project / DMP class PlanPresenter - attr_reader :data_contact, :contributors, :costs + attr_reader :data_contact, :contributors, :costs, :complete_plan_data - def initialize(plan:) + def initialize(plan:, complete: false) @contributors = [] return unless plan.present? @@ -22,6 +22,8 @@ def initialize(plan:) end @costs = plan_costs(plan: @plan) + + @complete_plan_data = fetch_all_q_and_a if complete end # Extract the ARK or DOI for the DMP OR use its URL if none exists @@ -55,6 +57,23 @@ def plan_costs(plan:) currency_code: 'usd', value: answer.text } end end + + # Fetch all questions and answers from a plan, regardless of theme + def fetch_all_q_and_a + answers = @plan.answers.includes(:question) + return [] unless answers.present? + + answers.filter_map do |answer| + q = answer.question + next unless q.present? + + { + title: "Question #{q.number || q.id}", + question: q.text.to_s, + answer: answer.text.to_s + } + end + end end end end From 67f79fd7df76196fed60589d4f4f4726a65b1b5b Mon Sep 17 00:00:00 2001 From: momo3404 Date: Fri, 6 Feb 2026 11:40:03 -0700 Subject: [PATCH 3/3] Add complete plan flag to `plans/_show.json.jbuilder` - Add complete flag data to extension in json --- app/views/api/v2/plans/_show.json.jbuilder | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/views/api/v2/plans/_show.json.jbuilder b/app/views/api/v2/plans/_show.json.jbuilder index 0de52775a3..3cd5dfe8c3 100644 --- a/app/views/api/v2/plans/_show.json.jbuilder +++ b/app/views/api/v2/plans/_show.json.jbuilder @@ -4,7 +4,7 @@ json.schema 'https://github.com/RDA-DMP-Common/RDA-DMP-Common-Standard/tree/master/examples/JSON/JSON-schema/1.0' -presenter = Api::V2::PlanPresenter.new(plan: plan) +presenter = Api::V2::PlanPresenter.new(plan: plan, complete: @complete) # Note the symbol of the dmproadmap json object # nested in extensions which is the container for the json template object, etc. @@ -68,5 +68,18 @@ unless @minimal json.title template.title end end + + if @complete + json.complete_plan do + q_and_a = presenter.complete_plan_data + next if q_and_a.blank? + + json.array! q_and_a do |item| + json.title item[:title] + json.question item[:question] + json.answer item[:answer] + end + end + end end end