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
@@ -7,33 +7,98 @@ module Decidim
7
7
  include Decidim::Resourceable
8
8
  include Decidim::Forms::HasQuestionnaire
9
9
  include Decidim::HasComponent
10
+ include Decidim::FilterableResource
11
+ include Decidim::Publicable
12
+ include Decidim::Searchable
10
13
 
11
- #component_manifest_name "surveys"
14
+ # component_manifest_name "surveys"
12
15
 
13
16
  delegate :title, to: :questionnaire
17
+ delegate :description, to: :questionnaire
18
+ delegate :tos, to: :questionnaire
14
19
 
15
20
  validates :questionnaire, presence: true
16
21
 
17
- def clean_after_publish?
18
- component.settings.clean_after_publish?
22
+ scope :open, lambda {
23
+ where(allow_responses: true)
24
+ .where(starts_at: nil, ends_at: nil).or(
25
+ where("starts_at <= ? AND (ends_at IS NULL OR ends_at > ?)", Time.current, Time.current)
26
+ ).or(
27
+ where("ends_at > ? AND (starts_at IS NULL OR starts_at <= ?)", Time.current, Time.current)
28
+ )
29
+ }
30
+ scope :closed, lambda {
31
+ where(allow_responses: false).or(
32
+ where("starts_at > ?", Time.current).or(
33
+ where(ends_at: ...Time.current)
34
+ )
35
+ )
36
+ }
37
+ scope :published, -> { where.not(published_at: nil) }
38
+
39
+ scope_search_multi :with_any_state, [:open, :closed]
40
+
41
+ searchable_fields({
42
+ participatory_space: { component: :participatory_space },
43
+ A: :title,
44
+ D: :description,
45
+ datetime: :starts_at
46
+ },
47
+ index_on_create: ->(survey) { survey.visible? },
48
+ index_on_update: ->(survey) { survey.visible? })
49
+
50
+ def open?
51
+ return false if allow_responses.blank?
52
+ return true if time_indefinite?
53
+ return true if started_but_no_end?
54
+ return true if no_start_but_ends_later?
55
+
56
+ return within_time_range? if time_range_defined?
57
+
58
+ false
19
59
  end
20
60
 
21
- def starts_at
22
- component.settings.starts_at
61
+ def closed?
62
+ !open?
23
63
  end
24
64
 
25
- def ends_at
26
- component.settings.ends_at
65
+ def self.ransackable_scopes(_auth_object = nil)
66
+ [:with_any_state]
27
67
  end
28
68
 
29
- def open?
30
- return true if starts_at.blank? && ends_at.blank?
31
- return true if ends_at.blank? && starts_at.past?
32
- return true if starts_at.blank? && ends_at.future?
69
+ def self.ransackable_attributes(_auth_object = nil)
70
+ %w(ends_at starts_at allow_responses)
71
+ end
33
72
 
34
- return Time.zone.now.between?(starts_at, ends_at) if starts_at.present? && ends_at.present?
73
+ def self.log_presenter_class_for(_log)
74
+ Decidim::Surveys::AdminLog::SurveyPresenter
75
+ end
35
76
 
36
- false
77
+ # Public: Overrides the `allow_resource_permissions?` Resourceable concern method.
78
+ def allow_resource_permissions?
79
+ true
80
+ end
81
+
82
+ private
83
+
84
+ def time_indefinite?
85
+ starts_at.blank? && ends_at.blank?
86
+ end
87
+
88
+ def started_but_no_end?
89
+ ends_at.blank? && starts_at.past?
90
+ end
91
+
92
+ def no_start_but_ends_later?
93
+ starts_at.blank? && ends_at.future?
94
+ end
95
+
96
+ def time_range_defined?
97
+ starts_at.present? && ends_at.present?
98
+ end
99
+
100
+ def within_time_range?
101
+ Time.zone.now.between?(starts_at, ends_at)
37
102
  end
38
103
  end
