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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 138bf45c73bc488b0951ad0c232049f9b6338d8c8489f394c1bea7b516746375
4
- data.tar.gz: 79999b942d0290ed8edfb86f77bc57058eb3bbcf5a2e7d2d77fd26bb6612b32b
3
+ metadata.gz: 21e71ab8ede3bd13dfad43953c26c02235f270c52e298a8d1278a0f138c12787
4
+ data.tar.gz: 4821e3649cbcd8021e4c0be8e81435c48e325c8722d586746fda8efd0becac80
5
5
  SHA512:
6
- metadata.gz: c094990f944ad6272dceceb181826b74b7924d28b531f4809ccbc4b35d9e34df341a29f02e442e02d240d9a17eaeeef13dad5c59f7555a78fd1cc22227be2c21
7
- data.tar.gz: bb770c4d8d2a6ad90c459f96ed827d5d3cc98d4abcbeb1d2157f525c04b2ec1cc41407edb4c9b610f1c4d8ae2e965ab80f1ecf943dda526a5bd5bdf4b95b55b2
6
+ metadata.gz: 62c989397692e0f5291a98524986333bb29c18611b061451b73c19aeb1309338a485a94287876c11049a4380ed4123ca72854d4cb58a1d012afb652a1b1122fd
7
+ data.tar.gz: 0dc46ca3253feaf5b21182bdfe7a997af126192896bcb6344985379bd9aa1482915b72cae01c9f279215f0056f06ddf7dc71320f54389cfe67ec657a70173f17
data/README.md CHANGED
@@ -18,7 +18,12 @@ Component to create forms in a participatory space, sponsored by the [Mkutano Co
18
18
 
19
19
  ## Usage
20
20
 
21
- OnlyForms will be available as a Component for a Participatory Space.
21
+ OnlyForms will be available as a Component for a Participatory Space. You can optionally email an organization or space administrator after each submission; see [Admin notification email](#admin-notification-email).
22
+
23
+ ## Compatibility
24
+
25
+ - Decidim: **>= 0.26, < 0.33**
26
+ - Ruby: **>= 3.2.2**
22
27
 
23
28
  ## Installation
24
29
 
@@ -36,9 +41,44 @@ bundle exec rails decidim_only_forms:install:migrations
36
41
  bundle exec rails db:migrate
37
42
  ```
38
43
 
44
+ Then in the admin panel, add the **Forms** component (manifest: `only_forms`) to your participatory space.
45
+
46
+ ## Configuration notes
47
+
48
+ These notes are for a space administrator setting up the **Forms** component.
49
+
50
+ - **Private-only**: set `Allow unregistered` to **false** and use a private participatory space (private users).
51
+ - **Multiple submissions**: in step settings, keep `Allow multiple answers` as **true** (default for this component).
52
+ - **Admin export**: answers are exportable via the component export `survey_user_answers` (CSV/JSON/Excel).
53
+
54
+ ### Admin notification email
55
+
56
+ After each successful submission, the component can send **one** email with the answers (same question and answer rendering as the admin answers view, including textareas, multiple options, and matrix questions). Registered submitters are shown by name; unregistered submitters are labelled as unregistered.
57
+
58
+ In the component **global** settings, set **Admin notification email** to:
59
+
60
+ - an **organization administrator** of this organization, or
61
+ - a **space administrator** of this participatory space (admin role on the process or assembly)
62
+
63
+ Leave the field **blank** to disable notifications. Any other address is rejected when the component is saved (moderators, evaluators, ordinary participants, and admins of other spaces or organizations are not accepted).
64
+
39
65
  ## Testing
66
+
67
+ GitLab CI runs `bin/ci-setup` then `unset DATABASE_URL && RAILS_ENV=test bundle exec rspec`.
68
+
69
+ Local CI parity (same images as GitLab: `ruby:3.2.2` + PostGIS + Redis):
70
+
71
+ ```bash
72
+ docker compose -f docker-compose.ci.yml run --rm rspec
40
73
  ```
41
- bundle exec rake test_app
74
+
75
+ Dev container:
76
+
77
+ ```bash
78
+ docker compose up -d
79
+ docker compose exec -T only_forms bash -lc "cd /home/module && unset DATABASE_URL && bundle install"
80
+ docker compose exec -T only_forms bash -lc "cd /home/module && unset DATABASE_URL && bundle exec rake test_app"
81
+ docker compose exec -T only_forms bash -lc "cd /home/module && unset DATABASE_URL && RAILS_ENV=test bundle exec rspec"
42
82
  ```
43
83
 
44
84
  ## Local development
@@ -53,6 +93,23 @@ docker-compose down -v --remove-orphans
53
93
  docker-compose up -d
54
94
  ```
55
95
 
96
+ ### Running tests in Docker
97
+
98
+ Prefer CI parity:
99
+
100
+ ```bash
101
+ docker compose -f docker-compose.ci.yml run --rm rspec
102
+ ```
103
+
104
+ Or the dev container (`unset DATABASE_URL` so specs hit `decidim_test`, not compose `DATABASE_URL`):
105
+
106
+ ```bash
107
+ docker compose up -d
108
+ docker compose exec -T only_forms bash -lc "cd /home/module && unset DATABASE_URL && bundle install"
109
+ docker compose exec -T only_forms bash -lc "cd /home/module && unset DATABASE_URL && bundle exec rake test_app"
110
+ docker compose exec -T only_forms bash -lc "cd /home/module && unset DATABASE_URL && RAILS_ENV=test bundle exec rspec"
111
+ ```
112
+
56
113
  Once created, you access the decidim container
57
114
  ```
58
115
  # Get the id of the decidim dev container
data/Rakefile CHANGED
@@ -1,6 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
4
+ require "yaml"
5
+
3
6
  require "decidim/dev/common_rake"
7
+ require "rake"
8
+ require "rake/file_utils"
9
+
10
+ # Stable dummy-app DB name; do not derive from `Dir.pwd` (Docker mounts `/home/module`).
11
+ def base_app_name
12
+ "decidim_only_forms"
13
+ end
4
14
 
5
15
  def install_module(path)
6
16
  Dir.chdir(path) do
@@ -16,52 +26,55 @@ end
16
26
 
17
27
  desc "Prepare for testing"
18
28
  task :prepare_tests do
19
- system("bundle add doorkeeper")
20
- system("bundle exec rails generate doorkeeper:install")
21
- system("bundle exec rails generate doorkeeper:migration")
22
29
  # Remove previous existing db, and recreate one.
23
30
  disable_docker_compose = ENV.fetch("DISABLED_DOCKER_COMPOSE", "false") == "true"
24
31
  unless disable_docker_compose
25
- system("sudo docker-compose down -v")
26
- system("sudo docker-compose up -d --remove-orphans")
32
+ system("docker-compose -f docker-compose.yml down -v --remove-orphans")
33
+ system("docker-compose -f docker-compose.yml up -d ")
27
34
  end
28
- ENV["RAILS_ENV"] = "test"
29
- databaseYml = {
30
- "test" => {
31
- "adapter" => "postgresql",
32
- "encoding" => "unicode",
33
- "host" => ENV.fetch("DATABASE_HOST", "localhost"),
34
- "port" => ENV.fetch("DATABASE_PORT", "5432").to_i,
35
- "username" => ENV.fetch("DATABASE_USERNAME", "decidim"),
36
- "password" => ENV.fetch("DATABASE_PASSWORD", "TEST-baeGhi4Ohtahcee5eejoaxaiwaezaiGo"),
37
- "database" => "decidim_test"
38
- }
35
+ ENV["RAILS_ENV"] = "development"
36
+ common_db_config = {
37
+ "adapter" => "postgresql",
38
+ "encoding" => "unicode",
39
+ "host" => ENV.fetch("DATABASE_HOST", "only-forms-pg"),
40
+ "port" => ENV.fetch("DATABASE_PORT", "5432").to_i,
41
+ "username" => ENV.fetch("DATABASE_USERNAME", "decidim"),
42
+ "password" => ENV.fetch("DATABASE_PASSWORD", "pleaseChangeMe"),
43
+ "database" => "#{base_app_name}_test_app"
44
+ }
45
+
46
+ database_yml = {
47
+ "test" => common_db_config,
48
+ "development" => common_db_config
39
49
  }
40
- config_file = File.expand_path("spec/dummy/config/database.yml", __dir__)
41
- File.open(config_file, "w") { |f| YAML.dump(databaseYml, f) }
42
- Dir.chdir("spec/dummy") do
43
- system("bundle exec rails db:migrate")
44
- end
50
+
51
+ config_file = File.expand_path("spec/decidim_dummy_app/config/database.yml", __dir__)
52
+ File.open(config_file, "w") { |f| YAML.dump(database_yml, f) }
53
+ Dir.chdir("spec/decidim_dummy_app") do
54
+ system("bundle exec rails db:drop")
55
+ system("bundle exec rails db:create")
56
+ system("bundle exec rails db:migrate")
57
+ end
45
58
  end
46
59
 
47
60
  desc "Generates a dummy app for testing"
48
61
  task :test_app do
49
62
  Bundler.with_original_env do
50
63
  generate_decidim_app(
51
- "spec/dummy",
52
- "--app_name",
53
- "decidim_test",
54
- "--path",
55
- "../..",
56
- "--skip_spring",
57
- "--demo",
58
- "--force_ssl",
59
- "false",
60
- "--locales",
61
- "en,fr,es"
64
+ "spec/decidim_dummy_app",
65
+ "--app_name",
66
+ "decidim_test",
67
+ "--path",
68
+ "../..",
69
+ "--skip_spring",
70
+ "--demo",
71
+ "--force_ssl",
72
+ "false",
73
+ "--locales",
74
+ "en,fr,es"
62
75
  )
63
76
  end
64
- install_module("spec/dummy")
77
+ install_module("spec/decidim_dummy_app")
65
78
  Rake::Task["prepare_tests"].invoke
66
79
  end
67
80
 
@@ -79,9 +92,6 @@ task :development_app do
79
92
  )
80
93
  end
81
94
 
82
- system("bin/rails generate doorkeeper:install")
83
- system("bin/rails generate doorkeeper:migration")
84
-
85
95
  install_module("development_app")
86
96
  seed_db("development_app")
87
97
  end
@@ -2,13 +2,7 @@
2
2
 
3
3
  module Decidim
4
4
  module Forms
5
- # maintains compatibility with v0.26 which uses Rectify
6
- if defined? ::Decidim::Command
7
- class Command < ::Decidim::Command
8
- end
9
- else
10
- class Command < ::Rectify::Command
11
- end
5
+ class Command < ::Decidim::Command
12
6
  end
13
7
  end
14
8
  end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Forms
5
+ # This command is executed when the user responses a Questionnaire.
6
+ class ResponseQuestionnaire < Decidim::Forms::Command
7
+ delegate :current_user, to: :form
8
+ include ::Decidim::MultipleAttachmentsMethods
9
+
10
+ # Initializes a ResponseQuestionnaire Command.
11
+ #
12
+ # form - The form from which to get the data.
13
+ # questionnaire - The current instance of the questionnaire to be responded.
14
+ def initialize(form, questionnaire, allow_editing_responses: false)
15
+ @form = form
16
+ @questionnaire = questionnaire
17
+ @allow_editing_responses = allow_editing_responses
18
+ end
19
+
20
+ # Responds a questionnaire if it is valid
21
+ #
22
+ # Broadcasts :ok if successful, :invalid otherwise.
23
+ def call
24
+ return broadcast(:invalid) if @form.invalid? || (user_already_responded? && !allow_editing_responses)
25
+
26
+ with_events do
27
+ clear_responses! if allow_editing_responses
28
+ response_questionnaire
29
+ end
30
+
31
+ if @errors
32
+ reset_form_attachments
33
+ broadcast(:invalid)
34
+ else
35
+ broadcast(:ok)
36
+ end
37
+ end
38
+
39
+ attr_reader :form, :questionnaire, :allow_editing_responses
40
+
41
+ private
42
+
43
+ def event_arguments
44
+ {
45
+ resource: questionnaire,
46
+ extra: {
47
+ session_token: form.context.session_token,
48
+ questionnaire:,
49
+ event_author: current_user
50
+ }
51
+ }
52
+ end
53
+
54
+ # This method will add an error to the `add_documents` field only if there is
55
+ # any error in any other field or an error in another response in the
56
+ # questionnaire. This is needed because when the form has
57
+ # an error, the attachments are lost, so we need a way to inform the user
58
+ # of this problem.
59
+ def reset_form_attachments
60
+ @form.responses.each do |response|
61
+ response.errors.add(:add_attachments, :needs_to_be_reattached) if response.has_attachments? || response.has_error_in_attachments?
62
+ end
63
+ end
64
+
65
+ def build_choices(response, form_response)
66
+ use_position = form_response.sorting?
67
+
68
+ form_response.selected_choices.each_with_index do |choice, idx|
69
+ choice_position = use_position ? choice.position.presence || idx : choice.position
70
+ response.choices.build(
71
+ body: choice.body,
72
+ custom_body: choice.custom_body,
73
+ decidim_response_option_id: choice.response_option_id,
74
+ decidim_question_matrix_row_id: choice.matrix_row_id,
75
+ position: choice_position
76
+ )
77
+ end
78
+ end
79
+
80
+ def clear_responses!
81
+ Response.where(questionnaire:, user: current_user, session_token: form.context.session_token, ip_hash: form.context.ip_hash).destroy_all
82
+ end
83
+
84
+ def response_questionnaire
85
+ @main_form = @form
86
+ @errors = nil
87
+
88
+ Response.transaction(requires_new: true) do
89
+ form.responses_by_step.flatten.select(&:display_conditions_fulfilled?).each do |form_response|
90
+ response = Response.new(
91
+ user: current_user,
92
+ questionnaire: @questionnaire,
93
+ question: form_response.question,
94
+ body: form_response.body,
95
+ session_token: form.context.session_token,
96
+ ip_hash: form.context.ip_hash
97
+ )
98
+
99
+ build_choices(response, form_response)
100
+
101
+ response.save!
102
+
103
+ next unless form_response.question.has_attachments?
104
+
105
+ # The attachments module expects `@form` to be the form with the
106
+ # attachments
107
+ @form = form_response
108
+ @attached_to = response
109
+
110
+ build_attachments
111
+
112
+ if attachments_invalid?
113
+ @errors = true
114
+ next
115
+ end
116
+
117
+ create_attachments if process_attachments?
118
+ attachment_cleanup!
119
+ end
120
+
121
+ @form = @main_form
122
+ raise ActiveRecord::Rollback if @errors
123
+ end
124
+ end
125
+
126
+ def user_already_responded?
127
+ return false if allow_multiple_responses?
128
+
129
+ questionnaire.responded_by?(current_user || form.context.session_token)
130
+ end
131
+
132
+ def allow_multiple_responses?
133
+ questionnaire.questionnaire_for.allow_multiple_responses if questionnaire.questionnaire_for.respond_to?("allow_multiple_responses")
134
+ end
135
+
136
+ def publish_responded_event
137
+ ActiveSupport::Notifications.publish(
138
+ "decidim.forms.response_questionnaire:after",
139
+ resource: questionnaire,
140
+ extra: event_extra
141
+ )
142
+ end
143
+
144
+ def event_extra
145
+ {
146
+ session_token: form.context.session_token,
147
+ questionnaire:,
148
+ event_author: current_user,
149
+ response_ids: created_response_ids
150
+ }
151
+ end
152
+
153
+ def created_response_ids
154
+ @created_response_ids ||= []
155
+ end
156
+ end
157
+ end
158
+ end
@@ -16,76 +16,81 @@ module Decidim
16
16
  helper Decidim::Forms::ApplicationHelper
17
17
  include FormFactory
18
18
 
19
- helper_method :questionnaire_for, :questionnaire, :allow_answers?, :visitor_can_answer?, :visitor_already_answered?, :update_url, :form_path
19
+ helper_method :questionnaire_for, :questionnaire, :allow_responses?, :visitor_can_respond?, :visitor_already_responded?, :update_url, :visitor_can_edit_responses?,
20
+ :form_path
20
21
 
21
22
  invisible_captcha on_spam: :spam_detected
22
23
 
23
24
  def show
24
25
  @form = form(Decidim::Forms::QuestionnaireForm).from_model(questionnaire)
25
- render template: "decidim/forms/questionnaires/show"
26
+ render template:
26
27
  end
27
28
 
28
- def answer
29
- enforce_permission_to_answer_questionnaire
29
+ # i18n-tasks-use t("decidim.forms.questionnaires.response.success")
30
+ # i18n-tasks-use t("decidim.forms.questionnaires.response.invalid")
31
+ def respond
32
+ enforce_permission_to_respond_questionnaire
30
33
 
31
- @form = form(Decidim::Forms::QuestionnaireForm).from_params(params, session_token: session_token, ip_hash: ip_hash)
32
- Decidim::Forms::AnswerQuestionnaire.call(@form, current_user, questionnaire) do
34
+ @form = form(Decidim::Forms::QuestionnaireForm).from_params(params, session_token:, ip_hash:)
35
+
36
+ Decidim::Forms::ResponseQuestionnaire.call(@form, questionnaire, allow_editing_responses: allow_editing_responses?) do
33
37
  on(:ok) do
34
- # i18n-tasks-use t("decidim.forms.questionnaires.answer.success")
35
- flash[:notice] = I18n.t("answer.success", scope: i18n_flashes_scope)
36
- redirect_to after_answer_path
38
+ flash[:notice] = I18n.t("response.success", scope: i18n_flashes_scope)
39
+ redirect_to after_response_path
37
40
  end
38
41
 
39
42
  on(:invalid) do
40
- # i18n-tasks-use t("decidim.forms.questionnaires.answer.invalid")
41
- flash.now[:alert] = I18n.t("answer.invalid", scope: i18n_flashes_scope)
42
- render template: "decidim/forms/questionnaires/show"
43
+ flash.now[:alert] = I18n.t("response.invalid", scope: i18n_flashes_scope)
44
+ render template:, status: :unprocessable_content
43
45
  end
44
46
  end
45
47
  end
46
48
 
49
+ def template
50
+ "decidim/forms/questionnaires/show"
51
+ end
52
+
47
53
  # Public: Method to be implemented at the controller. You need to
48
- # return true if the questionnaire can receive answers
49
- def allow_answers?
50
- raise "#{self.class.name} is expected to implement #allow_answers?"
54
+ # return true if the questionnaire can receive responses
55
+ def allow_responses?
56
+ raise "#{self.class.name} is expected to implement #allow_responses?"
51
57
  end
52
58
 
53
59
  # Public: Method to be implemented at the controller if needed. You need to
54
- # return true if the questionnaire can receive answers by unregistered users
60
+ # return true if the questionnaire can receive responses by unregistered users
55
61
  def allow_unregistered?
56
62
  false
57
63
  end
58
64
 
59
- # Public: return true if the current user (if logged) can answer the questionnaire
60
- def visitor_can_answer?
65
+ # Public: return true if the current user (if logged) can response the questionnaire
66
+ def visitor_can_respond?
61
67
  current_user || allow_unregistered?
62
68
  end
63
69
 
64
- # Public: return false if survey allow multiple answers
65
- # if not, return true if the current user (or session visitor) already answered the questionnaire
66
- def visitor_already_answered?
67
- return false if allow_multiple_answers?
70
+ # Public: return true if the current user (or session visitor) can respond the questionnaire
71
+ def visitor_already_responded?
72
+ return false if allow_multiple_responses?
68
73
 
69
- questionnaire.answered_by?(current_user || tokenize(session[:session_id]))
74
+ questionnaire.responded_by?(current_user || tokenize(session[:session_id]))
70
75
  end
71
76
 
72
77
  # Public: Returns a String or Object that will be passed to `redirect_to` after
73
- # answering the questionnaire. By default it redirects to the questionnaire_for.
78
+ # responding the questionnaire. By default it redirects to the questionnaire_for.
74
79
  #
75
80
  # It can be redefined at controller level if you need to redirect elsewhere.
76
- def after_answer_path
81
+ def after_response_path
77
82
  questionnaire_for
78
83
  end
79
84
 
80
85
  # You can implement this method in your controller to change the URL
81
86
  # where the questionnaire will be submitted.
82
87
  def update_url
83
- url_for([questionnaire_for, { action: :answer }])
88
+ url_for([questionnaire_for, { action: :respond }])
84
89
  end
85
90
 
86
91
  # Points to the shortest path accessing the current form. This will be
87
92
  # used to detect whether a user is leaving the form with some partial
88
- # answers, so that we can warn them.
93
+ # responses, so that we can warn them.
89
94
  #
90
95
  # Overwrite this method at the controller.
91
96
  def form_path
@@ -100,8 +105,16 @@ module Decidim
100
105
 
101
106
  private
102
107
 
103
- def allow_multiple_answers?
104
- return current_settings.allow_multiple_answers if current_settings.respond_to?("allow_multiple_answers")
108
+ def allow_multiple_responses?
109
+ questionnaire.questionnaire_for.allow_multiple_responses if questionnaire.questionnaire_for.respond_to?("allow_multiple_responses")
110
+ end
111
+
112
+ def allow_editing_responses?
113
+ false
114
+ end
115
+
116
+ def visitor_can_edit_responses?
117
+ current_user.present? && questionnaire_for.try(:allow_editing_responses?)
105
118
  end
106
119
 
107
120
  def i18n_flashes_scope
@@ -109,22 +122,22 @@ module Decidim
109
122
  end
110
123
 
111
124
  def questionnaire
112
- @questionnaire ||= Questionnaire.includes(questions: :answer_options).find_by(questionnaire_for: questionnaire_for)
125
+ @questionnaire ||= Questionnaire.includes(questions: :response_options).find_by(questionnaire_for:)
113
126
  end
114
127
 
115
128
  def spam_detected
116
- enforce_permission_to_answer_questionnaire
129
+ enforce_permission_to_respond_questionnaire
117
130
 
118
131
  @form = form(Decidim::Forms::QuestionnaireForm).from_params(params)
119
132
 
120
- flash.now[:alert] = I18n.t("answer.spam_detected", scope: i18n_flashes_scope)
121
- render template: "decidim/forms/questionnaires/show"
133
+ flash.now[:alert] = I18n.t("response.spam_detected", scope: i18n_flashes_scope)
134
+ render template:
122
135
  end
123
136
 
124
137
  # You can implement this method in your controller to change the
125
138
  # enforce_permission_to arguments.
126
- def enforce_permission_to_answer_questionnaire
127
- enforce_permission_to :answer, :questionnaire
139
+ def enforce_permission_to_respond_questionnaire
140
+ enforce_permission_to :respond, :questionnaire
128
141
  end
129
142
 
130
143
  def ip_hash
@@ -144,7 +157,7 @@ module Decidim
144
157
  end
145
158
 
146
159
  def tokenize(id, length: 10)
147
- tokenizer = Decidim::Tokenizer.new(salt: questionnaire.salt || questionnaire.id, length: length)
160
+ tokenizer = Decidim::Tokenizer.new(salt: questionnaire.salt || questionnaire.id, length:)
148
161
  tokenizer.int_digest(id).to_s
149
162
  end
150
163
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module OnlyForms
5
+ class AdminSubmissionMailer < Decidim::ApplicationMailer
6
+ include TranslatableAttributes
7
+
8
+ def notify(email, questionnaire, response_ids)
9
+ @questionnaire = questionnaire
10
+ records = responses_for(questionnaire, response_ids)
11
+ @author_user = records.first&.user
12
+ @responses = presented_responses(records)
13
+ @organization = organization_for(questionnaire)
14
+ deliver_notification(email)
15
+ end
16
+
17
+ private
18
+
19
+ def deliver_notification(email)
20
+ I18n.with_locale(@organization.default_locale) do
21
+ assign_titles
22
+ mail(to: email, subject: subject_for)
23
+ end
24
+ end
25
+
26
+ def assign_titles
27
+ @questionnaire_title = translated_attribute(@questionnaire.title)
28
+ @participatory_space_title = translated_attribute(participatory_space.title)
29
+ @author_label = author_label
30
+ end
31
+
32
+ def subject_for
33
+ I18n.t("notify.subject", scope: i18n_scope, questionnaire_title: @questionnaire_title)
34
+ end
35
+
36
+ def i18n_scope
37
+ "decidim.only_forms.admin_submission_mailer"
38
+ end
39
+
40
+ def organization_for(questionnaire)
41
+ questionnaire.questionnaire_for.component.organization
42
+ end
43
+
44
+ def responses_for(questionnaire, response_ids)
45
+ records_for(questionnaire)
46
+ .where(id: response_ids)
47
+ .joins(:question)
48
+ .order("decidim_forms_questions.position ASC")
49
+ end
50
+
51
+ def records_for(questionnaire)
52
+ return questionnaire.responses if questionnaire.respond_to?(:responses)
53
+
54
+ questionnaire.responses
55
+ end
56
+
57
+ def presented_responses(records)
58
+ records.map { |response| Decidim::Forms::Admin::QuestionnaireResponsePresenter.new(response:) }
59
+ end
60
+
61
+ def participatory_space
62
+ @questionnaire.questionnaire_for.component.participatory_space
63
+ end
64
+
65
+ def author_label
66
+ user = @author_user
67
+ return unregistered_label if user.blank?
68
+
69
+ "#{user.name} (#{user.email})"
70
+ end
71
+
72
+ def unregistered_label
73
+ I18n.t("notify.unregistered", scope: i18n_scope)
74
+ end
75
+ end
76
+ end
77
+ end