decidim-only_forms 0.1.0 → 0.2.0

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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +59 -2
  3. data/Rakefile +46 -36
  4. data/app/commands/decidim/forms/command.rb +1 -7
  5. data/app/commands/decidim/forms/response_questionnaire.rb +158 -0
  6. data/app/controllers/decidim/forms/concerns/has_questionnaire.rb +49 -36
  7. data/app/mailers/decidim/only_forms/admin_submission_mailer.rb +77 -0
  8. data/app/models/decidim/surveys/survey.rb +78 -13
  9. data/app/overrides/add_only_forms_attributes.rb +17 -0
  10. data/app/overrides/replace_announcement_display.rb +12 -0
  11. data/app/services/decidim/only_forms/admin_email_allowlist.rb +32 -0
  12. data/app/services/decidim/only_forms/admin_submission_notification.rb +64 -0
  13. data/app/views/decidim/only_forms/admin_submission_mailer/notify.html.erb +12 -0
  14. data/config/initializers/doorkeeper.rb +544 -0
  15. data/config/locales/doorkeeper.en.yml +155 -0
  16. data/config/locales/en.yml +13 -3
  17. data/config/locales/fr.yml +11 -3
  18. data/config/locales/pt.yml +11 -3
  19. data/lib/decidim/only_forms/component.rb +28 -15
  20. data/lib/decidim/only_forms/engine.rb +16 -2
  21. data/lib/decidim/only_forms/overrides/only_form_settings_form.rb +30 -0
  22. data/lib/decidim/only_forms/overrides/update_only_form_settings.rb +31 -0
  23. data/lib/decidim/only_forms/test/factories.rb +1 -1
  24. data/lib/decidim/only_forms/version.rb +3 -3
  25. data/lib/decidim/only_forms.rb +2 -0
  26. data/lib/tasks/decidim/only_forms/only_forms_upgrade.rake +13 -0
  27. data/lib/tasks/decidim/only_forms/only_forms_webpacker.rake +58 -0
  28. metadata +39 -13
  29. data/app/commands/decidim/forms/answer_questionnaire.rb +0 -112
@@ -1,112 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Decidim
4
- module Forms
5
- # This command is executed when the user answers a Questionnaire.
6
- class AnswerQuestionnaire < Decidim::Forms::Command
7
- include ::Decidim::MultipleAttachmentsMethods
8
-
9
- # Initializes a AnswerQuestionnaire Command.
10
- #
11
- # form - The form from which to get the data.
12
- # questionnaire - The current instance of the questionnaire to be answered.
13
- def initialize(form, current_user, questionnaire)
14
- @form = form
15
- @current_user = current_user
16
- @questionnaire = questionnaire
17
- end
18
-
19
- # Answers a questionnaire if it is valid
20
- #
21
- # Broadcasts :ok if successful, :invalid otherwise.
22
- def call
23
- return broadcast(:invalid) if @form.invalid? || user_already_answered?
24
-
25
- answer_questionnaire
26
-
27
- if @errors
28
- reset_form_attachments
29
- broadcast(:invalid)
30
- else
31
- broadcast(:ok)
32
- end
33
- end
34
-
35
- attr_reader :form, :questionnaire, :current_user
36
-
37
- private
38
-
39
- # This method will add an error to the `add_documents` field only if there's
40
- # any error in any other field or an error in another answer in the
41
- # questionnaire. This is needed because when the form has
42
- # an error, the attachments are lost, so we need a way to inform the user
43
- # of this problem.
44
- def reset_form_attachments
45
- @form.responses.each do |answer|
46
- answer.errors.add(:add_documents, :needs_to_be_reattached) if answer.has_attachments? || answer.has_error_in_attachments?
47
- end
48
- end
49
-
50
- def answer_questionnaire
51
- @main_form = @form
52
- @errors = nil
53
-
54
- Answer.transaction(requires_new: true) do
55
- form.responses_by_step.flatten.select(&:display_conditions_fulfilled?).each do |form_answer|
56
- answer = Answer.new(
57
- user: @current_user,
58
- questionnaire: @questionnaire,
59
- question: form_answer.question,
60
- body: form_answer.body,
61
- session_token: form.context.session_token,
62
- ip_hash: form.context.ip_hash
63
- )
64
-
65
- form_answer.selected_choices.each do |choice|
66
- answer.choices.build(
67
- body: choice.body,
68
- custom_body: choice.custom_body,
69
- decidim_answer_option_id: choice.answer_option_id,
70
- decidim_question_matrix_row_id: choice.matrix_row_id,
71
- position: choice.position
72
- )
73
- end
74
-
75
- answer.save!
76
-
77
- next unless form_answer.question.has_attachments?
78
-
79
- # The attachments module expects `@form` to be the form with the
80
- # attachments
81
- @form = form_answer
82
- @attached_to = answer
83
-
84
- build_attachments
85
-
86
- if attachments_invalid?
87
- @errors = true
88
- next
89
- end
90
-
91
- create_attachments if process_attachments?
92
- document_cleanup!
93
- end
94
-
95
- @form = @main_form
96
- raise ActiveRecord::Rollback if @errors
97
- end
98
- end
99
-
100
- def user_already_answered?
101
- return false if allow_multiple_answers?
102
-
103
- questionnaire.answered_by?(current_user || form.context.session_token)
104
- end
105
-
106
- def allow_multiple_answers?
107
- return current_settings.allow_multiple_answers if current_settings.respond_to?("allow_multiple_answers")
108
- end
109
-
110
- end
111
- end
112
- end