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,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Tenant-safe questionnaire lookup and filtering for the forms API.
|
|
7
|
+
class QuestionnaireScope
|
|
8
|
+
def initialize(organization:, visibility:)
|
|
9
|
+
@organization = organization
|
|
10
|
+
@visibility = visibility
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def find!(id)
|
|
14
|
+
questionnaire = Decidim::Forms::Questionnaire.includes(
|
|
15
|
+
questions: [Decidim032Compat.question_options_association, :matrix_rows, :display_conditions]
|
|
16
|
+
).find_by(id:)
|
|
17
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Questionnaire not found" unless questionnaire
|
|
18
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Questionnaire not found" unless in_organization?(questionnaire)
|
|
19
|
+
|
|
20
|
+
questionnaire
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def base_relation
|
|
24
|
+
Decidim::Forms::Questionnaire
|
|
25
|
+
.includes(:questions)
|
|
26
|
+
.where(id: visible_questionnaire_ids)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def filter(relation, filters)
|
|
30
|
+
ids = filtered_questionnaire_ids(filters)
|
|
31
|
+
return relation.none if ids.empty?
|
|
32
|
+
|
|
33
|
+
relation.where(id: ids)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def organization_for(questionnaire)
|
|
37
|
+
host = questionnaire.questionnaire_for
|
|
38
|
+
if host.respond_to?(:organization)
|
|
39
|
+
host.organization
|
|
40
|
+
elsif host.respond_to?(:component)
|
|
41
|
+
host.component.organization
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def component_for(questionnaire)
|
|
46
|
+
host = questionnaire.questionnaire_for
|
|
47
|
+
host.component if host.respond_to?(:component)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
attr_reader :organization, :visibility
|
|
53
|
+
|
|
54
|
+
def in_organization?(questionnaire)
|
|
55
|
+
organization_for(questionnaire)&.id == organization.id
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def visible_questionnaire_ids
|
|
59
|
+
survey_ids = visible_survey_ids
|
|
60
|
+
meeting_ids = visible_meeting_ids
|
|
61
|
+
Decidim::Forms::Questionnaire.where(
|
|
62
|
+
"(questionnaire_for_type = ? AND questionnaire_for_id IN (?)) OR " \
|
|
63
|
+
"(questionnaire_for_type = ? AND questionnaire_for_id IN (?))",
|
|
64
|
+
"Decidim::Surveys::Survey", survey_ids,
|
|
65
|
+
"Decidim::Meetings::Meeting", meeting_ids
|
|
66
|
+
).pluck(:id)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def visible_survey_ids
|
|
70
|
+
return [] unless defined?(Decidim::Surveys::Survey)
|
|
71
|
+
|
|
72
|
+
component_ids = visible_component_ids("surveys")
|
|
73
|
+
Decidim::Surveys::Survey.where(decidim_component_id: component_ids).pluck(:id)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def visible_meeting_ids
|
|
77
|
+
return [] unless defined?(Decidim::Meetings::Meeting)
|
|
78
|
+
|
|
79
|
+
component_ids = visible_component_ids("meetings")
|
|
80
|
+
Decidim::Meetings::Meeting.where(decidim_component_id: component_ids).pluck(:id)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def visible_component_ids(manifest_name)
|
|
84
|
+
components = Decidim::Component.where(manifest_name:)
|
|
85
|
+
return components.pluck(:id) unless visibility
|
|
86
|
+
|
|
87
|
+
visibility.in_visible_spaces(components).pluck(:id)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def filtered_questionnaire_ids(filters)
|
|
91
|
+
ids = visible_questionnaire_ids
|
|
92
|
+
if filters["component_id"].present?
|
|
93
|
+
cids = Array(filters["component_id"]).map(&:to_i)
|
|
94
|
+
ids &= questionnaire_ids_for_component_ids(cids)
|
|
95
|
+
end
|
|
96
|
+
if filters["participatory_space_id"].present?
|
|
97
|
+
space_ids = Array(filters["participatory_space_id"]).map(&:to_i)
|
|
98
|
+
ids &= questionnaire_ids_for_space_ids(space_ids)
|
|
99
|
+
end
|
|
100
|
+
if filters["questionnaire_id"].present?
|
|
101
|
+
qids = Array(filters["questionnaire_id"]).map(&:to_i)
|
|
102
|
+
ids &= qids
|
|
103
|
+
end
|
|
104
|
+
ids
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def questionnaire_ids_for_component_ids(component_ids)
|
|
108
|
+
survey_q = survey_questionnaire_ids_for_components(component_ids)
|
|
109
|
+
meeting_q = meeting_questionnaire_ids_for_components(component_ids)
|
|
110
|
+
survey_q + meeting_q
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def questionnaire_ids_for_space_ids(space_ids)
|
|
114
|
+
component_ids = Decidim::Component.where(participatory_space_id: space_ids).pluck(:id)
|
|
115
|
+
questionnaire_ids_for_component_ids(component_ids)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def survey_questionnaire_ids_for_components(component_ids)
|
|
119
|
+
return [] unless defined?(Decidim::Surveys::Survey)
|
|
120
|
+
|
|
121
|
+
Decidim::Forms::Questionnaire
|
|
122
|
+
.joins("INNER JOIN decidim_surveys_surveys ON decidim_surveys_surveys.id = decidim_forms_questionnaires.questionnaire_for_id " \
|
|
123
|
+
"AND decidim_forms_questionnaires.questionnaire_for_type = 'Decidim::Surveys::Survey'")
|
|
124
|
+
.where(decidim_surveys_surveys: { decidim_component_id: component_ids })
|
|
125
|
+
.pluck(:id)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def meeting_questionnaire_ids_for_components(component_ids)
|
|
129
|
+
return [] unless defined?(Decidim::Meetings::Meeting)
|
|
130
|
+
|
|
131
|
+
Decidim::Forms::Questionnaire
|
|
132
|
+
.joins("INNER JOIN decidim_meetings_meetings ON decidim_meetings_meetings.id = decidim_forms_questionnaires.questionnaire_for_id " \
|
|
133
|
+
"AND decidim_forms_questionnaires.questionnaire_for_type = 'Decidim::Meetings::Meeting'")
|
|
134
|
+
.where(decidim_meetings_meetings: { decidim_component_id: component_ids })
|
|
135
|
+
.pluck(:id)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Thin facade over JSON:API serializers (keeps controllers terse).
|
|
7
|
+
module ResponseBuilder
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def questionnaire_show(questionnaire, projection:, locale_meta:, host:)
|
|
11
|
+
Decidim::Api::RestFull::Forms::QuestionnaireSerializer.new(
|
|
12
|
+
questionnaire,
|
|
13
|
+
params: serializer_params(host:, locale_meta:, locale: locale_meta[:locale], projection:)
|
|
14
|
+
).serializable_hash
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def questionnaire_index(questionnaires, locale_meta:, host:)
|
|
18
|
+
Decidim::Api::RestFull::Forms::QuestionnaireSerializer.new(
|
|
19
|
+
questionnaires,
|
|
20
|
+
params: serializer_params(host:, locale_meta:, locale: locale_meta[:locale])
|
|
21
|
+
).serializable_hash
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def questionnaire_response(bundle, locale_meta:, host:)
|
|
25
|
+
Decidim::Api::RestFull::Forms::QuestionnaireResponseSerializer.new(
|
|
26
|
+
bundle,
|
|
27
|
+
params: serializer_params(host:, locale_meta:, locale: locale_meta[:locale])
|
|
28
|
+
).serializable_hash
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def submission_request(job, host:, result_id: nil)
|
|
32
|
+
Decidim::Api::RestFull::Forms::SubmissionRequestSerializer.new(
|
|
33
|
+
job,
|
|
34
|
+
params: serializer_params(host:, result_id:)
|
|
35
|
+
).serializable_hash
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def question(question)
|
|
39
|
+
Decidim::Api::RestFull::Forms::QuestionSerializer.new(question).serializable_hash
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def questions(questions, locale_meta:, host:)
|
|
43
|
+
Decidim::Api::RestFull::Forms::QuestionSerializer.new(
|
|
44
|
+
questions,
|
|
45
|
+
params: serializer_params(host:, locale_meta:, locale: locale_meta[:locale])
|
|
46
|
+
).serializable_hash.merge(meta: locale_meta)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def answer_option(option)
|
|
50
|
+
Decidim::Api::RestFull::Forms::AnswerOptionSerializer.new(option).serializable_hash
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def answer_index(answers, locale_meta:)
|
|
54
|
+
Decidim::Api::RestFull::Forms::AnswerSerializer.new(answers).serializable_hash.merge(meta: locale_meta)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def serializer_params(host:, locale_meta: nil, locale: nil, projection: nil, result_id: nil)
|
|
58
|
+
{
|
|
59
|
+
host:,
|
|
60
|
+
locale_meta:,
|
|
61
|
+
locale: locale || locale_meta&.dig(:locale),
|
|
62
|
+
projection:,
|
|
63
|
+
result_id:
|
|
64
|
+
}.compact
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
# Groups persisted answers into one questionnaire_response bundle.
|
|
7
|
+
class SubmissionBundle
|
|
8
|
+
def self.find!(response_id, organization:, visibility: nil)
|
|
9
|
+
anchor = Decidim::Forms::Answer.find_by(id: response_id)
|
|
10
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Submission not found" unless anchor
|
|
11
|
+
|
|
12
|
+
scope = QuestionnaireScope.new(organization:, visibility:)
|
|
13
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Submission not found" unless scope.organization_for(anchor.questionnaire)&.id == organization.id
|
|
14
|
+
|
|
15
|
+
answers = Decidim::Forms::Answer.where(
|
|
16
|
+
decidim_questionnaire_id: anchor.decidim_questionnaire_id,
|
|
17
|
+
decidim_user_id: anchor.decidim_user_id,
|
|
18
|
+
session_token: anchor.session_token
|
|
19
|
+
).includes(:question, :choices)
|
|
20
|
+
|
|
21
|
+
new(anchor_id: anchor.id, answers: answers.to_a, questionnaire: anchor.questionnaire)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
attr_reader :anchor_id, :answers, :questionnaire
|
|
25
|
+
|
|
26
|
+
def id
|
|
27
|
+
anchor_id.to_s
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(anchor_id:, answers:, questionnaire:)
|
|
31
|
+
@anchor_id = anchor_id
|
|
32
|
+
@answers = answers
|
|
33
|
+
@questionnaire = questionnaire
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def answers_map
|
|
37
|
+
answers.each_with_object({}) do |answer, hash|
|
|
38
|
+
next if answer.question.separator? || answer.question.title_and_description?
|
|
39
|
+
|
|
40
|
+
hash[answer.decidim_question_id.to_s] = serialize_answer(answer)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def user
|
|
45
|
+
answers.first&.user
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def ip_hash
|
|
49
|
+
answers.first&.ip_hash
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def created_at
|
|
53
|
+
answers.map(&:created_at).compact.min
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def serialize_answer(answer)
|
|
59
|
+
if answer.choices.any?
|
|
60
|
+
if answer.question.matrix?
|
|
61
|
+
answer.choices.each_with_object({}) do |choice, h|
|
|
62
|
+
row_id = choice.decidim_question_matrix_row_id
|
|
63
|
+
h[row_id.to_s] = choice.decidim_answer_option_id.to_s if row_id
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
answer.choices.map { |c| c.decidim_answer_option_id.to_s }
|
|
67
|
+
end
|
|
68
|
+
else
|
|
69
|
+
answer.body
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
en:
|
|
2
|
+
decidim:
|
|
3
|
+
rest_full:
|
|
4
|
+
forms:
|
|
5
|
+
oauth_scopes:
|
|
6
|
+
surveys: Surveys & questionnaires
|
|
7
|
+
models:
|
|
8
|
+
api_client:
|
|
9
|
+
permission:
|
|
10
|
+
surveys:
|
|
11
|
+
answers:
|
|
12
|
+
destroy: Delete submissions
|
|
13
|
+
read: Read answers and submissions
|
|
14
|
+
submit: Submit questionnaire answers
|
|
15
|
+
questionnaires:
|
|
16
|
+
read: Read questionnaires (JSON Forms projection)
|
|
17
|
+
questions:
|
|
18
|
+
manage: Manage questionnaire questions
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Decidim 0.32 renamed Forms Answer* → Response*. Keep RestFull on historical
|
|
4
|
+
# "answer" vocabulary via aliases (models load after Decidim::Forms is required).
|
|
5
|
+
# On 0.29, association includes must use :answer_options (aliases do not help AR).
|
|
6
|
+
module Decidim
|
|
7
|
+
module RestFull
|
|
8
|
+
module Forms
|
|
9
|
+
module Decidim032Compat
|
|
10
|
+
# API keeps short_answer/long_answer; Decidim 0.32 stores short_response/long_response.
|
|
11
|
+
QUESTION_TYPE_API_TO_DECIDIM = {
|
|
12
|
+
"short_answer" => "short_response",
|
|
13
|
+
"long_answer" => "long_response"
|
|
14
|
+
}.freeze
|
|
15
|
+
QUESTION_TYPE_DECIDIM_TO_API = QUESTION_TYPE_API_TO_DECIDIM.invert.freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def response_vocabulary?
|
|
20
|
+
defined?(::Decidim::Forms::Response)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def question_options_association
|
|
24
|
+
response_vocabulary? ? :response_options : :answer_options
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def api_question_type(decidim_type)
|
|
28
|
+
return decidim_type.to_s unless response_vocabulary?
|
|
29
|
+
|
|
30
|
+
QUESTION_TYPE_DECIDIM_TO_API.fetch(decidim_type.to_s, decidim_type.to_s)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def decidim_question_type(api_type)
|
|
34
|
+
return api_type.to_s unless response_vocabulary?
|
|
35
|
+
|
|
36
|
+
QUESTION_TYPE_API_TO_DECIDIM.fetch(api_type.to_s, api_type.to_s)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def apply!
|
|
40
|
+
return unless response_vocabulary?
|
|
41
|
+
return if ::Decidim::Forms.const_defined?(:Answer, false)
|
|
42
|
+
|
|
43
|
+
::Decidim::Forms.const_set(:Answer, ::Decidim::Forms::Response)
|
|
44
|
+
::Decidim::Forms.const_set(:AnswerOption, ::Decidim::Forms::ResponseOption)
|
|
45
|
+
::Decidim::Forms.const_set(:AnswerChoice, ::Decidim::Forms::ResponseChoice)
|
|
46
|
+
::Decidim::Forms.const_set(:AnswerForm, ::Decidim::Forms::ResponseForm)
|
|
47
|
+
::Decidim::Forms.const_set(:AnswerChoiceForm, ::Decidim::Forms::ResponseChoiceForm)
|
|
48
|
+
::Decidim::Forms.const_set(:AnswerQuestionnaire, ::Decidim::Forms::ResponseQuestionnaire)
|
|
49
|
+
|
|
50
|
+
::Decidim::Forms::Question.class_eval do
|
|
51
|
+
alias_method :answer_options, :response_options unless method_defined?(:answer_options)
|
|
52
|
+
alias_method :answers, :responses unless method_defined?(:answers)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
::Decidim::Forms::Questionnaire.class_eval do
|
|
56
|
+
alias_method :answers, :responses unless method_defined?(:answers)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Forms
|
|
6
|
+
class Engine < ::Rails::Engine
|
|
7
|
+
config.root = Forms::ENGINE_ROOT
|
|
8
|
+
|
|
9
|
+
# ponytail: const aliases until API vocabulary moves to Response*
|
|
10
|
+
config.to_prepare do
|
|
11
|
+
Decidim::RestFull::Forms::Decidim032Compat.apply!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
initializer "rest_full.forms.extension" do
|
|
15
|
+
Decidim::RestFull::Extension.register(:forms) do |ext|
|
|
16
|
+
ext.toggle_feature gem: "decidim-restfull-forms"
|
|
17
|
+
ext.controller_paths(
|
|
18
|
+
"forms", "questionnaires", "questions", "answers",
|
|
19
|
+
"answer_options", "questionnaire_responses", "submission_requests"
|
|
20
|
+
)
|
|
21
|
+
ext.oauth_scopes :surveys
|
|
22
|
+
ext.permissions(:surveys, "surveys.questionnaires.read", group: :surveys)
|
|
23
|
+
ext.permissions(:surveys, "surveys.questions.manage", group: :surveys)
|
|
24
|
+
ext.permissions(:surveys, "surveys.answers.read", group: :surveys)
|
|
25
|
+
ext.permissions(:surveys, "surveys.answers.submit", group: :surveys)
|
|
26
|
+
ext.permissions(:surveys, "surveys.answers.destroy", group: :surveys)
|
|
27
|
+
|
|
28
|
+
ext.api_job "forms/answers#create", ->(ctx, p) { Forms::AnswersOperations.new(ctx, p).create! }
|
|
29
|
+
ext.api_job "forms/questionnaires#update", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).update_questionnaire! }
|
|
30
|
+
ext.api_job "forms/questions#create", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).create_question! }
|
|
31
|
+
ext.api_job "forms/questions#update", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).update_question! }
|
|
32
|
+
ext.api_job "forms/questions#destroy", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).destroy_question! }
|
|
33
|
+
ext.api_job "forms/answer_options#create", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).create_answer_option! }
|
|
34
|
+
ext.api_job "forms/answer_options#update", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).update_answer_option! }
|
|
35
|
+
ext.api_job "forms/answer_options#destroy", ->(ctx, p) { Forms::AuthoringOperations.new(ctx, p).destroy_answer_option! }
|
|
36
|
+
ext.api_job "forms/questionnaire_responses#destroy", lambda { |ctx, p|
|
|
37
|
+
Forms::AuthoringOperations.new(ctx, p).destroy_questionnaire_response!
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
ext.open_api_definitions(
|
|
41
|
+
File.join(Forms::ENGINE_ROOT, "lib/decidim/rest_full/forms/test_definitions.rb")
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
ext.rswag_specs(
|
|
45
|
+
File.join(Forms::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/forms/**/*_spec.rb")
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
ext.routes do
|
|
49
|
+
Decidim::RestFull::Routing.async_resources(
|
|
50
|
+
self,
|
|
51
|
+
:questionnaires,
|
|
52
|
+
controller: "forms/questionnaires",
|
|
53
|
+
only: [:index, :show, :update]
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
Decidim::RestFull::Routing.async_resources(
|
|
57
|
+
self,
|
|
58
|
+
:questions,
|
|
59
|
+
controller: "forms/questions",
|
|
60
|
+
only: [:index, :show, :create, :update, :destroy]
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
Decidim::RestFull::Routing.async_resources(
|
|
64
|
+
self,
|
|
65
|
+
:answer_options,
|
|
66
|
+
controller: "forms/answer_options",
|
|
67
|
+
only: [:index, :create, :update, :destroy]
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
Decidim::RestFull::Routing.async_resources(
|
|
71
|
+
self,
|
|
72
|
+
:answers,
|
|
73
|
+
controller: "forms/answers",
|
|
74
|
+
only: [:index, :create]
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
resources :questionnaire_responses, only: [:show, :destroy],
|
|
78
|
+
controller: "/decidim/api/rest_full/forms/questionnaire_responses" do
|
|
79
|
+
member do
|
|
80
|
+
put "/", action: :update_forbidden
|
|
81
|
+
delete "sync", action: :destroy_sync
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
Decidim::RestFull::Routing.read_resources(
|
|
86
|
+
self,
|
|
87
|
+
:submission_requests,
|
|
88
|
+
controller: "forms/submission_requests",
|
|
89
|
+
only: [:show]
|
|
90
|
+
)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_object(:forms_validation_error_item) do
|
|
4
|
+
{
|
|
5
|
+
type: :object,
|
|
6
|
+
title: "Forms validation error",
|
|
7
|
+
properties: {
|
|
8
|
+
title: { type: :string },
|
|
9
|
+
code: { type: :string },
|
|
10
|
+
pointer: { type: :string, description: "JSON Pointer into the request body" },
|
|
11
|
+
question_id: { type: :string, nullable: true }
|
|
12
|
+
},
|
|
13
|
+
required: [:title, :code],
|
|
14
|
+
additionalProperties: false
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_object(:forms_validation_error_response) do
|
|
19
|
+
{
|
|
20
|
+
type: :object,
|
|
21
|
+
title: "Forms validation error response",
|
|
22
|
+
properties: {
|
|
23
|
+
meta: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:forms_locale_meta) },
|
|
24
|
+
errors: {
|
|
25
|
+
type: :array,
|
|
26
|
+
items: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:forms_validation_error_item) }
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
required: [:errors],
|
|
30
|
+
additionalProperties: false
|
|
31
|
+
}
|
|
32
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_object(:forms_locale_meta) do
|
|
4
|
+
{
|
|
5
|
+
type: :object,
|
|
6
|
+
title: "Forms locale metadata",
|
|
7
|
+
properties: {
|
|
8
|
+
locale: { type: :string, description: "Effective locale for projected strings" },
|
|
9
|
+
requested_locale: { type: :string, description: "Client-requested locale before fallback" },
|
|
10
|
+
fallback_from: { type: :string, nullable: true, description: "Locale downgraded from, if any" }
|
|
11
|
+
},
|
|
12
|
+
required: [:locale, :requested_locale],
|
|
13
|
+
additionalProperties: false
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_object(:forms_collection_meta) do
|
|
18
|
+
{
|
|
19
|
+
type: :object,
|
|
20
|
+
title: "Forms collection pagination and locale metadata",
|
|
21
|
+
properties: {
|
|
22
|
+
page: { type: :integer, minimum: 1 },
|
|
23
|
+
per_page: { type: :integer, minimum: 1, maximum: 100 },
|
|
24
|
+
has_more: {
|
|
25
|
+
type: :boolean,
|
|
26
|
+
description: "True when another page may exist (limit+1). Always true when order=rand."
|
|
27
|
+
},
|
|
28
|
+
next: { type: :string, nullable: true },
|
|
29
|
+
prev: { type: :string, nullable: true },
|
|
30
|
+
locale: { type: :string, description: "Effective locale for projected strings" },
|
|
31
|
+
requested_locale: { type: :string, description: "Client-requested locale before fallback" },
|
|
32
|
+
fallback_from: { type: :string, nullable: true, description: "Locale downgraded from, if any" }
|
|
33
|
+
},
|
|
34
|
+
required: [:page, :per_page, :has_more, :locale, :requested_locale],
|
|
35
|
+
additionalProperties: false
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_object(:forms_submission_policy_meta) do
|
|
40
|
+
{
|
|
41
|
+
type: :object,
|
|
42
|
+
title: "Questionnaire submission policy",
|
|
43
|
+
properties: {
|
|
44
|
+
allows_anonymous: { type: :boolean },
|
|
45
|
+
requires_participant_ip: { type: :boolean }
|
|
46
|
+
},
|
|
47
|
+
required: [:allows_anonymous, :requires_participant_ip],
|
|
48
|
+
additionalProperties: false
|
|
49
|
+
}
|
|
50
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_resource(:question) do
|
|
4
|
+
{
|
|
5
|
+
type: :object,
|
|
6
|
+
title: "Question",
|
|
7
|
+
description: "Authoring row for a single Decidim::Forms::Question (admin CRUD).",
|
|
8
|
+
properties: {
|
|
9
|
+
id: { type: :string },
|
|
10
|
+
type: { type: :string, enum: ["questions"] },
|
|
11
|
+
attributes: {
|
|
12
|
+
type: :object,
|
|
13
|
+
properties: {
|
|
14
|
+
position: { type: :integer },
|
|
15
|
+
mandatory: { type: :boolean },
|
|
16
|
+
question_type: { type: :string },
|
|
17
|
+
body: { type: :object, additionalProperties: true, description: "Translated fields (may include machine_translations)" },
|
|
18
|
+
description: { type: :object, additionalProperties: true, nullable: true },
|
|
19
|
+
max_choices: { type: :integer, nullable: true },
|
|
20
|
+
max_characters: { type: :integer, nullable: true }
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
relationships: {
|
|
24
|
+
type: :object,
|
|
25
|
+
properties: {
|
|
26
|
+
questionnaire: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("questionnaires", title: "Questionnaire")
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
required: [:id, :type]
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_resource(:answer_option) do
|
|
35
|
+
{
|
|
36
|
+
type: :object,
|
|
37
|
+
title: "Answer option",
|
|
38
|
+
description: "Selectable option for single/multiple choice questions.",
|
|
39
|
+
properties: {
|
|
40
|
+
id: { type: :string },
|
|
41
|
+
type: { type: :string, enum: ["answer_options"] },
|
|
42
|
+
attributes: {
|
|
43
|
+
type: :object,
|
|
44
|
+
properties: {
|
|
45
|
+
body: { type: :object, additionalProperties: { type: :string } }
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
relationships: {
|
|
49
|
+
type: :object,
|
|
50
|
+
properties: {
|
|
51
|
+
question: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("question", title: "Question")
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
required: [:id, :type]
|
|
56
|
+
}
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_resource(:answer) do
|
|
60
|
+
{
|
|
61
|
+
type: :object,
|
|
62
|
+
title: "Answer row",
|
|
63
|
+
description: "Stored Decidim::Forms::Answer (corpus listing, not the submission bundle).",
|
|
64
|
+
properties: {
|
|
65
|
+
id: { type: :string },
|
|
66
|
+
type: { type: :string, enum: ["answers"] },
|
|
67
|
+
attributes: {
|
|
68
|
+
type: :object,
|
|
69
|
+
properties: {
|
|
70
|
+
body: { type: :string, nullable: true },
|
|
71
|
+
question_id: { type: :string }
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
relationships: {
|
|
75
|
+
type: :object,
|
|
76
|
+
properties: {
|
|
77
|
+
questionnaire: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("questionnaires", title: "Questionnaire")
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
required: [:id, :type]
|
|
82
|
+
}
|
|
83
|
+
end
|