-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathplan_presenter.rb
More file actions
77 lines (61 loc) · 2.39 KB
/
Copy pathplan_presenter.rb
File metadata and controls
77 lines (61 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
module Api
module V2
# Helper class for the API V2 project / DMP
class PlanPresenter
attr_reader :data_contact, :contributors, :costs, :complete_plan_data
def initialize(plan:, complete: false)
@contributors = []
return unless plan.present?
@plan = plan
@data_contact = @plan.owner
# Attach the first data_curation role as the data_contact, otherwise
# add the contributor to the contributors array
@plan.contributors.each do |contributor|
@data_contact = contributor if contributor.data_curation? && @data_contact.nil?
@contributors << contributor
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
def identifier
doi = @plan.identifiers.select do |id|
::Plan::DMP_ID_TYPES.include?(id.identifier_format)
end
return doi.first if doi.first.present?
# if no DOI then use the URL for the API's 'show' method
Identifier.new(value: Rails.application.routes.url_helpers.api_v2_plan_url(@plan))
end
private
# Fetch all questions and answers from a plan, regardless of theme
def fetch_all_q_and_a
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?
{
title: "Question #{q.number || q.id}",
question: q.text.to_s,
answer: a.text.to_s
}
end
end
# Retrieve the answers that have the Budget theme
def plan_costs(plan:)
theme = Theme.where(title: 'Cost').first
return [] unless theme.present?
# TODO: define a new 'Currency' question type that includes a float field
# any currency type selector (e.g GBP or USD)
answers = plan.answers.includes(question: :themes).select do |answer|
answer.question.themes.include?(theme)
end
answers.map do |answer|
# TODO: Investigate whether question level guidance should be the description
{ title: answer.question.text, description: nil,
currency_code: 'usd', value: answer.text }
end
end
end
end
end