decidim-restfull-forms 0.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/app/concerns/decidim/api/rest_full/forms/controller_helpers.rb +66 -0
- data/app/controllers/decidim/api/rest_full/forms/answer_options_controller.rb +76 -0
- data/app/controllers/decidim/api/rest_full/forms/answers_controller.rb +85 -0
- data/app/controllers/decidim/api/rest_full/forms/questionnaire_responses_controller.rb +51 -0
- data/app/controllers/decidim/api/rest_full/forms/questionnaires_controller.rb +64 -0
- data/app/controllers/decidim/api/rest_full/forms/questions_controller.rb +90 -0
- data/app/controllers/decidim/api/rest_full/forms/submission_requests_controller.rb +39 -0
- data/app/serializers/decidim/api/rest_full/forms/answer_option_serializer.rb +17 -0
- data/app/serializers/decidim/api/rest_full/forms/answer_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/forms/application_serializer.rb +22 -0
- data/app/serializers/decidim/api/rest_full/forms/question_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/forms/questionnaire_response_serializer.rb +37 -0
- data/app/serializers/decidim/api/rest_full/forms/questionnaire_serializer.rb +75 -0
- data/app/serializers/decidim/api/rest_full/forms/submission_request_serializer.rb +37 -0
- data/app/services/decidim/rest_full/forms/answers_operations.rb +180 -0
- data/app/services/decidim/rest_full/forms/authoring_operations.rb +153 -0
- data/app/services/decidim/rest_full/forms/error_payload_builder.rb +40 -0
- data/app/services/decidim/rest_full/forms/locale_resolver.rb +86 -0
- data/app/services/decidim/rest_full/forms/questionnaire_json_forms_builder.rb +136 -0
- data/app/services/decidim/rest_full/forms/questionnaire_scope.rb +140 -0
- data/app/services/decidim/rest_full/forms/response_builder.rb +69 -0
- data/app/services/decidim/rest_full/forms/submission_bundle.rb +75 -0
- data/config/locales/decidim_rest_full_forms.en.yml +18 -0
- data/lib/decidim/rest_full/forms/decidim_032_compat.rb +62 -0
- data/lib/decidim/rest_full/forms/engine.rb +97 -0
- data/lib/decidim/rest_full/forms/test/definitions/forms_errors.rb +32 -0
- data/lib/decidim/rest_full/forms/test/definitions/locale_meta.rb +50 -0
- data/lib/decidim/rest_full/forms/test/definitions/question.rb +83 -0
- data/lib/decidim/rest_full/forms/test/definitions/question_create_request.rb +94 -0
- data/lib/decidim/rest_full/forms/test/definitions/questionnaire.rb +47 -0
- data/lib/decidim/rest_full/forms/test/definitions/questionnaire_response.rb +41 -0
- data/lib/decidim/rest_full/forms/test/definitions/request_bodies.rb +88 -0
- data/lib/decidim/rest_full/forms/test/definitions/submission_request.rb +44 -0
- data/lib/decidim/rest_full/forms/test/definitions/tags/forms_tag.rb +30 -0
- data/lib/decidim/rest_full/forms/test_definitions.rb +11 -0
- data/lib/decidim-restfull-forms.rb +14 -0
- data/spec/requests/decidim/api/rest_full/forms/answers_controller_create_spec.rb +68 -0
- data/spec/requests/decidim/api/rest_full/forms/answers_controller_sync_spec.rb +67 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaire_responses_controller_show_spec.rb +55 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_index_spec.rb +43 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_show_spec.rb +46 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_update_spec.rb +107 -0
- data/spec/requests/decidim/api/rest_full/forms/questions_controller_spec.rb +166 -0
- metadata +141 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class SubmissionRequestSerializer < ApplicationSerializer
|
|
8
|
+
set_type :submission_request
|
|
9
|
+
|
|
10
|
+
set_id(&:id)
|
|
11
|
+
|
|
12
|
+
attribute :status, &:status
|
|
13
|
+
|
|
14
|
+
meta do |job, _params|
|
|
15
|
+
{ status: job.status }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
link :self do |job, params|
|
|
19
|
+
{
|
|
20
|
+
href: "#{api_prefix(params[:host])}/submission_requests/#{job.id}",
|
|
21
|
+
rel: "self",
|
|
22
|
+
meta: { action_method: "GET" }
|
|
23
|
+
}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
link :result, if: proc { |job, params| job.status == "completed" && params[:result_id].present? } do |_, params|
|
|
27
|
+
{
|
|
28
|
+
href: "#{api_prefix(params[:host])}/questionnaire_responses/#{params[:result_id]}",
|
|
29
|
+
rel: "result",
|
|
30
|
+
meta: { action_method: "GET" }
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
class ValidationError < StandardError
|
|
7
|
+
attr_reader :payload
|
|
8
|
+
|
|
9
|
+
def initialize(payload)
|
|
10
|
+
@payload = payload
|
|
11
|
+
super("validation failed")
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Maps REST answer payloads to Decidim::Forms::AnswerQuestionnaire.
|
|
16
|
+
class AnswersOperations
|
|
17
|
+
def initialize(ctx, params)
|
|
18
|
+
@ctx = ctx
|
|
19
|
+
@params = params
|
|
20
|
+
@organization = ctx.organization
|
|
21
|
+
@payload = extract_payload
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def create!
|
|
25
|
+
questionnaire = load_questionnaire!
|
|
26
|
+
form = build_questionnaire_form(questionnaire)
|
|
27
|
+
raise ValidationError, ErrorPayloadBuilder.from_form(form, locale_meta:) if form.invalid?
|
|
28
|
+
|
|
29
|
+
result = nil
|
|
30
|
+
Decidim::Forms::AnswerQuestionnaire.call(form, questionnaire) do
|
|
31
|
+
on(:ok) { result = bundle_after_submit(questionnaire, form) }
|
|
32
|
+
on(:invalid) do
|
|
33
|
+
raise ValidationError, ErrorPayloadBuilder.from_form(form, locale_meta:)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
result
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
attr_reader :ctx, :params, :organization, :payload
|
|
42
|
+
|
|
43
|
+
def extract_payload
|
|
44
|
+
data = params["data"] || params[:data] || {}
|
|
45
|
+
data = data.to_unsafe_h if data.respond_to?(:to_unsafe_h)
|
|
46
|
+
meta = params["meta"] || params[:meta] || {}
|
|
47
|
+
meta = meta.to_unsafe_h if meta.respond_to?(:to_unsafe_h)
|
|
48
|
+
relationships = data["relationships"] || data[:relationships] || {}
|
|
49
|
+
{ data:, meta:, relationships: }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def load_questionnaire!
|
|
53
|
+
qrel = payload.dig(:relationships, "questionnaire") || payload.dig(:relationships, :questionnaire)
|
|
54
|
+
qdata = qrel&.dig("data") || qrel&.dig(:data)
|
|
55
|
+
qid = qdata&.dig("id") || qdata&.dig(:id)
|
|
56
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "relationships.questionnaire required" if qid.blank?
|
|
57
|
+
|
|
58
|
+
visibility = ctx.respond_to?(:visibility) ? ctx.visibility : nil
|
|
59
|
+
scope = QuestionnaireScope.new(organization:, visibility:)
|
|
60
|
+
scope.find!(qid)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def build_questionnaire_form(questionnaire)
|
|
64
|
+
form = Decidim::Forms::QuestionnaireForm.from_model(questionnaire)
|
|
65
|
+
answers_hash = payload.dig(:data, "attributes", "answers") ||
|
|
66
|
+
payload.dig(:data, :attributes, :answers) || {}
|
|
67
|
+
form.responses = questionnaire.questions.map do |question|
|
|
68
|
+
answer_form = Decidim::Forms::AnswerForm.from_model(
|
|
69
|
+
Decidim::Forms::Answer.new(question:)
|
|
70
|
+
)
|
|
71
|
+
qid = question.id.to_s
|
|
72
|
+
apply_answer!(answer_form, question, answers_hash[qid]) if answers_hash.has_key?(qid)
|
|
73
|
+
answer_form
|
|
74
|
+
end
|
|
75
|
+
form.tos_agreement = true
|
|
76
|
+
form.with_context(form_context(questionnaire))
|
|
77
|
+
form
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def apply_answer!(answer_form, question, value)
|
|
81
|
+
case Decidim032Compat.api_question_type(question.question_type)
|
|
82
|
+
when "short_answer", "long_answer"
|
|
83
|
+
answer_form.body = value.to_s
|
|
84
|
+
when "single_option"
|
|
85
|
+
answer_form.choices = [build_choice(value, question)]
|
|
86
|
+
when "multiple_option", "sorting"
|
|
87
|
+
ids = Array(value)
|
|
88
|
+
answer_form.choices = ids.map { |id| build_choice(id, question) }
|
|
89
|
+
when "matrix_single", "matrix_multiple"
|
|
90
|
+
answer_form.choices = value.to_h.map do |row_id, opt_id|
|
|
91
|
+
build_matrix_choice(row_id, opt_id, question)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def build_choice(option_id, question)
|
|
97
|
+
Decidim::Forms::AnswerChoiceForm.new(
|
|
98
|
+
body: "1",
|
|
99
|
+
answer_option_id: option_id.to_i,
|
|
100
|
+
question:
|
|
101
|
+
)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def build_matrix_choice(row_id, option_id, question)
|
|
105
|
+
Decidim::Forms::AnswerChoiceForm.new(
|
|
106
|
+
body: "1",
|
|
107
|
+
answer_option_id: option_id.to_i,
|
|
108
|
+
matrix_row_id: row_id.to_i,
|
|
109
|
+
question:
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def form_context(questionnaire)
|
|
114
|
+
respondent = resolve_respondent
|
|
115
|
+
session_token = session_token_for(questionnaire, respondent)
|
|
116
|
+
ip = payload.dig(:meta, "client_ip") || payload.dig(:meta, :client_ip)
|
|
117
|
+
OpenStruct.new(
|
|
118
|
+
current_user: respondent,
|
|
119
|
+
session_token:,
|
|
120
|
+
ip_hash: ip.present? ? tokenize_ip(questionnaire, ip) : nil,
|
|
121
|
+
responses: nil
|
|
122
|
+
)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def resolve_respondent
|
|
126
|
+
anonymous = payload.dig(:meta, "anonymous") == true || payload.dig(:meta, :anonymous) == true
|
|
127
|
+
return nil if anonymous
|
|
128
|
+
|
|
129
|
+
user_rel = payload.dig(:data, "relationships", "user") || payload.dig(:data, :relationships, :user)
|
|
130
|
+
user_data = user_rel&.dig("data") || user_rel&.dig(:data)
|
|
131
|
+
if user_data.present?
|
|
132
|
+
uid = user_data["id"] || user_data[:id]
|
|
133
|
+
return Decidim::User.find_by(id: uid, organization:)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
ctx.current_user
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def session_token_for(questionnaire, user)
|
|
140
|
+
if user
|
|
141
|
+
tokenize(questionnaire, user.id)
|
|
142
|
+
else
|
|
143
|
+
tokenize(questionnaire, SecureRandom.hex(16))
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def tokenize(questionnaire, id)
|
|
148
|
+
Decidim::Tokenizer.new(salt: questionnaire.salt || questionnaire.id, length: 10).int_digest(id).to_s
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def tokenize_ip(questionnaire, ip)
|
|
152
|
+
Decidim::Tokenizer.new(salt: questionnaire.salt || questionnaire.id, length: 10).int_digest(ip).to_s
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def bundle_after_submit(questionnaire, form)
|
|
156
|
+
user = form.current_user
|
|
157
|
+
session_token = form.context.session_token
|
|
158
|
+
anchor = Decidim::Forms::Answer.where(
|
|
159
|
+
decidim_questionnaire_id: questionnaire.id,
|
|
160
|
+
session_token:
|
|
161
|
+
).order(:id).first
|
|
162
|
+
anchor ||= Decidim::Forms::Answer.where(
|
|
163
|
+
decidim_questionnaire_id: questionnaire.id,
|
|
164
|
+
decidim_user_id: user&.id
|
|
165
|
+
).order(:id).last
|
|
166
|
+
{ questionnaire_response_id: anchor&.id }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def locale_meta
|
|
170
|
+
LocaleResolver.new(
|
|
171
|
+
organization:,
|
|
172
|
+
user: ctx.current_user,
|
|
173
|
+
params:,
|
|
174
|
+
accept_language: ctx.respond_to?(:request_headers) ? ctx.request_headers["HTTP_ACCEPT_LANGUAGE"] : nil
|
|
175
|
+
).meta_hash
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Sync/async commands for questionnaire authoring (questions, options, metadata).
|
|
7
|
+
class AuthoringOperations
|
|
8
|
+
def initialize(ctx, params)
|
|
9
|
+
@ctx = ctx
|
|
10
|
+
@params = params
|
|
11
|
+
@organization = ctx.organization
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def update_questionnaire!
|
|
15
|
+
questionnaire = scope.find!(resource_id)
|
|
16
|
+
attrs = data_attributes.permit(:tos, title: {}, description: {}).to_h
|
|
17
|
+
questionnaire.update!(attrs) if attrs.present?
|
|
18
|
+
questionnaire.reload
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def create_question!
|
|
22
|
+
questionnaire = scope.find!(questionnaire_id_from_relationship!)
|
|
23
|
+
questionnaire.questions.create!(question_attributes)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def update_question!
|
|
27
|
+
question = find_question!
|
|
28
|
+
question.update!(question_attributes)
|
|
29
|
+
question
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def destroy_question!
|
|
33
|
+
find_question!.destroy!
|
|
34
|
+
true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def create_answer_option!
|
|
38
|
+
question = find_question_by_filter!
|
|
39
|
+
body = data_attributes.permit(body: {}).to_h["body"] || {}
|
|
40
|
+
question.answer_options.create!(body:)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def update_answer_option!
|
|
44
|
+
option = find_answer_option!
|
|
45
|
+
body = data_attributes.permit(body: {}).to_h["body"] || {}
|
|
46
|
+
option.update!(body:)
|
|
47
|
+
option
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def destroy_answer_option!
|
|
51
|
+
find_answer_option!.destroy!
|
|
52
|
+
true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def destroy_questionnaire_response!
|
|
56
|
+
bundle = SubmissionBundle.find!(
|
|
57
|
+
resource_id,
|
|
58
|
+
organization: @organization,
|
|
59
|
+
visibility:
|
|
60
|
+
)
|
|
61
|
+
bundle.answers.each(&:destroy!)
|
|
62
|
+
true
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
attr_reader :ctx, :params, :organization
|
|
68
|
+
|
|
69
|
+
def scope
|
|
70
|
+
@scope ||= QuestionnaireScope.new(organization:, visibility:)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def visibility
|
|
74
|
+
ctx.respond_to?(:visibility) ? ctx.visibility : nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def resource_id
|
|
78
|
+
path_id = params.dig("path", "id") || params.dig(:path, :id)
|
|
79
|
+
(path_id || params[:id] || params["id"]).to_s
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def data_attributes
|
|
83
|
+
data = params["data"] || params[:data] || {}
|
|
84
|
+
data = data.to_unsafe_h if data.respond_to?(:to_unsafe_h)
|
|
85
|
+
attrs = data["attributes"] || data[:attributes] || {}
|
|
86
|
+
attrs = attrs.to_unsafe_h if attrs.respond_to?(:to_unsafe_h)
|
|
87
|
+
ActionController::Parameters.new(attrs)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def question_attributes
|
|
91
|
+
attrs = data_attributes.permit(
|
|
92
|
+
:position, :mandatory, :question_type, :max_choices, :max_characters, body: {}, description: {}
|
|
93
|
+
).to_h
|
|
94
|
+
attrs["question_type"] = Decidim032Compat.decidim_question_type(attrs["question_type"]) if attrs["question_type"].present?
|
|
95
|
+
attrs
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def questionnaire_id_from_relationship!
|
|
99
|
+
qid = questionnaire_id_from_relationship
|
|
100
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "relationships.questionnaire required" if qid.blank?
|
|
101
|
+
|
|
102
|
+
qid
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def questionnaire_id_from_relationship
|
|
106
|
+
questionnaire_relationship_data&.dig("id") || questionnaire_relationship_data&.dig(:id)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def questionnaire_relationship_data
|
|
110
|
+
rel = request_data_payload.dig("relationships", "questionnaire") ||
|
|
111
|
+
request_data_payload.dig(:relationships, :questionnaire)
|
|
112
|
+
rel&.dig("data") || rel&.dig(:data)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def request_data_payload
|
|
116
|
+
data = params["data"] || params[:data] || {}
|
|
117
|
+
data.respond_to?(:to_unsafe_h) ? data.to_unsafe_h : data
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def find_question!
|
|
121
|
+
Decidim::Forms::Question.find(resource_id).tap do |q|
|
|
122
|
+
scope.find!(q.decidim_questionnaire_id)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def find_question_by_filter!
|
|
127
|
+
qid = filter_hash["question_id"] || params.dig("path", "question_id") || params[:question_id]
|
|
128
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "filter[question_id] required" if qid.blank?
|
|
129
|
+
|
|
130
|
+
question = Decidim::Forms::Question.find(qid)
|
|
131
|
+
scope.find!(question.decidim_questionnaire_id)
|
|
132
|
+
question
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def find_answer_option!
|
|
136
|
+
Decidim::Forms::AnswerOption.find(resource_id).tap do |o|
|
|
137
|
+
question = Decidim::Forms::Question.find(o.decidim_question_id)
|
|
138
|
+
scope.find!(question.decidim_questionnaire_id)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def filter_hash
|
|
143
|
+
fp = params["filter"] || params[:filter] || params.dig("path", "filter")
|
|
144
|
+
return {} if fp.blank?
|
|
145
|
+
|
|
146
|
+
fp.respond_to?(:to_unsafe_h) ? fp.to_unsafe_h.stringify_keys : fp.to_h.stringify_keys
|
|
147
|
+
rescue StandardError
|
|
148
|
+
{}
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Stable 422 error envelope for forms API.
|
|
7
|
+
class ErrorPayloadBuilder
|
|
8
|
+
def self.from_form(form, locale_meta:)
|
|
9
|
+
errors = form.errors.map do |error|
|
|
10
|
+
question_id = extract_question_id(error.attribute, form)
|
|
11
|
+
{
|
|
12
|
+
title: error.message,
|
|
13
|
+
code: error.type.to_s,
|
|
14
|
+
pointer: pointer_for(error.attribute, question_id),
|
|
15
|
+
question_id:
|
|
16
|
+
}.compact
|
|
17
|
+
end
|
|
18
|
+
{ meta: locale_meta, errors: }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.from_message(message, code: "invalid", locale_meta: {})
|
|
22
|
+
{ meta: locale_meta, errors: [{ title: message, code: }] }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.extract_question_id(attribute, form)
|
|
26
|
+
match = attribute.to_s.match(/\Aresponse_(\d+)/)
|
|
27
|
+
return match[1] if match
|
|
28
|
+
|
|
29
|
+
form.question_id&.to_s if form.is_a?(Decidim::Forms::AnswerForm)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.pointer_for(attribute, question_id)
|
|
33
|
+
return "/data/attributes/answers/#{question_id}" if question_id
|
|
34
|
+
|
|
35
|
+
"/data/attributes/#{attribute}"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Resolves effective locale for questionnaire projection and error messages.
|
|
7
|
+
class LocaleResolver
|
|
8
|
+
Result = Struct.new(:locale, :requested_locale, :fallback_from, keyword_init: true)
|
|
9
|
+
|
|
10
|
+
def initialize(organization:, user: nil, params: {}, accept_language: nil)
|
|
11
|
+
@organization = organization
|
|
12
|
+
@user = user
|
|
13
|
+
@params = params
|
|
14
|
+
@accept_language = accept_language
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def resolve!
|
|
18
|
+
requested = explicit_locale || accept_language_locale
|
|
19
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "invalid_locale" if requested && available.exclude?(normalize(requested))
|
|
20
|
+
|
|
21
|
+
effective = pick_effective(requested)
|
|
22
|
+
fallback = requested && normalize(requested) != effective ? normalize(requested) : nil
|
|
23
|
+
Result.new(
|
|
24
|
+
locale: effective,
|
|
25
|
+
requested_locale: requested ? normalize(requested) : effective,
|
|
26
|
+
fallback_from: fallback
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def meta_hash
|
|
31
|
+
r = resolve!
|
|
32
|
+
{
|
|
33
|
+
locale: r.locale,
|
|
34
|
+
requested_locale: r.requested_locale,
|
|
35
|
+
fallback_from: r.fallback_from
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
attr_reader :organization, :user, :params, :accept_language
|
|
42
|
+
|
|
43
|
+
def available
|
|
44
|
+
@available ||= organization.available_locales.map { |l| normalize(l) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def explicit_locale
|
|
48
|
+
loc = params[:locale] || params["locale"]
|
|
49
|
+
loc.presence
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def accept_language_locale
|
|
53
|
+
return nil if accept_language.blank?
|
|
54
|
+
|
|
55
|
+
accept_language.split(",").each do |part|
|
|
56
|
+
tag = part.split(";").first.to_s.strip
|
|
57
|
+
next if tag.blank?
|
|
58
|
+
|
|
59
|
+
normalized = normalize(tag)
|
|
60
|
+
return normalized if available.include?(normalized)
|
|
61
|
+
|
|
62
|
+
base = normalized.split("-").first
|
|
63
|
+
match = available.find { |a| a == base || a.start_with?("#{base}-") }
|
|
64
|
+
return match if match
|
|
65
|
+
end
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def pick_effective(requested)
|
|
70
|
+
return normalize(requested) if requested && available.include?(normalize(requested))
|
|
71
|
+
|
|
72
|
+
if user&.locale.present?
|
|
73
|
+
ul = normalize(user.locale)
|
|
74
|
+
return ul if available.include?(ul)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
normalize(organization.default_locale)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def normalize(locale)
|
|
81
|
+
locale.to_s.tr("_", "-").downcase
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Projects Decidim::Forms::Questionnaire to JSON Schema + JSON Forms UI.
|
|
7
|
+
class QuestionnaireJsonFormsBuilder
|
|
8
|
+
def initialize(questionnaire, locale:, organization:, host:)
|
|
9
|
+
@questionnaire = questionnaire
|
|
10
|
+
@locale = locale
|
|
11
|
+
@organization = organization
|
|
12
|
+
@host = host
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def build
|
|
16
|
+
{
|
|
17
|
+
schema: build_schema,
|
|
18
|
+
ui: build_ui,
|
|
19
|
+
meta: build_submission_meta
|
|
20
|
+
}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
attr_reader :questionnaire, :locale, :organization, :host
|
|
26
|
+
|
|
27
|
+
def build_schema
|
|
28
|
+
properties = {}
|
|
29
|
+
questionnaire.questions.each do |question|
|
|
30
|
+
next if question.separator? || question.title_and_description?
|
|
31
|
+
|
|
32
|
+
key = question.id.to_s
|
|
33
|
+
properties[key] = schema_for_question(question)
|
|
34
|
+
end
|
|
35
|
+
{ type: "object", properties:, required: [] }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def build_ui
|
|
39
|
+
elements = questionnaire.questions.filter_map do |question|
|
|
40
|
+
ui_element_for_question(question)
|
|
41
|
+
end
|
|
42
|
+
{ type: "VerticalLayout", elements: }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def build_submission_meta
|
|
46
|
+
{
|
|
47
|
+
allows_anonymous: allows_anonymous?,
|
|
48
|
+
requires_participant_ip: requires_participant_ip?
|
|
49
|
+
}
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def allows_anonymous?
|
|
53
|
+
host = questionnaire.questionnaire_for
|
|
54
|
+
return true if host.is_a?(Decidim::Surveys::Survey)
|
|
55
|
+
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def requires_participant_ip?
|
|
60
|
+
allows_anonymous?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def schema_for_question(question)
|
|
64
|
+
case Decidim032Compat.api_question_type(question.question_type)
|
|
65
|
+
when "single_option", "multiple_option", "sorting"
|
|
66
|
+
{ type: "string", title: translate(question.body), enum: question.answer_options.map { |o| o.id.to_s } }
|
|
67
|
+
when "matrix_single", "matrix_multiple"
|
|
68
|
+
row_props = question.matrix_rows.index_with { |_row| { type: "string" } }
|
|
69
|
+
{ type: "object", title: translate(question.body), properties: row_props.transform_keys { |r| r.id.to_s } }
|
|
70
|
+
else
|
|
71
|
+
{ type: "string", title: translate(question.body) }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def ui_element_for_question(question)
|
|
76
|
+
return { type: "Group", label: translate(question.body), elements: [] } if question.separator?
|
|
77
|
+
|
|
78
|
+
control = {
|
|
79
|
+
type: "Control",
|
|
80
|
+
scope: "#/properties/#{question.id}",
|
|
81
|
+
label: translate(question.body)
|
|
82
|
+
}
|
|
83
|
+
control[:options] = control_options(question) if needs_options?(question)
|
|
84
|
+
rule = display_rule(question)
|
|
85
|
+
control[:rule] = rule if rule
|
|
86
|
+
control
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def needs_options?(question)
|
|
90
|
+
question.question_type.in?(%w(single_option multiple_option sorting matrix_single matrix_multiple))
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def control_options(question)
|
|
94
|
+
opts = {
|
|
95
|
+
choices: question.answer_options.map { |o| choice_hash(o) }
|
|
96
|
+
}
|
|
97
|
+
opts[:matrix] = { rowId: question.matrix_rows.first&.id&.to_s } if question.matrix?
|
|
98
|
+
opts
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def choice_hash(option)
|
|
102
|
+
translations = (option.body || {}).stringify_keys
|
|
103
|
+
{ id: option.id.to_s, translations: }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def display_rule(question)
|
|
107
|
+
condition = question.display_conditions.first
|
|
108
|
+
return nil unless condition
|
|
109
|
+
|
|
110
|
+
cond_qid = condition.condition_question_id
|
|
111
|
+
effect = "SHOW"
|
|
112
|
+
schema_fragment = case condition.condition_type
|
|
113
|
+
when "equal"
|
|
114
|
+
{ const: condition.decidim_answer_option_id.to_s }
|
|
115
|
+
else
|
|
116
|
+
{ minLength: 1 }
|
|
117
|
+
end
|
|
118
|
+
{
|
|
119
|
+
effect:,
|
|
120
|
+
condition: {
|
|
121
|
+
scope: "#/properties/#{cond_qid}",
|
|
122
|
+
schema: schema_fragment
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def translate(field)
|
|
128
|
+
return "" unless field.is_a?(Hash)
|
|
129
|
+
|
|
130
|
+
loc = locale.tr("-", "_").to_sym
|
|
131
|
+
field[loc] || field[locale] || field[organization.default_locale.to_sym] || field.values.compact.first.to_s
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|