decidim-decidim_awesome 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE-AGPLv3.txt +661 -0
  3. data/README.md +307 -0
  4. data/Rakefile +51 -0
  5. data/app/assets/config/decidim_admin_decidim_awesome_manifest.css +3 -0
  6. data/app/assets/config/decidim_admin_decidim_awesome_manifest.js +1 -0
  7. data/app/assets/config/decidim_decidim_awesome_manifest.css +5 -0
  8. data/app/assets/config/decidim_decidim_awesome_manifest.js +2 -0
  9. data/app/assets/images/decidim/decidim_awesome/loading.gif +0 -0
  10. data/app/assets/images/decidim/decidim_awesome/platoniq-logo.png +0 -0
  11. data/app/assets/javascripts/decidim/decidim_awesome/admin.js +2 -0
  12. data/app/assets/javascripts/decidim/decidim_awesome/admin/constraints.js.es6 +54 -0
  13. data/app/assets/javascripts/decidim/decidim_awesome/application.js +4 -0
  14. data/app/assets/javascripts/decidim/decidim_awesome/awesome_map/api_fetcher.js.es6 +38 -0
  15. data/app/assets/javascripts/decidim/decidim_awesome/awesome_map/categories.js.es6 +25 -0
  16. data/app/assets/javascripts/decidim/decidim_awesome/awesome_map/map.js.es6 +215 -0
  17. data/app/assets/javascripts/decidim/decidim_awesome/awesome_map/meetings.js.es6 +129 -0
  18. data/app/assets/javascripts/decidim/decidim_awesome/awesome_map/proposals.js.es6 +78 -0
  19. data/app/assets/javascripts/decidim/decidim_awesome/editors/markdown_view.js.es6 +12 -0
  20. data/app/assets/javascripts/decidim/decidim_awesome/editors/quill_editor.js.es6 +162 -0
  21. data/app/assets/javascripts/decidim/decidim_awesome/forms/autosave.js.es6 +90 -0
  22. data/app/assets/javascripts/decidim/decidim_awesome/proposals/images.js.es6 +25 -0
  23. data/app/assets/stylesheets/decidim/decidim_awesome/admin.scss +45 -0
  24. data/app/assets/stylesheets/decidim/decidim_awesome/admin/constraints.scss +37 -0
  25. data/app/assets/stylesheets/decidim/decidim_awesome/application.scss +8 -0
  26. data/app/assets/stylesheets/decidim/decidim_awesome/awesome_iframe/iframe.scss +10 -0
  27. data/app/assets/stylesheets/decidim/decidim_awesome/awesome_map/leaflet.scss.erb +9 -0
  28. data/app/assets/stylesheets/decidim/decidim_awesome/awesome_map/map.scss +66 -0
  29. data/app/assets/stylesheets/decidim/decidim_awesome/editors/markdown_editor.scss +17 -0
  30. data/app/assets/stylesheets/decidim/decidim_awesome/editors/markdown_view.scss +27 -0
  31. data/app/assets/stylesheets/decidim/decidim_awesome/editors/quill_editor.scss +7 -0
  32. data/app/assets/stylesheets/decidim/decidim_awesome/forms/autosave.scss +17 -0
  33. data/app/awesome_overrides/presenters/decidim/proposals/proposal_presenter_override.rb +31 -0
  34. data/app/commands/decidim/decidim_awesome/admin/create_constraint.rb +47 -0
  35. data/app/commands/decidim/decidim_awesome/admin/destroy_constraint.rb +31 -0
  36. data/app/commands/decidim/decidim_awesome/admin/update_config.rb +44 -0
  37. data/app/commands/decidim/decidim_awesome/admin/update_constraint.rb +44 -0
  38. data/app/commands/decidim/decidim_awesome/create_editor_image.rb +34 -0
  39. data/app/controllers/concerns/decidim/decidim_awesome/needs_awesome_config.rb +17 -0
  40. data/app/controllers/decidim/decidim_awesome/admin/application_controller.rb +21 -0
  41. data/app/controllers/decidim/decidim_awesome/admin/checks_controller.rb +37 -0
  42. data/app/controllers/decidim/decidim_awesome/admin/config_controller.rb +44 -0
  43. data/app/controllers/decidim/decidim_awesome/admin/constraints_controller.rb +126 -0
  44. data/app/controllers/decidim/decidim_awesome/application_controller.rb +14 -0
  45. data/app/controllers/decidim/decidim_awesome/editor_images_controller.rb +50 -0
  46. data/app/controllers/decidim/decidim_awesome/iframe_component/application_controller.rb +15 -0
  47. data/app/controllers/decidim/decidim_awesome/iframe_component/iframe_controller.rb +33 -0
  48. data/app/controllers/decidim/decidim_awesome/map_component/application_controller.rb +15 -0
  49. data/app/controllers/decidim/decidim_awesome/map_component/map_controller.rb +31 -0
  50. data/app/forms/decidim/decidim_awesome/admin/config_form.rb +22 -0
  51. data/app/forms/decidim/decidim_awesome/admin/constraint_form.rb +20 -0
  52. data/app/forms/decidim/decidim_awesome/admin/intergram_form.rb +21 -0
  53. data/app/forms/decidim/decidim_awesome/editor_image_form.rb +16 -0
  54. data/app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb +90 -0
  55. data/app/helpers/decidim/decidim_awesome/application_helper.rb +10 -0
  56. data/app/helpers/decidim/decidim_awesome/map_helper.rb +121 -0
  57. data/app/models/decidim/decidim_awesome/application_record.rb +10 -0
  58. data/app/models/decidim/decidim_awesome/awesome_config.rb +23 -0
  59. data/app/models/decidim/decidim_awesome/config_constraint.rb +13 -0
  60. data/app/models/decidim/decidim_awesome/editor_image.rb +24 -0
  61. data/app/permissions/decidim/decidim_awesome/permissions.rb +29 -0
  62. data/app/uploaders/decidim/decidim_awesome/image_uploader.rb +22 -0
  63. data/app/views/decidim/decidim_awesome/admin/checks/index.html.erb +35 -0
  64. data/app/views/decidim/decidim_awesome/admin/config/_constraints.html.erb +27 -0
  65. data/app/views/decidim/decidim_awesome/admin/config/_form_editors.html.erb +20 -0
  66. data/app/views/decidim/decidim_awesome/admin/config/_form_livechat.html.erb +44 -0
  67. data/app/views/decidim/decidim_awesome/admin/config/_form_proposals.html.erb +12 -0
  68. data/app/views/decidim/decidim_awesome/admin/config/_form_surveys.html.erb +9 -0
  69. data/app/views/decidim/decidim_awesome/admin/config/_modal.html.erb +11 -0
  70. data/app/views/decidim/decidim_awesome/admin/config/show.html.erb +15 -0
  71. data/app/views/decidim/decidim_awesome/admin/constraints/_form.html.erb +56 -0
  72. data/app/views/decidim/decidim_awesome/admin/constraints/new.html.erb +14 -0
  73. data/app/views/decidim/decidim_awesome/admin/constraints/show.html.erb +14 -0
  74. data/app/views/decidim/decidim_awesome/iframe_component/iframe/show.html.erb +9 -0
  75. data/app/views/decidim/decidim_awesome/map_component/map/error.erb +7 -0
  76. data/app/views/decidim/decidim_awesome/map_component/map/show.html.erb +74 -0
  77. data/app/views/layouts/decidim/_head.html.erb +5 -0
  78. data/app/views/layouts/decidim/admin/_header.html.erb +5 -0
  79. data/app/views/layouts/decidim/admin/decidim_awesome.html.erb +47 -0
  80. data/app/views/layouts/decidim/decidim_awesome/_awesome_config.html.erb +20 -0
  81. data/app/views/layouts/decidim/decidim_awesome/_intergram_widget.html.erb +16 -0
  82. data/app/views/v0.21/layouts/decidim/_head.html.erb +34 -0
  83. data/app/views/v0.21/layouts/decidim/admin/_header.html.erb +9 -0
  84. data/app/views/v0.22/layouts/decidim/_head.html.erb +36 -0
  85. data/app/views/v0.22/layouts/decidim/admin/_header.html.erb +11 -0
  86. data/config/i18n-tasks.yml +10 -0
  87. data/config/locales/ca.yml +125 -0
  88. data/config/locales/cs.yml +125 -0
  89. data/config/locales/en.yml +176 -0
  90. data/config/locales/es.yml +125 -0
  91. data/config/locales/fr.yml +125 -0
  92. data/config/locales/sv.yml +125 -0
  93. data/db/migrate/20200324170000_create_decidim_awesome_config.rb +16 -0
  94. data/db/migrate/20200324230936_create_awesome_editor_images.rb +14 -0
  95. data/db/migrate/20200403142257_create_decidim_awesome_config_constraints.rb +13 -0
  96. data/db/seeds.rb +27 -0
  97. data/lib/decidim/decidim_awesome.rb +110 -0
  98. data/lib/decidim/decidim_awesome/admin.rb +10 -0
  99. data/lib/decidim/decidim_awesome/admin_engine.rb +45 -0
  100. data/lib/decidim/decidim_awesome/awesome_helpers.rb +55 -0
  101. data/lib/decidim/decidim_awesome/checksums.yml +15 -0
  102. data/lib/decidim/decidim_awesome/config.rb +129 -0
  103. data/lib/decidim/decidim_awesome/content_renderers.rb +9 -0
  104. data/lib/decidim/decidim_awesome/content_renderers/markdown_renderer.rb +18 -0
  105. data/lib/decidim/decidim_awesome/context_analyzers.rb +11 -0
  106. data/lib/decidim/decidim_awesome/context_analyzers/component_analyzer.rb +38 -0
  107. data/lib/decidim/decidim_awesome/context_analyzers/participatory_space_analyzer.rb +31 -0
  108. data/lib/decidim/decidim_awesome/context_analyzers/request_analyzer.rb +97 -0
  109. data/lib/decidim/decidim_awesome/engine.rb +38 -0
  110. data/lib/decidim/decidim_awesome/iframe_component/component.rb +59 -0
  111. data/lib/decidim/decidim_awesome/iframe_component/engine.rb +23 -0
  112. data/lib/decidim/decidim_awesome/map_component/component.rb +74 -0
  113. data/lib/decidim/decidim_awesome/map_component/engine.rb +23 -0
  114. data/lib/decidim/decidim_awesome/system_checker.rb +63 -0
  115. data/lib/decidim/decidim_awesome/test/factories.rb +36 -0
  116. data/lib/decidim/decidim_awesome/test/shared_examples/config_examples.rb +25 -0
  117. data/lib/decidim/decidim_awesome/test/themes/css.lvh.me.css +3 -0
  118. data/lib/decidim/decidim_awesome/test/themes/erb.lvh.me.scss.erb +2 -0
  119. data/lib/decidim/decidim_awesome/test/themes/scss.lvh.me.scss +1 -0
  120. data/lib/decidim/decidim_awesome/test/themes/test-theme/body.scss +4 -0
  121. data/lib/decidim/decidim_awesome/version.rb +10 -0
  122. data/vendor/assets/images/layers-2x.png +0 -0
  123. data/vendor/assets/images/layers.png +0 -0
  124. data/vendor/assets/images/marker-icon.png +0 -0
  125. data/vendor/assets/javascripts/codemirror-4.inline-attachment.js +89 -0
  126. data/vendor/assets/javascripts/form-storage.js +824 -0
  127. data/vendor/assets/javascripts/highlight.min.js +44 -0
  128. data/vendor/assets/javascripts/image-resize.min.js +1 -0
  129. data/vendor/assets/javascripts/image-upload.min.js +6 -0
  130. data/vendor/assets/javascripts/inline-attachment.js +399 -0
  131. data/vendor/assets/javascripts/inscrybmde.min.js +7 -0
  132. data/vendor/assets/javascripts/jquery.inline-attachment.js +66 -0
  133. data/vendor/assets/javascripts/leaflet.featuregroup.subgroup.js +6 -0
  134. data/vendor/assets/stylesheets/default.min.css +1 -0
  135. data/vendor/assets/stylesheets/foundation.min.css +1 -0
  136. data/vendor/assets/stylesheets/github.min.css +2 -0
  137. data/vendor/assets/stylesheets/inscrybmde.min.css +8 -0
  138. metadata +267 -0
