decidim-decidim_awesome 0.12.3 → 0.12.4

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 (80) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -1
  3. data/README.md +1 -1
  4. data/app/commands/concerns/decidim/decidim_awesome/admin/needs_constraint_helpers.rb +39 -5
  5. data/app/commands/decidim/decidim_awesome/admin/create_authorization_group.rb +42 -0
  6. data/app/commands/decidim/decidim_awesome/admin/create_custom_redirect.rb +10 -14
  7. data/app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb +9 -8
  8. data/app/commands/decidim/decidim_awesome/admin/create_proposal_custom_field.rb +3 -8
  9. data/app/commands/decidim/decidim_awesome/admin/create_scoped_admin.rb +4 -8
  10. data/app/commands/decidim/decidim_awesome/admin/create_scoped_style.rb +3 -7
  11. data/app/commands/decidim/decidim_awesome/admin/destroy_authorization_group.rb +37 -0
  12. data/app/commands/decidim/decidim_awesome/admin/destroy_custom_redirect.rb +10 -9
  13. data/app/commands/decidim/decidim_awesome/admin/destroy_menu_hack.rb +8 -7
  14. data/app/commands/decidim/decidim_awesome/admin/destroy_proposal_custom_field.rb +7 -13
  15. data/app/commands/decidim/decidim_awesome/admin/destroy_scoped_admin.rb +7 -10
  16. data/app/commands/decidim/decidim_awesome/admin/destroy_scoped_style.rb +6 -11
  17. data/app/commands/decidim/decidim_awesome/admin/update_config.rb +12 -1
  18. data/app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb +11 -11
  19. data/app/commands/decidim/decidim_awesome/admin/update_menu_hack.rb +10 -8
  20. data/app/controllers/concerns/decidim/decidim_awesome/enforce_access_authorizations.rb +49 -0
  21. data/app/controllers/concerns/decidim/decidim_awesome/needs_hashcash.rb +7 -5
  22. data/app/controllers/decidim/decidim_awesome/admin/constraints_controller.rb +2 -0
  23. data/app/controllers/decidim/decidim_awesome/admin/force_authorizations_controller.rb +44 -0
  24. data/app/controllers/decidim/decidim_awesome/required_authorizations_controller.rb +51 -13
  25. data/app/forms/concerns/decidim/decidim_awesome/proposals/admin/proposal_form_customizations.rb +59 -0
  26. data/app/forms/concerns/decidim/decidim_awesome/proposals/proposal_form_customizations.rb +28 -28
  27. data/app/forms/concerns/decidim/decidim_awesome/proposals/proposal_form_customizations_base.rb +36 -0
  28. data/app/forms/decidim/decidim_awesome/admin/authorization_group_form.rb +66 -0
  29. data/app/forms/decidim/decidim_awesome/admin/config_form.rb +12 -26
  30. data/app/forms/decidim/decidim_awesome/admin/constraint_form.rb +2 -0
  31. data/app/helpers/concerns/decidim/decidim_awesome/amendments_helper_override.rb +2 -1
  32. data/app/helpers/decidim/decidim_awesome/admin/config_constraints_helpers.rb +20 -17
  33. data/app/overrides/decidim/assemblies/admin/assemblies/_form/add_visibility_callout.html.erb.deface +3 -0
  34. data/app/overrides/decidim/conferences/admin/conferences/_form/add_visibility_callout.html.erb.deface +3 -0
  35. data/app/overrides/decidim/participatory_processes/admin/participatory_process_groups/_form/add_visibility_callout.html.erb.deface +3 -0
  36. data/app/overrides/decidim/participatory_processes/admin/participatory_processes/_form/add_visibility_callout.html.erb.deface +3 -0
  37. data/app/overrides/decidim/shared/_login_modal/add_hashcash.html.erb.deface +3 -0
  38. data/app/packs/src/decidim/decidim_awesome/admin/auto_edit.js +15 -0
  39. data/app/packs/src/decidim/decidim_awesome/awesome_admin.js +0 -1
  40. data/app/packs/stylesheets/decidim/decidim_awesome/admin/codemirror.scss +6 -1
  41. data/app/packs/stylesheets/decidim/decidim_awesome/admin/constraints.scss +5 -0
  42. data/app/packs/stylesheets/decidim/decidim_awesome/admin/custom_fields.scss +1 -2
  43. data/app/queries/decidim/decidim_awesome/space_constraint_finder.rb +43 -0
  44. data/app/serializers/concerns/decidim/decidim_awesome/proposals/proposal_serializer_methods.rb +3 -2
  45. data/app/services/decidim/decidim_awesome/access_authorization_service.rb +79 -0
  46. data/app/types/concerns/decidim/decidim_awesome/add_proposal_type_custom_fields.rb +2 -1
  47. data/app/views/decidim/decidim_awesome/admin/config/_authorization_options_form.html.erb +19 -0
  48. data/app/views/decidim/decidim_awesome/admin/config/_form_verifications.html.erb +71 -9
  49. data/app/views/decidim/decidim_awesome/admin/constraints/_form.html.erb +5 -4
  50. data/app/views/decidim/decidim_awesome/admin/shared/_visibility_notice.html.erb +8 -0
  51. data/app/views/decidim/decidim_awesome/hashcash/_hidden_field.html.erb +0 -1
  52. data/app/views/decidim/decidim_awesome/required_authorizations/index.html.erb +14 -4
  53. data/config/i18n-tasks.yml +1 -0
  54. data/config/locales/ca.yml +31 -7
  55. data/config/locales/cs.yml +32 -7
  56. data/config/locales/de.yml +31 -7
  57. data/config/locales/en.yml +36 -12
  58. data/config/locales/es.yml +32 -7
  59. data/config/locales/eu.yml +3 -8
  60. data/config/locales/fr.yml +3 -8
  61. data/config/locales/ja.yml +70 -7
  62. data/config/locales/sv.yml +49 -0
  63. data/lib/decidim/decidim_awesome/admin_engine.rb +1 -0
  64. data/lib/decidim/decidim_awesome/{authorizator.rb → authorizer.rb} +1 -1
  65. data/lib/decidim/decidim_awesome/awesome.rb +5 -17
  66. data/lib/decidim/decidim_awesome/awesome_helpers.rb +14 -5
  67. data/lib/decidim/decidim_awesome/checksums.yml +11 -0
  68. data/lib/decidim/decidim_awesome/config.rb +18 -7
  69. data/lib/decidim/decidim_awesome/context_analyzers/request_analyzer.rb +1 -1
  70. data/lib/decidim/decidim_awesome/engine.rb +3 -1
  71. data/lib/decidim/decidim_awesome/menu.rb +1 -1
  72. data/lib/decidim/decidim_awesome/middleware/current_config.rb +5 -10
  73. data/lib/decidim/decidim_awesome/test/initializer.rb +1 -1
  74. data/lib/decidim/decidim_awesome/test/shared_examples/proposal_form_examples.rb +191 -0
  75. data/lib/decidim/decidim_awesome/test/shared_examples/summary_examples.rb +7 -3
  76. data/lib/decidim/decidim_awesome/version.rb +1 -1
  77. data/package.json +4 -4
  78. metadata +20 -5
  79. data/app/controllers/concerns/decidim/decidim_awesome/check_login_authorizations.rb +0 -60
  80. data/app/packs/src/decidim/decidim_awesome/admin/verification_selects.js +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e111d7d8a993a82615ebe5876f238141d046d140d17cc2ce8a47bd57a6e4028b