39
104
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AddOnlyFormsAttributes
4
+ Deface::Override.new(
5
+ virtual_path: "decidim/surveys/admin/settings/surveys/_form",
6
+ name: "add_only_form_attributes",
7
+ insert_after: "div.row.column:contains('form.check_box :allow_unregistered')",
8
+ text: <<~ERB
9
+ <div class="row column">
10
+ <%= form.check_box :allow_multiple_responses, label: t("form.settings.allow_multiple_responses", scope: "decidim.only_forms.admin"), aria: { label: :allow_multiple_responses } %>
11
+ </div>
12
+ <div class="row column">
13
+ <%= form.text_field :admin_email, label: t("form.settings.admin_email", scope: "decidim.only_forms.admin"), aria: { label: :admin_email }, help_text: t("form.settings.admin_email_help", scope: "decidim.only_forms.admin") %>
14
+ </div>
15
+ ERB
16
+ )
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ReplaceAnnouncementDisplay
4
+ Deface::Override.new(
5
+ virtual_path: "decidim/forms/questionnaires/show",
6
+ name: "announcements_display",
7
+ replace: "erb[loud]:contains('render partial: \"decidim/shared/component_announcement\"')",
8
+ text: <<~ERB
9
+ <%= render partial: "decidim/shared/component_announcement" if ["surveys", "only_forms"].include?(current_component.manifest_name) %>
10
+ ERB
11
+ )
12
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module OnlyForms
5
+ class AdminEmailAllowlist
6
+ def initialize(email, participatory_space)
7
+ @email = email.to_s.strip
8
+ @space = participatory_space
9
+ end
10
+
11
+ def valid_setting?
12
+ @email.blank? || allowed_recipient?
13
+ end
14
+
15
+ def allowed_recipient?
16
+ formatted? && matching_admins.exists?
17
+ end
18
+
19
+ private
20
+
21
+ def formatted?
22
+ @email.match?(URI::MailTo::EMAIL_REGEXP)
23
+ end
24
+
25
+ def matching_admins
26
+ return Decidim::User.none if @space.blank?
27
+
28
+ @space.admins.where("LOWER(email) = ?", @email.downcase)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module OnlyForms
5
+ class AdminSubmissionNotification
6
+ def self.handle(_event_name, data)
7
+ new(data).deliver
8
+ end
9
+
10
+ def initialize(data)
11
+ @questionnaire = data[:resource]
12
+ @session_token = data.dig(:extra, :session_token)
13
+ @event_author = data.dig(:extra, :event_author)
14
+ end
15
+
16
+ def deliver
17
+ return unless @questionnaire.is_a?(::Decidim::Forms::Questionnaire)
18
+ return unless notify?
19
+
20
+ AdminSubmissionMailer.notify(admin_email, @questionnaire, @response_ids).deliver_later
21
+ end
22
+
23
+ private
24
+
25
+ def response_ids
26
+ @response_ids = collection.where(session_token: @session_token).map(&:id)
27
+ end
28
+
29
+ def notify?
30
+ only_forms? && recipient_allowed? && response_ids.present?
31
+ end
32
+
33
+ def only_forms?
34
+ survey? && component&.manifest_name.to_s == "only_forms"
35
+ end
36
+
37
+ def survey?
38
+ questionnaire_for.instance_of?(::Decidim::Surveys::Survey)
39
+ end
40
+
41
+ def recipient_allowed?
42
+ AdminEmailAllowlist.new(admin_email, component&.participatory_space).allowed_recipient?
43
+ end
44
+
45
+ def admin_email
46
+ questionnaire_for&.admin_email.to_s.strip
47
+ end
48
+
49
+ def component
50
+ questionnaire_for.try(:component)
51
+ end
52
+
53
+ def questionnaire_for
54
+ @questionnaire.questionnaire_for
55
+ end
56
+
57
+ def collection
58
+ return @questionnaire.responses if @questionnaire.respond_to?(:responses)
59
+
60
+ @questionnaire.responses
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,12 @@
1
+ <p>
2
+ <%= t(".intro", questionnaire_title: @questionnaire_title, participatory_space: @participatory_space_title) %>
3
+ </p>
4
+ <p>
5
+ <%= t(".submitted_by", author: @author_label) %>
6
+ </p>
7
+ <dl>
8
+ <% @responses.each do |response| %>
9
+ <dt><%= response.question %></dt>
10
+ <dd><%= response.body %></dd>
11
+ <% end %>
12
+ </dl>