@@ -0,0 +1,17 @@
1
+ // editor tweaks
2
+ //
3
+ .CodeMirror{
4
+ color: #333;
5
+ font-weight: normal;
6
+ line-height: 1.45;
7
+
8
+ code{
9
+ border: none;
10
+ }
11
+
12
+ .CodeMirror-code{
13
+ span{
14
+ color: #333 !important;
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,27 @@
1
+ .awesome-markdown-editor{
2
+ code{
3
+ line-height: 1;
4
+ background: #f0f0f0;
5
+ }
6
+
7
+ pre{
8
+ font-family: Consolas, "Liberation Mono", Courier, monospace;
9
+ font-size: .9em;
10
+ border: 1px solid #e8e8e8;
11
+ background: #f0f0f0;
12
+
13
+ p,
14
+ code{
15
+ line-height: .8;
16
+ }
17
+
18
+ code{
19
+ display: block;
20
+ padding: 1em 0 1em 1em;
21
+ margin: 0;
22
+ border: none;
23
+ }
24
+
25
+ margin: 1em 0;
26
+ }
27
+ }
@@ -0,0 +1,7 @@
1
+ .editor-loading{
2
+ background: url(loading.gif) no-repeat center right;
3
+ }
4
+
5
+ .editor-container{
6
+ overflow: hidden;
7
+ }
@@ -0,0 +1,17 @@
1
+ .awesome_autosave-notice{
2
+ background-color: rgba(var(--success-rgb), .8);
3
+ position: fixed;
4
+ top: 0;
5
+ right: 0;
6
+ margin: 2em;
7
+ padding: .5em 1em;
8
+ border-radius: 4px;
9
+ border: 1px solid var(--success);
10
+ font-weight: 600;
11
+ color: white;
12
+ z-index: 1000;
13
+
14
+ &.error{
15
+ background-color: rgba(var(--alert-rgb), .8);
16
+ }
17
+ }
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Tune Proposal presenter to use markdown if configured
4
+ Decidim::Proposals::ProposalPresenter.class_eval do
5
+ def body(links: false, extras: true, strip_tags: false)
6
+ text = proposal.body
7
+ text = strip_tags(text) if strip_tags
8
+
9
+ renderer = Decidim::ContentRenderers::HashtagRenderer.new(text)
10
+ text = renderer.render(links: links, extras: extras).html_safe
11
+
12
+ if use_markdown? proposal
13
+ text = Decidim::DecidimAwesome::ContentRenderers::MarkdownRenderer.new(text).render
14
+ # HACK: to avoid the replacement of lines to <br> that simple_format does
15
+ text = text.gsub(">\n", ">").gsub("\n<", "<")
16
+ elsif links
17
+ text = Decidim::ContentRenderers::LinkRenderer.new(text).render
18
+ end
19
+ text
20
+ end
21
+
22
+ private
23
+
24
+ def use_markdown?(proposal)
25
+ return false unless proposal.respond_to? :organization
26
+
27
+ config = Decidim::DecidimAwesome::Config.new(proposal.organization)
28
+ config.context_from_component proposal
29
+ config.enabled_for? :use_markdown_editor
30
+ end
31
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ class CreateConstraint < Rectify::Command
7
+ # Public: Initializes the command.
8
+ #
9
+ # form - A constraint form
10
+ def initialize(form)
11
+ @form = form
12
+ end
13
+
14
+ # Executes the command. Broadcasts these events:
15
+ #
16
+ # - :ok when everything is valid.
17
+ # - :invalid if we couldn't proceed.
18
+ #
19
+ # Returns nothing.
20
+ def call
21
+ return broadcast(:invalid) if form.invalid?
22
+ return broadcast(:invalid) if attributes.blank?
23
+
24
+ begin
25
+ @constraint = ConfigConstraint.create!(
26
+ awesome_config: form.context.setting,
27
+ settings: attributes
28
+ )
29
+ broadcast(:ok, @constraint)
30
+ rescue ActiveRecord::RecordNotUnique
31
+ broadcast(:invalid, I18n.t("decidim.decidim_awesome.admin.constraints.errors.not_unique"))
32
+ rescue StandardError => e
33
+ broadcast(:invalid, e.message)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :form, :constraint
40
+
41
+ def attributes
42
+ form.attributes.filter { |_i, v| v.present? }
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ class DestroyConstraint < Rectify::Command
7
+ # Public: Initializes the command.
8
+ #
9
+ # constraint - A constraint constraint
10
+ def initialize(constraint)
11
+ @constraint = constraint
12
+ end
13
+
14
+ # Executes the command. Broadcasts these events:
15
+ #
16
+ # - :ok when everything is valid.
17
+ # - :invalid if we couldn't proceed.
18
+ #
19
+ # Returns nothing.
20
+ def call
21
+ constraint.destroy!
22
+ broadcast(:ok)
23
+ rescue StandardError => e
24
+ broadcast(:invalid, e.message)
25
+ end
26
+
27
+ attr_reader :constraint
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ class UpdateConfig < Rectify::Command
7
+ # Public: Initializes the command.
8
+ #
9
+ # form - A config form
10
+ def initialize(form)
11
+ @form = form
12
+ end
13
+
14
+ # Executes the command. Broadcasts these events:
15
+ #
16
+ # - :ok when everything is valid.
17
+ # - :invalid if we couldn't proceed.
18
+ #
19
+ # Returns nothing.
20
+ def call
21
+ return broadcast(:invalid) if form.invalid?
22
+
23
+ begin
24
+ form.attributes.each do |key, val|
25
+ # ignore nil attributes (must specifically be set to false if necessary)
26
+ next if val.nil?
27
+
28
+ setting = AwesomeConfig.find_or_initialize_by(var: key, organization: form.current_organization)
29
+
30
+ setting.value = val.respond_to?(:attributes) ? val.attributes : val
31
+ setting.save!
32
+ end
33
+
34
+ broadcast(:ok)
35
+ rescue ActiveRecord::RecordInvalid => e
36
+ broadcast(:invalid, e.message)
37
+ end
38
+ end
39
+
40
+ attr_reader :form
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ class UpdateConstraint < Rectify::Command
7
+ # Public: Initializes the command.
8
+ #
9
+ # form - A constraint form
10
+ def initialize(form)
11
+ @form = form
12
+ end
13
+
14
+ # Executes the command. Broadcasts these events:
15
+ #
16
+ # - :ok when everything is valid.
17
+ # - :invalid if we couldn't proceed.
18
+ #
19
+ # Returns nothing.
20
+ def call
21
+ return broadcast(:invalid) if form.invalid?
22
+ return broadcast(:invalid) if attributes.blank?
23
+
24
+ begin
25
+ constraint = ConfigConstraint.find(form.id)
26
+ constraint.settings = attributes
27
+ constraint.save!
28
+ broadcast(:ok)
29
+ rescue ActiveRecord::RecordNotUnique
30
+ broadcast(:invalid, I18n.t("decidim.decidim_awesome.admin.constraints.errors.not_unique"))
31
+ rescue StandardError => e
32
+ broadcast(:invalid, e.message)
33
+ end
34
+ end
35
+
36
+ attr_reader :form
37
+
38
+ def attributes
39
+ form.attributes.filter { |_i, v| v.present? }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ class CreateEditorImage < Rectify::Command
6
+ # Creates a blueprint.
7
+ #
8
+ # form - The form with the data.
9
+ def initialize(form)
10
+ @form = form
11
+ end
12
+
13
+ # Executes the command. Broadcasts these events:
14
+ #
15
+ # - :ok when everything is valid
16
+ # - :invalid if the form wasn't valid and we couldn't proceed.
17
+ #
18
+ # Returns nothing.
19
+ def call
20
+ return broadcast(:invalid) if form.invalid?
21
+
22
+ image = EditorImage.create!(
23
+ image: form.image,
24
+ path: form.path,
25
+ decidim_author_id: form.current_user.id,
26
+ organization: form.current_organization
27
+ )
28
+ broadcast(:ok, image)
29
+ end
30
+
31
+ attr_reader :form
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/decidim_awesome/awesome_helpers"
4
+
5
+ module Decidim
6
+ module DecidimAwesome
7
+ module NeedsAwesomeConfig
8
+ def self.extended(base)
9
+ base.extend AwesomeHelpers
10
+ end
11
+
12
+ def self.included(base)
13
+ base.include AwesomeHelpers
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ # This controller is the abstract class from which all other controllers of
7
+ # this engine inherit.
8
+ #
9
+ # Note that it inherits from `Decidim::Admin::Components::BaseController`, which
10
+ # override its layout and provide all kinds of useful methods.
11
+ class ApplicationController < Decidim::Admin::ApplicationController
12
+ # def permission_class_chain
13
+ # [::Decidim::DecidimAwesome::Admin::Permissions] + super
14
+ # end
15
+ before_action do
16
+ enforce_permission_to :update, :organization, organization: current_organization
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/decidim_awesome/version"
4
+
5
+ module Decidim
6
+ module DecidimAwesome
7
+ module Admin
8
+ # System compatibility analyzer
9
+ class ChecksController < DecidimAwesome::Admin::ApplicationController
10
+ include NeedsAwesomeConfig
11
+ helper ConfigConstraintsHelpers
12
+
13
+ layout "decidim/admin/decidim_awesome"
14
+
15
+ helper_method :overrides, :valid?, :decidim_version, :decidim_version_valid?
16
+
17
+ private
18
+
19
+ def overrides
20
+ SystemChecker.to_h
21
+ end
22
+
23
+ def valid?(spec, file)
24
+ SystemChecker.valid?(spec, file)
25
+ end
26
+
27
+ def decidim_version
28
+ Decidim.version
29
+ end
30
+
31
+ def decidim_version_valid?
32
+ Gem::Dependency.new("", DecidimAwesome::COMPAT_DECIDIM_VERSION).match?("", decidim_version)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ # Global configuration controller
7
+ class ConfigController < DecidimAwesome::Admin::ApplicationController
8
+ include NeedsAwesomeConfig
9
+ include ConfigConstraintsHelpers
10
+ helper ConfigConstraintsHelpers
11
+
12
+ layout "decidim/admin/decidim_awesome"
13
+
14
+ helper_method :constraints_for
15
+
16
+ def show
17
+ @form = form(ConfigForm).from_params(organization_awesome_config)
18
+ end
19
+
20
+ def update
21
+ @form = form(ConfigForm).from_params(params)
22
+
23
+ UpdateConfig.call(@form) do
24
+ on(:ok) do
25
+ flash[:notice] = I18n.t("config.update.success", scope: "decidim.decidim_awesome.admin")
26
+ redirect_to decidim_admin_decidim_awesome.config_path
27
+ end
28
+
29
+ on(:invalid) do |message|
30
+ flash.now[:alert] = I18n.t("config.update.error", error: message, scope: "decidim.decidim_awesome.admin")
31
+ render :show
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def constraints_for(key)
39
+ awesome_config_instance.setting_for(key)&.constraints
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ # Constraints configuration controller for config keys
7
+ class ConstraintsController < DecidimAwesome::Admin::ApplicationController
8
+ include NeedsAwesomeConfig
9
+ helper ConfigConstraintsHelpers
10
+
11
+ layout false
12
+
13
+ # helper_method :participatory_space_manifests, :participatory_spaces_list, :translate_constraint_value
14
+
15
+ def new
16
+ @form = form(ConstraintForm).from_params(filtered_params, setting: current_setting)
17
+ end
18
+
19
+ def show
20
+ @form = form(ConstraintForm).from_params(constraint.settings.merge(filtered_params))
21
+ end
22
+
23
+ def create
24
+ @form = form(ConstraintForm).from_params(params, setting: current_setting)
25
+ CreateConstraint.call(@form) do
26
+ on(:ok) do |constraint|
27
+ render json: {
28
+ id: constraint.id,
29
+ key: current_setting.var,
30
+ message: I18n.t("decidim_awesome.admin.constraints.create.success", scope: "decidim"),
31
+ html: render_to_string(partial: "decidim/decidim_awesome/admin/config/constraints",
32
+ locals: {
33
+ key: current_setting.var,
34
+ constraints: current_setting.constraints
35
+ })
36
+ }
37
+ end
38
+
39
+ on(:invalid) do |message|
40
+ render json: {
41
+ id: params[:id],
42
+ key: current_setting.var,
43
+ message: I18n.t("decidim_awesome.admin.constraints.create.error", scope: "decidim"),
44
+ error: message
45
+ },
46
+ status: :unprocessable_entity
47
+ end
48
+ end
49
+ end
50
+
51
+ def update
52
+ @form = form(ConstraintForm).from_params(params)
53
+ UpdateConstraint.call(@form) do
54
+ on(:ok) do
55
+ render json: {
56
+ id: params[:id],
57
+ key: constraint.awesome_config.var,
58
+ message: I18n.t("decidim_awesome.admin.constraints.update.success", scope: "decidim"),
59
+ html: render_to_string(partial: "decidim/decidim_awesome/admin/config/constraints",
60
+ locals: {
61
+ key: constraint.awesome_config.var,
62
+ constraints: constraint.awesome_config.constraints
63
+ })
64
+ }
65
+ end
66
+
67
+ on(:invalid) do |message|
68
+ render json: {
69
+ id: params[:id],
70
+ key: constraint.awesome_config.var,
71
+ message: I18n.t("decidim_awesome.admin.constraints.update.error", scope: "decidim"),
72
+ error: message
73
+ },
74
+ status: :unprocessable_entity
75
+ end
76
+ end
77
+ end
78
+
79
+ def destroy
80
+ DestroyConstraint.call(constraint) do
81
+ on(:ok) do
82
+ render json: {
83
+ id: params[:id],
84
+ key: constraint.awesome_config.var,
85
+ message: I18n.t("decidim_awesome.admin.constraints.destroy.success", scope: "decidim"),
86
+ html: render_to_string(partial: "decidim/decidim_awesome/admin/config/constraints",
87
+ locals: {
88
+ key: constraint.awesome_config.var,
89
+ constraints: constraint.awesome_config.constraints
90
+ })
91
+ }
92
+ end
93
+
94
+ on(:invalid) do |message|
95
+ render json: {
96
+ id: params[:id],
97
+ key: constraint.awesome_config.var,
98
+ message: I18n.t("decidim_awesome.admin.constraints.destroy.error", scope: "decidim"),
99
+ error: message
100
+ },
101
+ status: :unprocessable_entity
102
+ end
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def filtered_params
109
+ ops = {}
110
+ [:participatory_space_manifest, :participatory_space_slug].each do |key|
111
+ ops[key] = params[key] if params[key]
112
+ end
113
+ ops
114
+ end
115
+
116
+ def constraint
117
+ @constraint ||= ConfigConstraint.find(params[:id])
118
+ end
119
+
120
+ def current_setting
121
+ awesome_config_instance.setting_for params[:key]
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end