4
- data.tar.gz: 21012c001100d7bb1fdc7555a6650b695ba990bca5b38e0014ac78082af6f15e
3
+ metadata.gz: 85eb621adc0f8d42ade17d24162061418885a2d566fab94af0ede1a195c1cf06
4
+ data.tar.gz: ae6688521eb2a8f81d02f67790ea25e6ca1be0466e0874d6f624431b65a73c31
5
5
  SHA512:
6
- metadata.gz: 70df95271a2b16f3510ee8fa6549ec616f9c38963b5266291acfa8ec0bf572980af1781474be44b55ee0bd4cdff04efbfb6c135ca48bafe2676ab9d8a2ffc3c6
7
- data.tar.gz: 6e75d54f61ffde030812ff30e6f4ec58b6db4d2e1ce8168f38c58921efa3bf8a648c4c5c58801a714e0740beeabb3d16df97319badab04bae70cd6171169114b
6
+ metadata.gz: b4b183a80725bc02a7c518f1de438eb182e2b1108c4d2ab935d47cd959abf8890da6d587990dc5de64194a69513bf7d758c9d763084c853ae83b62998c0d850c
7
+ data.tar.gz: 75aa3a809688d9d5f8a4cfa51a2cea964b1deeb214d9ad774a6869dadc6c81bcf640182ec9577a7b3dd840f5a74916a73c28e1c1913f03ed30ab8b5569605bab
data/CHANGELOG.md CHANGED
@@ -1,7 +1,19 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
- v0.12.2
4
+ v0.12.4
5
+ -------
6
+ Compatibility:
7
+ - Decidim 0.29.x
8
+
9
+ Features:
10
+ - Added application context for config constraints (whether the user is logged or not)
11
+ - Allow to granular permissions to forced verifications.
12
+ **NOTE** Config var `force_authorization_after_login` has changed to `force_authorizations`
13
+ - Fix use of Hashcash in loginModals
14
+ - Fix validation overrides in the admin for proposals
15
+
16
+ v0.12.3
5
17
  -------
6
18
 
7
19
  Compatibility:
data/README.md CHANGED
@@ -387,7 +387,7 @@ Admins can manage these settings in the Awesome admin panel under the "Verificat
387
387
  ![Forced verifications admin side](examples/forced_verifications_admin.png)
388
388
  ![Forced verifications public side](examples/forced_verifications_public.png)
389
389
 
390
- Note that some pages are allowed, you can even configure which controller are allowed by creating an initializer ("required_authorizations" and "authorizations" are always allowed):
390
+ You can configure which controller are allowed by creating an initializer (some controllers like the login, terms and conditions, etc. are always allowed):
391
391
 
392
392
  ```ruby
393
393
  # config/initializers/decidim_awesome.rb
@@ -6,12 +6,46 @@ module Decidim
6
6
  module NeedsConstraintHelpers
7
7
  private
8
8
 
9
- def create_constraint_never(var)
10
- settings = { "participatory_space_manifest" => "none" }
11
- subconfig = AwesomeConfig.find_or_initialize_by(var: "#{var}_#{@ident}", organization: @organization)
9
+ attr_reader :organization, :config_var
10
+
11
+ def ident
12
+ @ident ||= rand(36**8).to_s(36)
13
+ end
14
+
15
+ def find_var
16
+ @find_var ||= AwesomeConfig.find_or_initialize_by(var: config_var, organization:)
17
+ end
18
+
19
+ def find_sub_var
20
+ @find_sub_var ||= AwesomeConfig.find_or_initialize_by(var: "#{config_var.to_s.singularize}_#{ident}", organization:)
21
+ end
22
+
23
+ def create_array_config!(default_attributes = nil)
24
+ find_var.value = [] unless find_var.value.is_a?(Array)
25
+ find_var.value << default_attributes if default_attributes
26
+ find_var.save!
27
+ find_var
28
+ end
29
+
30
+ def create_hash_config!(default_attributes = nil)
31
+ find_var.value = {} unless find_var.value.is_a?(Hash)
32
+ find_var.value[ident] = default_attributes if default_attributes
33
+ find_var.save!
34
+ find_var
35
+ end
36
+
37
+ def destroy_hash_ident!
38
+ find_var.value.except!(ident)
39
+ find_var.save!
40
+
41
+ # remove associated sub var (dependents will be destroyed automatically via ActiveRecord triggers)
42
+ find_sub_var.destroy! if find_sub_var.present?
43
+ end
44
+
45
+ def create_constraint_never!
12
46
  @constraint = ConfigConstraint.create!(
13
- awesome_config: subconfig,
14
- settings:
47
+ awesome_config: find_sub_var,
48
+ settings: { "participatory_space_manifest" => "none" }
15
49
  )
16
50
  end
17
51
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ class CreateAuthorizationGroup < Command
7
+ include NeedsConstraintHelpers
8
+ # Public: Initializes the command.
9
+ #
10
+ def initialize(organization, config_var = :force_authorizations)
11
+ @organization = organization
12
+ @config_var = config_var
13
+ end
14
+
15
+ # Executes the command. Broadcasts these events:
16
+ #
17
+ # - :ok when everything is valid.
18
+ # - :invalid if we couldn't proceed.
19
+ #
20
+ # Returns nothing.
21
+ def call
22
+ create_hash_config!(attributes)
23
+
24
+ create_constraint_never!
25
+
26
+ broadcast(:ok, ident)
27
+ rescue StandardError => e
28
+ broadcast(:invalid, e.message)
29
+ end
30
+
31
+ private
32
+
33
+ def attributes
34
+ {
35
+ "authorization_handlers" => {},
36
+ "force_authorization_help_text" => {}
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -4,11 +4,14 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class CreateCustomRedirect < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  def initialize(form)
10
11
  @form = form
11
- @redirections = AwesomeConfig.find_or_initialize_by(var: :custom_redirects, organization: form.current_organization)
12
+ @ident = form.to_params[0]
13
+ @organization = form.current_organization
14
+ @config_var = :custom_redirects
12
15
  end
13
16
 
14
17
  # Executes the command. Broadcasts these events:
@@ -21,7 +24,8 @@ module Decidim
21
24
  return broadcast(:invalid) if form.invalid?
22
25
  return broadcast(:invalid, I18n.t("custom_redirects.origin_exists", scope: "decidim.decidim_awesome.admin")) if url_exists?
23
26
 
24
- create_redirection!
27
+ create_hash_config!(form.to_params[1])
28
+
25
29
  broadcast(:ok)
26
30
  rescue StandardError => e
27
31
  broadcast(:invalid, e.message)
@@ -29,21 +33,13 @@ module Decidim
29
33
 
30
34
  private
31
35
 
32
- attr_reader :form, :redirections
33
-
34
- delegate :to_params, to: :form
35
-
36
- def create_redirection!
37
- redirections.value = {} unless redirections.value.is_a? Hash
38
- redirections.value[to_params[0]] = to_params[1]
39
- redirections.save!
40
- end
36
+ attr_reader :form
41
37
 
42
38
  def url_exists?
43
- return false unless redirections
44
- return false unless redirections.value.is_a? Hash
39
+ return false unless find_var
40
+ return false unless find_var.value.is_a? Hash
45
41
 
46
- redirections.value[form.origin].present?
42
+ find_var.value[form.origin].present?
47
43
  end
48
44
  end
49
45
  end
@@ -4,11 +4,13 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class CreateMenuHack < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  def initialize(form, menu_name)
10
11
  @form = form
11
- @menu = AwesomeConfig.find_or_initialize_by(var: menu_name, organization: form.current_organization)
12
+ @config_var = menu_name
13
+ @organization = form.current_organization
12
14
  end
13
15
 
14
16
  # Executes the command. Broadcasts these events:
@@ -21,22 +23,21 @@ module Decidim
21
23
  return broadcast(:invalid) if form.invalid?
22
24
  return broadcast(:invalid, I18n.t("menu_hacks.url_exists", scope: "decidim.decidim_awesome.admin")) if url_exists?
23
25
 
24
- menu.value = [] unless menu.value.is_a? Array
25
- menu.value << to_params
26
- menu.save!
27
- broadcast(:ok, menu)
26
+ create_array_config!(to_params)
27
+
28
+ broadcast(:ok, find_var)
28
29
  rescue StandardError => e
29
30
  broadcast(:invalid, e.message)
30
31
  end
31
32
 
32
33
  private
33
34
 
34
- attr_reader :form, :menu
35
+ attr_reader :form
35
36
 
36
37
  def url_exists?
37
- return false unless menu
38
+ return false unless find_var
38
39
 
39
- menu.value&.detect { |i| i["url"] == form.url.gsub(/\?.*/, "") }
40
+ find_var.value&.detect { |i| i["url"] == form.url.gsub(/\?.*/, "") }
40
41
  end
41
42
 
42
43
  def to_params
@@ -10,7 +10,6 @@ module Decidim
10
10
  #
11
11
  def initialize(organization, config_var = :proposal_custom_fields)
12
12
  @organization = organization
13
- @ident = rand(36**8).to_s(36)
14
13
  @config_var = config_var
15
14
  end
16
15
 
@@ -21,15 +20,11 @@ module Decidim
21
20
  #
22
21
  # Returns nothing.
23
22
  def call
24
- fields = AwesomeConfig.find_or_initialize_by(var: @config_var, organization: @organization)
25
- fields.value = {} unless fields.value.is_a? Hash
26
- # TODO: prevent (unlikely) colisions with exisiting values
27
- fields.value[@ident] = default_definition
28
- fields.save!
23
+ create_hash_config!(default_definition)
29
24
 
30
- create_constraint_never(@config_var == :proposal_custom_fields ? :proposal_custom_field : :proposal_private_custom_field)
25
+ create_constraint_never!
31
26
 
32
- broadcast(:ok, @ident)
27
+ broadcast(:ok, ident)
33
28
  rescue StandardError => e
34
29
  broadcast(:invalid, e.message)
35
30
  end
@@ -10,7 +10,7 @@ module Decidim
10
10
  #
11
11
  def initialize(organization)
12
12
  @organization = organization
13
- @ident = rand(36**8).to_s(36)
13
+ @config_var = :scoped_admins
14
14
  end
15
15
 
16
16
  # Executes the command. Broadcasts these events:
@@ -20,15 +20,11 @@ module Decidim
20
20
  #
21
21
  # Returns nothing.
22
22
  def call
23
- admins = AwesomeConfig.find_or_initialize_by(var: :scoped_admins, organization: @organization)
24
- admins.value = {} unless admins.value.is_a? Hash
25
- # TODO: prevent (unlikely) colisions with exisiting values
26
- admins.value[@ident] = []
27
- admins.save!
23
+ create_hash_config!([])
28
24
 
29
- create_constraint_never(:scoped_admin)
25
+ create_constraint_never!
30
26
 
31
- broadcast(:ok, @ident)
27
+ broadcast(:ok, ident)
32
28
  rescue StandardError => e
33
29
  broadcast(:invalid, e.message)
34
30
  end
@@ -4,11 +4,11 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class CreateScopedStyle < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  def initialize(organization, config_var = :scoped_styles)
10
11
  @organization = organization
11
- @ident = rand(36**8).to_s(36)
12
12
  @config_var = config_var
13
13
  end
14
14
 
@@ -19,13 +19,9 @@ module Decidim
19
19
  #
20
20
  # Returns nothing.
21
21
  def call
22
- styles = AwesomeConfig.find_or_initialize_by(var: @config_var, organization: @organization)
23
- styles.value = {} unless styles.value.is_a? Hash
24
- # TODO: prevent (unlikely) colisions with exisiting values
25
- styles.value[@ident] = ""
26
- styles.save!
22
+ create_hash_config!("")
27
23
 
28
- broadcast(:ok, @ident)
24
+ broadcast(:ok, ident)
29
25
  rescue StandardError => e
30
26
  broadcast(:invalid, e.message)
31
27
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DecidimAwesome
5
+ module Admin
6
+ class DestroyAuthorizationGroup < Command
7
+ include NeedsConstraintHelpers
8
+ # Public: Initializes the command.
9
+ #
10
+ # key - the key to destroy inside force_authorizations
11
+ # organization - the organization to which the config belongs
12
+ def initialize(key, organization, config_var = :force_authorizations)
13
+ @ident = key
14
+ @organization = organization
15
+ @config_var = config_var
16
+ end
17
+
18
+ # Executes the command. Broadcasts these events:
19
+ #
20
+ # - :ok when everything is valid.
21
+ # - :invalid if we couldn't proceed.
22
+ #
23
+ # Returns nothing.
24
+ def call
25
+ return broadcast(:invalid, "Not a hash") unless find_var&.value.is_a?(Hash)
26
+ return broadcast(:invalid, "#{ident} key invalid") unless find_var.value.has_key?(ident)
27
+
28
+ destroy_hash_ident!
29
+
30
+ broadcast(:ok, ident)
31
+ rescue StandardError => e
32
+ broadcast(:invalid, e.message)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -4,14 +4,15 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class DestroyCustomRedirect < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
- # item - the redirections item to destroy
10
+ # item - the redirects item to destroy
10
11
  # organization
11
12
  def initialize(item, organization)
12
13
  @item = item
13
14
  @organization = organization
14
- @redirections = AwesomeConfig.find_by(var: :custom_redirects, organization:)
15
+ @config_var = :custom_redirects
15
16
  end
16
17
 
17
18
  # Executes the command. Broadcasts these events:
@@ -23,23 +24,23 @@ module Decidim
23
24
  def call
24
25
  return broadcast(:invalid) unless url_exists?
25
26
 
26
- redirections.value&.except!(item.origin)
27
- redirections.save!
27
+ find_var.value&.except!(item.origin)
28
+ find_var.save!
28
29
 
29
- broadcast(:ok, @item)
30
+ broadcast(:ok, item)
30
31
  rescue StandardError => e
31
32
  broadcast(:invalid, e.message)
32
33
  end
33
34
 
34
35
  private
35
36
 
36
- attr_reader :organization, :item, :redirections
37
+ attr_reader :item
37
38
 
38
39
  def url_exists?
39
- return false unless redirections
40
- return false unless redirections.value.is_a? Hash
40
+ return false unless find_var
41
+ return false unless find_var.value.is_a? Hash
41
42
 
42
- redirections.value[item.origin].present?
43
+ find_var.value[item.origin].present?
43
44
  end
44
45
  end
45
46
  end
@@ -4,6 +4,7 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class DestroyMenuHack < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  # item - the menu item to destroy
@@ -11,7 +12,7 @@ module Decidim
11
12
  def initialize(item, menu_name, organization)
12
13
  @item = item
13
14
  @organization = organization
14
- @menu = AwesomeConfig.find_by(var: menu_name, organization:)
15
+ @config_var = menu_name
15
16
  end
16
17
 
17
18
  # Executes the command. Broadcasts these events:
@@ -23,8 +24,8 @@ module Decidim
23
24
  def call
24
25
  return broadcast(:invalid) unless url_exists?
25
26
 
26
- menu.value&.reject! { |i| i["url"] == item.url }
27
- menu.save!
27
+ find_var.value&.reject! { |i| i["url"] == item.url }
28
+ find_var.save!
28
29
 
29
30
  broadcast(:ok, @item)
30
31
  rescue StandardError => e
@@ -33,13 +34,13 @@ module Decidim
33
34
 
34
35
  private
35
36
 
36
- attr_reader :organization, :item, :menu
37
+ attr_reader :organization, :item
37
38
 
38
39
  def url_exists?
39
- return false unless menu
40
- return false unless menu.value.is_a? Array
40
+ return false unless find_var
41
+ return false unless find_var.value.is_a? Array
41
42
 
42
- menu.value&.detect { |i| i["url"] == item.url }
43
+ find_var.value&.detect { |i| i["url"] == item.url }
43
44
  end
44
45
  end
45
46
  end
@@ -4,12 +4,13 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class DestroyProposalCustomField < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
- # key - the key to destroy inise proposal_custom_fields
10
+ # key - the key to destroy init proposal_custom_fields
10
11
  # organization
11
12
  def initialize(key, organization, config_var = :proposal_custom_fields)
12
- @key = key
13
+ @ident = key
13
14
  @organization = organization
14
15
  @config_var = config_var
15
16
  end
@@ -21,19 +22,12 @@ module Decidim
21
22
  #
22
23
  # Returns nothing.
23
24
  def call
24
- fields = AwesomeConfig.find_by(var: @config_var, organization: @organization)
25
- return broadcast(:invalid, "Not a hash") unless fields&.value.is_a? Hash
26
- return broadcast(:invalid, "#{key} key invalid") unless fields.value.has_key?(@key)
25
+ return broadcast(:invalid, "Not a hash") unless find_var&.value.is_a? Hash
26
+ return broadcast(:invalid, "#{ident} key invalid") unless find_var.value.has_key?(ident)
27
27
 
28
- fields.value.except!(@key)
29
- fields.save!
28
+ destroy_hash_ident!
30
29
 
31
- # remove constrains associated (a new config var is generated automatically, by removing it, it will trigger destroy on dependents)
32
- constraint = @config_var == :proposal_custom_fields ? :proposal_custom_field : :proposal_private_custom_field
33
- constraint = AwesomeConfig.find_by(var: "#{constraint}_#{@key}", organization: @organization)
34
- constraint.destroy! if constraint.present?
35
-
36
- broadcast(:ok, @key)
30
+ broadcast(:ok, ident)
37
31
  rescue StandardError => e
38
32
  broadcast(:invalid, e.message)
39
33
  end
@@ -4,13 +4,15 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class DestroyScopedAdmin < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  # key - the key to destroy inside scoped_admins
10
11
  # organization
11
12
  def initialize(key, organization)
12
- @key = key
13
+ @ident = key
13
14
  @organization = organization
15
+ @config_var = :scoped_admins
14
16
  end
15
17
 
16
18
  # Executes the command. Broadcasts these events:
@@ -20,17 +22,12 @@ module Decidim
20
22
  #
21
23
  # Returns nothing.
22
24
  def call
23
- admins = AwesomeConfig.find_by(var: :scoped_admins, organization: @organization)
24
- return broadcast(:invalid, "Not a hash") unless admins&.value.is_a? Hash
25
- return broadcast(:invalid, "#{key} key invalid") unless admins.value.has_key?(@key)
25
+ return broadcast(:invalid, "Not a hash") unless find_var&.value.is_a? Hash
26
+ return broadcast(:invalid, "#{ident} key invalid") unless find_var.value.has_key?(ident)
26
27
 
27
- admins.value.except!(@key)
28
- admins.save!
29
- # remove constrains associated (a new config var is generated automatically, by removing it, it will trigger destroy on dependents)
30
- constraint = AwesomeConfig.find_by(var: "scoped_admin_#{@key}", organization: @organization)
31
- constraint.destroy! if constraint.present?
28
+ destroy_hash_ident!
32
29
 
33
- broadcast(:ok, @key)
30
+ broadcast(:ok, ident)
34
31
  rescue StandardError => e
35
32
  broadcast(:invalid, e.message)
36
33
  end
@@ -4,12 +4,13 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class DestroyScopedStyle < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  # key - the key to destroy inside scoped_styles/scoped_admin_styles
10
11
  # organization
11
12
  def initialize(key, organization, config_var = :scoped_styles)
12
- @key = key
13
+ @ident = key
13
14
  @organization = organization
14
15
  @config_var = config_var
15
16
  end
@@ -21,18 +22,12 @@ module Decidim
21
22
  #
22
23
  # Returns nothing.
23
24
  def call
24
- styles = AwesomeConfig.find_by(var: @config_var, organization: @organization)
25
- return broadcast(:invalid, "Not a hash") unless styles&.value.is_a? Hash
26
- return broadcast(:invalid, "#{key} key invalid") unless styles.value.has_key?(@key)
25
+ return broadcast(:invalid, "Not a hash") unless find_var&.value.is_a? Hash
26
+ return broadcast(:invalid, "#{ident} key invalid") unless find_var.value.has_key?(ident)
27
27
 
28
- styles.value.except!(@key)
29
- styles.save!
30
- # remove constrains associated (a new config var is generated automatically, by removing it, it will trigger destroy on dependents)
31
- constraint = @config_var == :scoped_styles ? :scoped_style : :scoped_admin_style
32
- constraint = AwesomeConfig.find_by(var: "#{constraint}_#{@key}", organization: @organization)
33
- constraint.destroy! if constraint.present?
28
+ destroy_hash_ident!
34
29
 
35
- broadcast(:ok, @key)
30
+ broadcast(:ok, ident)
36
31
  rescue StandardError => e
37
32
  broadcast(:invalid, e.message)
38
33
  end
@@ -30,7 +30,18 @@ module Decidim
30
30
  next unless form.valid_keys.include?(key.to_sym)
31
31
 
32
32
  setting = AwesomeConfig.find_or_initialize_by(var: key, organization: form.current_organization)
33
- setting.value = val.respond_to?(:attributes) ? val.attributes : val
33
+
34
+ value = if form.respond_to?("#{key}_attributes")
35
+ # for complex cases
36
+ form.public_send("#{key}_attributes")
37
+ elsif val.respond_to?(:attributes)
38
+ # when the value is another form
39
+ val.attributes
40
+ else
41
+ val
42
+ end
43
+
44
+ setting.value = value
34
45
  setting.save!
35
46
  end
36
47
 
@@ -4,12 +4,14 @@ module Decidim
4
4
  module DecidimAwesome
5
5
  module Admin
6
6
  class UpdateCustomRedirect < Command
7
+ include NeedsConstraintHelpers
7
8
  # Public: Initializes the command.
8
9
  #
9
10
  def initialize(form, item)
10
11
  @form = form
11
12
  @item = item
12
- @redirects = AwesomeConfig.find_by(var: :custom_redirects, organization: form.current_organization)
13
+ @config_var = :custom_redirects
14
+ @organization = form.current_organization
13
15
  end
14
16
 
15
17
  # Executes the command. Broadcasts these events:
@@ -22,26 +24,24 @@ module Decidim
22
24
  return broadcast(:invalid) if form.invalid?
23
25
  return broadcast(:invalid, I18n.t("custom_redirects.origin_missing", scope: "decidim.decidim_awesome.admin")) unless url_exists?
24
26
 
25
- redirects.value&.except!(item.origin)
26
- redirects.value[to_params[0]] = to_params[1]
27
- redirects.save!
27
+ find_var.value&.except!(item.origin)
28
+ find_var.value[form.to_params[0]] = form.to_params[1]
29
+ find_var.save!
28
30
 
29
- broadcast(:ok, redirects)
31
+ broadcast(:ok, find_var)
30
32
  rescue StandardError => e
31
33
  broadcast(:invalid, e.message)
32
34
  end
33
35
 
34
36
  private
35
37
 
36
- attr_reader :form, :redirects, :item
37
-
38
- delegate :to_params, to: :form
38
+ attr_reader :form, :item
39
39
 
40
40
  def url_exists?
41
- return false unless redirects
42
- return false unless redirects.value.is_a? Hash
41
+ return false unless find_var
42
+ return false unless find_var.value.is_a? Hash
43
43
 
44
- redirects.value[item.origin].present?
44
+ find_var.value[item.origin].present?
45
45
  end
46
46
  end
47
47
  end