decidim-department_admin 0.8.0 → 0.12.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 (91) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -10
  3. data/app/commands/decidim/department_admin/admin/create_department.rb +15 -0
  4. data/app/commands/decidim/department_admin/admin/destroy_department.rb +24 -0
  5. data/app/commands/decidim/department_admin/admin/update_department.rb +11 -0
  6. data/app/controllers/decidim/department_admin/admin/departments_controller.rb +92 -0
  7. data/app/decorators/decidim/admin/newsletters_controller_decorator.rb +5 -5
  8. data/app/decorators/decidim/{newsletters_helper_decorator.rb → admin/newsletters_helper_decorator.rb} +5 -6
  9. data/app/decorators/decidim/admin/users_controller_decorator.rb +10 -10
  10. data/app/decorators/decidim/assemblies/admin/assemblies_controller_decorator.rb +1 -1
  11. data/app/decorators/decidim/assemblies/admin/assemblies_helper_decorator.rb +11 -6
  12. data/app/decorators/decidim/assemblies/admin/assembly_form_decorator.rb +11 -0
  13. data/app/decorators/decidim/assemblies/assembly_presenter_decorator.rb +15 -0
  14. data/app/decorators/decidim/assemblies/content_blocks/metadata_cell_decorator.rb +15 -0
  15. data/app/decorators/decidim/assemblies/create_assembly_decorator.rb +18 -6
  16. data/app/decorators/decidim/assemblies/parent_assemblies_for_select_decorator.rb +2 -2
  17. data/app/decorators/decidim/assemblies/permissions_decorator.rb +1 -1
  18. data/app/decorators/decidim/assemblies/update_assembly_decorator.rb +14 -7
  19. data/app/decorators/decidim/assemblies_decorator.rb +5 -0
  20. data/app/decorators/decidim/assemblies_with_user_role_decorator.rb +4 -4
  21. data/app/decorators/decidim/conference_form_decorator.rb +6 -6
  22. data/app/decorators/decidim/conferences/conference_presenter_decorator.rb +17 -0
  23. data/app/decorators/decidim/conferences/create_conference_decorator.rb +4 -40
  24. data/app/decorators/decidim/conferences/duplicate_conference_decorator.rb +21 -0
  25. data/app/decorators/decidim/conferences/permissions_decorator.rb +1 -1
  26. data/app/decorators/decidim/conferences/update_conference_decorator.rb +5 -9
  27. data/app/decorators/decidim/conferences_decorator.rb +3 -3
  28. data/app/decorators/decidim/conferences_with_user_role_decorator.rb +3 -7
  29. data/app/decorators/decidim/decidim_form_helper_decorator.rb +10 -0
  30. data/app/decorators/decidim/invite_user_decorator.rb +10 -10
  31. data/app/decorators/decidim/invite_user_form_decorator.rb +10 -8
  32. data/app/decorators/decidim/newsletter_decorator.rb +8 -7
  33. data/app/decorators/decidim/organization_decorator.rb +13 -0
  34. data/app/decorators/decidim/participatory_process_decorator.rb +5 -0
  35. data/app/decorators/decidim/participatory_processes/admin/participatory_process_form_decorator.rb +11 -0
  36. data/app/decorators/decidim/participatory_processes/admin/participatory_processes_controller_decorator.rb +3 -3
  37. data/app/decorators/decidim/participatory_processes/content_blocks/metadata_cell_decorator.rb +19 -0
  38. data/app/decorators/decidim/participatory_processes/create_participatory_process_decorator.rb +19 -6
  39. data/app/decorators/decidim/participatory_processes/participatory_process_presenter_decorator.rb +15 -0
  40. data/app/decorators/decidim/participatory_processes/permissions_decorator.rb +1 -1
  41. data/app/decorators/decidim/participatory_processes/update_participatory_process_decorator.rb +14 -6
  42. data/app/decorators/decidim/participatory_processes_with_user_role_decorator.rb +4 -4
  43. data/app/decorators/decidim/ransackable_attributes_decorator.rb +25 -0
  44. data/app/decorators/decidim/user_decorator.rb +7 -0
  45. data/app/decorators/lib/decidim/participatory_space_resourceable_decorator.rb +22 -24
  46. data/app/forms/decidim/department_admin/admin/department_form.rb +19 -0
  47. data/app/models/decidim/department_admin/department.rb +57 -0
  48. data/app/overrides/decidim/admin/shared/add_radio_buttons_to_filters.rb +9 -2
  49. data/app/overrides/decidim/admin/users/add_department_to_form.rb +25 -0
  50. data/app/overrides/decidim/assemblies/admin/assemblies/add_department_columns_to_assemblies_thead.rb +18 -0
  51. data/app/overrides/decidim/assemblies/admin/assemblies/add_department_columns_to_assembly_row.rb +18 -0
  52. data/app/overrides/decidim/assemblies/admin/assemblies/add_department_to_form.rb +30 -0
  53. data/app/overrides/decidim/conferences/admin/conferences/add_department_columns_to_conference_row.rb +18 -0
  54. data/app/overrides/decidim/conferences/admin/conferences/add_department_columns_to_conferences_thead.rb +12 -0
  55. data/app/overrides/decidim/conferences/admin/conferences/add_department_to_form.rb +27 -0
  56. data/app/overrides/decidim/participatory_processes/admin/participatory_processes/add_department_columns_to_process_row.rb +18 -0
  57. data/app/overrides/decidim/participatory_processes/admin/participatory_processes/add_department_columns_to_processes_thead.rb +18 -0
  58. data/app/overrides/decidim/participatory_processes/admin/participatory_processes/add_department_to_form.rb +30 -0
  59. data/app/permissions/decidim/department_admin/permissions.rb +90 -42
  60. data/app/presenters/decidim/department_admin/department_presenter.rb +16 -0
  61. data/app/views/decidim/admin/users/_role_filter.html.erb +18 -0
  62. data/app/views/decidim/admin/users/_search_mode_filter.html.erb +13 -0
  63. data/app/views/decidim/admin/users/index.html.erb +73 -43
  64. data/app/views/decidim/department_admin/admin/departments/_form.html.erb +9 -0
  65. data/app/views/decidim/department_admin/admin/departments/edit.html.erb +16 -0
  66. data/app/views/decidim/department_admin/admin/departments/index.html.erb +48 -0
  67. data/app/views/decidim/department_admin/admin/departments/new.html.erb +16 -0
  68. data/config/i18n-tasks.yml +27 -16
  69. data/config/locales/ca.yml +62 -1
  70. data/config/locales/cs.yml +59 -0
  71. data/config/locales/en.yml +62 -1
  72. data/config/locales/es.yml +62 -1
  73. data/db/migrate/20260506100001_create_decidim_department_admin_departments.rb +13 -0
  74. data/db/migrate/20260506100002_create_decidim_department_admin_user_departments.rb +16 -0
  75. data/db/migrate/20260506100003_add_decidim_department_admin_department_id_to_participatory_processes.rb +8 -0
  76. data/db/migrate/20260506100004_add_decidim_department_admin_department_id_to_assemblies.rb +8 -0
  77. data/db/migrate/20260515000001_migrate_department_admin_areas_to_departments.rb +66 -0
  78. data/db/migrate/20260909000001_add_decidim_department_admin_department_id_to_conferences.rb +10 -0
  79. data/db/migrate/20260909000002_migrate_department_admin_conference_areas_to_departments.rb +43 -0
  80. data/lib/decidim/department_admin/admin_engine.rb +21 -7
  81. data/lib/decidim/department_admin/engine.rb +68 -65
  82. data/lib/decidim/department_admin/test/factories.rb +7 -0
  83. data/lib/decidim/department_admin/version.rb +3 -1
  84. metadata +49 -58
  85. data/app/decorators/decidim/area_decorator.rb +0 -44
  86. data/app/views/decidim/admin/users/_filters.html.erb +0 -32
  87. data/app/views/decidim/admin/users/_form.html.erb +0 -36
  88. data/app/views/decidim/assemblies/admin/assemblies/index.html.erb +0 -139
  89. data/app/views/decidim/conferences/admin/conferences/_form.html.erb +0 -134
  90. data/app/views/decidim/conferences/admin/conferences/index.html.erb +0 -78
  91. data/app/views/decidim/participatory_processes/admin/participatory_processes/index.html.erb +0 -117
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim::Conferences::DuplicateConferenceDecorator
4
+ # Keeps the Department of the original conference in the duplicated one.
5
+ def self.decorate
6
+ return unless Decidim::DepartmentAdmin.conferences_defined?
7
+
8
+ Decidim::Conferences::Admin::DuplicateConference.class_eval do
9
+ alias_method :original_duplicate_conference, :duplicate_conference
10
+
11
+ private
12
+
13
+ def duplicate_conference
14
+ original_duplicate_conference
15
+ @duplicated_conference.update!(decidim_department_admin_department_id: @conference.decidim_department_admin_department_id)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Decidim::Conferences::DuplicateConferenceDecorator.decorate
@@ -12,7 +12,7 @@ module Decidim::Conferences::PermissionsDecorator
12
12
 
13
13
  # rubocop: disable Lint/UnusedMethodArgument
14
14
  def has_manageable_conferences?(role: :any)
15
- return unless user
15
+ return false unless user
16
16
 
17
17
  user.department_admin? || original_has_manageable_conferences?
18
18
  end
@@ -1,19 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Intercepts the `call` method and forces the Area of the user if it is a
4
- # department_admin user.
5
3
  module Decidim::Conferences::UpdateConferenceDecorator
4
+ # Saves the Department chosen in the form.
5
+ # Department admins can update any conference without department restriction.
6
6
  def self.decorate
7
7
  return unless Decidim::DepartmentAdmin.conferences_defined?
8
8
 
9
9
  Decidim::Conferences::Admin::UpdateConference.class_eval do
10
- alias_method :original_call, :call
11
-
12
- def call
13
- author = form.current_user
14
- form.area_id = author.areas.first.id if author.department_admin?
15
- original_call
16
- end
10
+ fetch_form_attributes :title, :slogan, :slug, :weight, :description, :short_description,
11
+ :objectives, :location, :start_date, :end_date, :promoted, :show_statistics,
12
+ :taxonomizations, :registrations_enabled, :decidim_department_admin_department_id
17
13
  end
18
14
  end
19
15
  end
@@ -9,9 +9,9 @@ module Decidim::ConferencesDecorator
9
9
 
10
10
  require_dependency "decidim/conference"
11
11
  Decidim::Conference.class_eval do
12
- belongs_to :area,
13
- foreign_key: "decidim_area_id",
14
- class_name: "Decidim::Area",
12
+ belongs_to :department,
13
+ class_name: "Decidim::DepartmentAdmin::Department",
14
+ foreign_key: :decidim_department_admin_department_id,
15
15
  optional: true
16
16
 
17
17
  has_and_belongs_to_many :users_with_any_role,
@@ -11,15 +11,11 @@ module Decidim::ConferencesWithUserRoleDecorator
11
11
  Decidim::Conferences::ConferencesWithUserRole.class_eval do
12
12
  private
13
13
 
14
- alias_method :conference_ids_by_conferences_user_table, :conference_ids
14
+ alias_method :conference_ids_original, :conference_ids
15
15
 
16
16
  def conference_ids
17
- ids = [conference_ids_by_conferences_user_table]
18
- if user&.department_admin?
19
- ids << ::Decidim::Conference
20
- .where("decidim_area_id" => user.areas.pluck(:id)).pluck(:id)
21
- end
22
-
17
+ ids = [conference_ids_original]
18
+ ids << ::Decidim::Conference.pluck(:id) if user&.department_admin?
23
19
  ::Decidim::Conference.where(id: ids.flatten.uniq)
24
20
  end
25
21
  end
@@ -16,6 +16,16 @@ module Decidim::DecidimFormHelperDecorator
16
16
 
17
17
  original_areas_for_select(organization)
18
18
  end
19
+
20
+ def departments_for_select(organization)
21
+ author = current_user
22
+
23
+ return author.departments.map { |d| [translated_attribute(d.name), d.id] } if author&.department_admin? && controller_path.split("/").include?("admin")
24
+
25
+ Decidim::DepartmentAdmin::Department
26
+ .where(decidim_organization_id: organization.id)
27
+ .map { |d| [translated_attribute(d.name), d.id] }
28
+ end
19
29
  end
20
30
  end
21
31
  end
@@ -2,14 +2,14 @@
2
2
 
3
3
  module Decidim::InviteUserDecorator
4
4
  #
5
- # This decorator overwrites how InviteUser performs the invitation by associating the area to the user.
5
+ # This decorator overwrites how InviteUser performs the invitation by associating the department to the user.
6
6
  #
7
7
  def self.decorate
8
8
  Decidim::InviteUser.class_eval do
9
9
  alias_method :original_update_user, :update_user
10
10
 
11
11
  def update_user
12
- add_selected_area_to(user)
12
+ add_selected_department_to(user)
13
13
  clear_department_admin_role if admin_role?
14
14
  original_update_user
15
15
  end
@@ -18,12 +18,12 @@ module Decidim::InviteUserDecorator
18
18
  @user = Decidim::User.new(
19
19
  name: form.name,
20
20
  email: form.email.downcase,
21
- nickname: Decidim::UserBaseEntity.nicknamize(form.name, organization: form.organization),
21
+ nickname: Decidim::UserBaseEntity.nicknamize(form.name, form.organization.id),
22
22
  organization: form.organization,
23
23
  admin: admin_role?,
24
24
  roles: admin_role? ? [] : [form.role].compact
25
25
  )
26
- add_selected_area_to(@user)
26
+ add_selected_department_to(@user)
27
27
  @user.invite!(
28
28
  form.invited_by,
29
29
  invitation_instructions: form.invitation_instructions
@@ -32,12 +32,12 @@ module Decidim::InviteUserDecorator
32
32
 
33
33
  private #---------------------------------------------------------
34
34
 
35
- def add_selected_area_to(user)
35
+ def add_selected_department_to(user)
36
36
  if current_user.department_admin?
37
- user.areas << current_user.areas.first
38
- elsif form.selected_area.present?
39
- user.areas.clear
40
- user.areas << form.selected_area
37
+ user.departments << current_user.departments.first
38
+ elsif form.selected_department.present?
39
+ user.departments.clear
40
+ user.departments << form.selected_department
41
41
  end
42
42
  end
43
43
 
@@ -46,7 +46,7 @@ module Decidim::InviteUserDecorator
46
46
  end
47
47
 
48
48
  def clear_department_admin_role
49
- user.areas.clear
49
+ user.departments.clear
50
50
  user.roles.delete("department_admin")
51
51
  end
52
52
  end
@@ -2,12 +2,12 @@
2
2
 
3
3
  module Decidim::InviteUserFormDecorator
4
4
  #
5
- # This decorator adds the attribute area_id tot the InviteUserForm and
5
+ # This decorator adds the attribute department_id to the InviteUserForm and
6
6
  # extends it with utility methods for the view and command.
7
7
  #
8
8
  def self.decorate
9
9
  Decidim::InviteUserForm.class_eval do
10
- attribute :area_id, Integer
10
+ attribute :decidim_department_admin_department_id, Integer
11
11
 
12
12
  alias_method :original_roles_method, :available_roles_for_select
13
13
 
@@ -25,18 +25,20 @@ module Decidim::InviteUserFormDecorator
25
25
  end
26
26
 
27
27
  # called from the view
28
- def available_areas_for_select
28
+ def available_departments_for_select
29
29
  if current_user.department_admin?
30
- current_user.areas.collect { |area| [area.translated_name, area.id] }
30
+ current_user.departments.map { |d| [d.translated_name, d.id] }
31
31
  else
32
- Decidim::Area.all.collect { |area| [area.translated_name, area.id] }
32
+ Decidim::DepartmentAdmin::Department
33
+ .where(decidim_organization_id: current_organization.id)
34
+ .map { |d| [d.translated_name, d.id] }
33
35
  end
34
36
  end
35
37
 
36
38
  # called from the command
37
- # returns the selected Decidim::Area instance.
38
- def selected_area
39
- Decidim::Area.find_by(id: area_id)
39
+ # returns the selected Decidim::DepartmentAdmin::Department instance.
40
+ def selected_department
41
+ Decidim::DepartmentAdmin::Department.find_by(id: decidim_department_admin_department_id)
40
42
  end
41
43
  end
42
44
  end
@@ -5,13 +5,14 @@ require_dependency "decidim/newsletter"
5
5
  module Decidim::NewsletterDecorator
6
6
  def self.decorate
7
7
  Decidim::Newsletter.class_eval do
8
- # The area of the newsletter is the same
9
- # than the area of the author of the newsletter.
10
- # The method name is `area` because the permissions system
11
- # must verify that the `user` has the same `area` as the `resource`.
12
- def area
13
- return unless author.areas.any?
14
- return author.areas.first if author&.department_admin?
8
+ # The department of the newsletter is the same
9
+ # than the department of the author of the newsletter.
10
+ # The method name is `department` because the permissions system
11
+ # must verify that the `user` has the same `department` as the `resource`.
12
+ def department
13
+ return unless author.departments.any?
14
+
15
+ author.departments.first if author&.department_admin?
15
16
  end
16
17
  end
17
18
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim::OrganizationDecorator
4
+ def self.decorate
5
+ Decidim::Organization.class_eval do
6
+ has_many :departments, lambda {
7
+ order(name: :asc)
8
+ }, foreign_key: "decidim_organization_id", class_name: "Decidim::DepartmentAdmin::Department", inverse_of: :organization, dependent: :destroy
9
+ end
10
+ end
11
+ end
12
+
13
+ Decidim::OrganizationDecorator.decorate
@@ -14,6 +14,11 @@ module Decidim::ParticipatoryProcessDecorator
14
14
  foreign_key: :decidim_participatory_process_id,
15
15
  association_foreign_key: :decidim_user_id,
16
16
  validate: false
17
+
18
+ belongs_to :department,
19
+ class_name: "Decidim::DepartmentAdmin::Department",
20
+ foreign_key: :decidim_department_admin_department_id,
21
+ optional: true
17
22
  end
18
23
  end
19
24
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessFormDecorator
4
+ def self.decorate
5
+ Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessForm.class_eval do
6
+ attribute :decidim_department_admin_department_id, Integer
7
+ end
8
+ end
9
+ end
10
+
11
+ Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessFormDecorator.decorate
@@ -9,13 +9,13 @@ module Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessesControllerD
9
9
  Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessesController.class_eval do
10
10
  private
11
11
 
12
- alias_method :original_organization_processes, :collection
12
+ alias_method :original_collection, :collection
13
13
 
14
14
  def collection
15
15
  @collection ||= if current_user.admin?
16
- original_organization_processes
16
+ original_collection
17
17
  else
18
- ::Decidim::ParticipatoryProcessesWithUserRole.for(current_user)
18
+ ::Decidim::ParticipatoryProcessesWithUserRole.for(current_user).order(weight: :asc)
19
19
  end
20
20
  end
21
21
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim::ParticipatoryProcesses::ContentBlocks::MetadataCellDecorator
4
+ def metadata_items
5
+ items = super
6
+ items << "department_name" if resource.decidim_department_admin_department_id.present? && items.exclude?("department_name")
7
+ items
8
+ end
9
+
10
+ def department_name
11
+ translated_attribute(resource.department.name) if resource.department
12
+ end
13
+
14
+ def self.decorate
15
+ Decidim::ParticipatoryProcesses::ContentBlocks::MetadataCell.prepend(self)
16
+ end
17
+ end
18
+
19
+ Decidim::ParticipatoryProcesses::ContentBlocks::MetadataCellDecorator.decorate
@@ -1,16 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decidim::ParticipatoryProcesses::CreateParticipatoryProcessDecorator
4
- # Intercepts the `call` method and forces the Area of the user if it is a
5
- # department_admin user.
4
+ # Forces the Department of the user if it is a department_admin user.
6
5
  def self.decorate
7
6
  Decidim::ParticipatoryProcesses::Admin::CreateParticipatoryProcess.class_eval do
8
- alias_method :original_call, :call
7
+ fetch_form_attributes :organization, :title, :subtitle, :weight, :slug, :description,
8
+ :short_description, :promoted, :taxonomizations, :announcement,
9
+ :private_space, :developer_group, :local_area, :target,
10
+ :participatory_scope, :participatory_structure, :meta_scope, :start_date, :end_date,
11
+ :participatory_process_group, :decidim_department_admin_department_id
12
+
13
+ protected
14
+
15
+ def run_after_hooks
16
+ create_steps
17
+ add_admins_as_followers
18
+ link_related_processes
19
+ Decidim::ContentBlocksCreator.new(resource).create_default!
9
20
 
10
- def call
11
21
  author = form.current_user
12
- form.area_id = author.areas.first.id if author.department_admin?
13
- original_call
22
+ if author.department_admin?
23
+ resource.update(decidim_department_admin_department_id: author.departments.first.id)
24
+ elsif form.try(:decidim_department_admin_department_id).present?
25
+ resource.update(decidim_department_admin_department_id: form.decidim_department_admin_department_id)
26
+ end
14
27
  end
15
28
  end
16
29
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim::ParticipatoryProcesses::ParticipatoryProcessPresenterDecorator
4
+ def self.decorate
5
+ Decidim::ParticipatoryProcesses::ParticipatoryProcessPresenter.class_eval do
6
+ def department_name
7
+ return if process.decidim_department_admin_department_id.blank?
8
+
9
+ Decidim::DepartmentAdmin::DepartmentPresenter.new(process.department).translated_name
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Decidim::ParticipatoryProcesses::ParticipatoryProcessPresenterDecorator.decorate
@@ -10,7 +10,7 @@ module Decidim::ParticipatoryProcesses::PermissionsDecorator
10
10
 
11
11
  # rubocop: disable Lint/UnusedMethodArgument
12
12
  def has_manageable_processes?(role: :any)
13
- return unless user
13
+ return false unless user
14
14
 
15
15
  user.department_admin? || original_has_manageable_processes?
16
16
  end
@@ -1,16 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decidim::ParticipatoryProcesses::UpdateParticipatoryProcessDecorator
4
- # Intercepts the `call` method and forces the Area of the user if it is a
5
- # department_admin user.
4
+ # Forces the Department of the user if it is a department_admin user.
6
5
  def self.decorate
7
6
  Decidim::ParticipatoryProcesses::Admin::UpdateParticipatoryProcess.class_eval do
8
- alias_method :original_call, :call
7
+ fetch_form_attributes :title, :subtitle, :weight, :slug, :promoted,
8
+ :taxonomizations, :private_space, :developer_group, :local_area,
9
+ :target, :participatory_scope, :participatory_structure,
10
+ :meta_scope, :start_date, :end_date, :participatory_process_group,
11
+ :announcement, :decidim_department_admin_department_id
9
12
 
10
- def call
13
+ protected
14
+
15
+ def run_before_hooks
11
16
  author = form.current_user
12
- form.area_id = author.areas.first.id if author.department_admin?
13
- original_call
17
+ if author.department_admin?
18
+ form.decidim_department_admin_department_id = author.departments.first.id
19
+ elsif form.try(:decidim_department_admin_department_id).blank?
20
+ form.decidim_department_admin_department_id = resource.decidim_department_admin_department_id
21
+ end
14
22
  end
15
23
  end
16
24
  end
@@ -9,13 +9,13 @@ module Decidim::ParticipatoryProcessesWithUserRoleDecorator
9
9
  Decidim::ParticipatoryProcessesWithUserRole.class_eval do
10
10
  private
11
11
 
12
- alias_method :process_ids_by_process_user_table, :process_ids
12
+ alias_method :process_ids_original, :process_ids
13
13
 
14
14
  def process_ids
15
- ids = [process_ids_by_process_user_table]
16
- if user&.department_admin?
15
+ ids = [process_ids_original]
16
+ if user&.department_admin? && role == :any
17
17
  ids << ::Decidim::ParticipatoryProcess
18
- .where("decidim_area_id" => user.areas.pluck(:id))
18
+ .where(decidim_department_admin_department_id: user.departments.pluck(:id))
19
19
  end
20
20
 
21
21
  ::Decidim::ParticipatoryProcess.where(id: ids.flatten.uniq)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim::RansackableAttributesDecorator
4
+ #
5
+ # Decidim only allows organization admins to filter and sort by admin attributes (private_space,
6
+ # published_at, process group...). Department admins need them too in the admin spaces lists.
7
+ #
8
+ ADMIN_AUTH_OBJECT = Object.new.tap { |auth_object| auth_object.define_singleton_method(:admin?) { true } }.freeze
9
+
10
+ def self.decorate
11
+ models = [Decidim::ParticipatoryProcess, Decidim::Assembly]
12
+ models << Decidim::Conference if Decidim::DepartmentAdmin.conferences_defined?
13
+ models.each do |model|
14
+ model.singleton_class.prepend(self)
15
+ end
16
+ end
17
+
18
+ def ransackable_attributes(auth_object = nil)
19
+ return super unless auth_object.try(:department_admin?)
20
+
21
+ super(ADMIN_AUTH_OBJECT)
22
+ end
23
+ end
24
+
25
+ Decidim::RansackableAttributesDecorator.decorate
@@ -13,6 +13,13 @@ module Decidim::UserDecorator
13
13
  association_foreign_key: :decidim_area_id,
14
14
  validate: false
15
15
 
16
+ has_and_belongs_to_many :departments,
17
+ class_name: "Decidim::DepartmentAdmin::Department",
18
+ join_table: :decidim_department_admin_user_departments,
19
+ foreign_key: :decidim_user_id,
20
+ association_foreign_key: :decidim_department_admin_department_id,
21
+ validate: false
22
+
16
23
  has_and_belongs_to_many :participatory_processes,
17
24
  join_table: :decidim_participatory_process_user_roles,
18
25
  foreign_key: :decidim_user_id,
@@ -2,32 +2,30 @@
2
2
 
3
3
  module Lib::Decidim::ParticipatorySpaceResourceableDecorator
4
4
  #
5
- # This decorator overrided method to avoid .to_sym error when logged in user is Department Admin
6
- # Override affects only line `case role_name&.to_sym`
7
- # Furthermore added new ParticipatorySpaceRole case for DeparmentAdmin
5
+ # - Add new ParticipatorySpaceRole case for DeparmentAdmin.
6
+ # - Add .to_sym to the `case` test, to avoid error when logged in user is Department Admin
7
+ # This override affects only line `case role_name&.to_sym`
8
8
  #
9
- def self.decorate
10
- Decidim::ParticipatorySpaceResourceable.class_eval do
11
- def user_role_config_for(user, role_name)
12
- case role_name&.to_sym
13
- when :organization_admin
14
- Decidim::ParticipatorySpaceRoleConfig::Admin.new(user)
15
- when :admin # ParticipatorySpace admin
16
- Decidim::ParticipatorySpaceRoleConfig::ParticipatorySpaceAdmin.new(user)
17
- when :department_admin
18
- Decidim::ParticipatorySpaceRoleConfig::DepartmentAdmin.new(user)
19
- when :valuator
20
- Decidim::ParticipatorySpaceRoleConfig::Valuator.new(user)
21
- when :moderator
22
- Decidim::ParticipatorySpaceRoleConfig::Moderator.new(user)
23
- when :collaborator
24
- Decidim::ParticipatorySpaceRoleConfig::Collaborator.new(user)
25
- else
26
- Decidim::ParticipatorySpaceRoleConfig::NullObject.new(user)
27
- end
28
- end
9
+ def user_role_config_for(user, role_name)
10
+ case role_name&.to_sym
11
+ when :organization_admin
12
+ Decidim::ParticipatorySpaceRoleConfig::Admin.new(user)
13
+ when :admin # ParticipatorySpace admin
14
+ Decidim::ParticipatorySpaceRoleConfig::ParticipatorySpaceAdmin.new(user)
15
+ when :department_admin
16
+ Decidim::ParticipatorySpaceRoleConfig::DepartmentAdmin.new(user)
17
+ when :evaluator
18
+ Decidim::ParticipatorySpaceRoleConfig::Evaluator.new(user)
19
+ when :moderator
20
+ Decidim::ParticipatorySpaceRoleConfig::Moderator.new(user)
21
+ when :collaborator
22
+ Decidim::ParticipatorySpaceRoleConfig::Collaborator.new(user)
23
+ else
24
+ Decidim::ParticipatorySpaceRoleConfig::NullObject.new(user)
29
25
  end
30
26
  end
31
27
  end
32
28
 
33
- Lib::Decidim::ParticipatorySpaceResourceableDecorator.decorate
29
+ Decidim::Assembly.prepend(Lib::Decidim::ParticipatorySpaceResourceableDecorator)
30
+ Decidim::ParticipatoryProcess.prepend(Lib::Decidim::ParticipatorySpaceResourceableDecorator)
31
+ Decidim::Conference.prepend(Lib::Decidim::ParticipatorySpaceResourceableDecorator) if Decidim::DepartmentAdmin.conferences_defined?
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DepartmentAdmin
5
+ module Admin
6
+ class DepartmentForm < Decidim::Form
7
+ include TranslatableAttributes
8
+
9
+ translatable_attribute :name, String
10
+ attribute :organization, Decidim::Organization
11
+
12
+ validates :name, translatable_presence: true
13
+ validates :organization, presence: true
14
+
15
+ alias organization current_organization
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module DepartmentAdmin
5
+ class Department < ApplicationRecord
6
+ include Traceable
7
+ include Loggable
8
+ include Decidim::TranslatableResource
9
+
10
+ translatable_fields :name
11
+
12
+ belongs_to :organization,
13
+ foreign_key: "decidim_organization_id",
14
+ class_name: "Decidim::Organization"
15
+
16
+ has_and_belongs_to_many :users,
17
+ -> { where("'department_admin'=ANY(\"decidim_users\".\"roles\")") },
18
+ class_name: "Decidim::User",
19
+ join_table: :decidim_department_admin_user_departments,
20
+ foreign_key: :decidim_department_admin_department_id,
21
+ association_foreign_key: :decidim_user_id,
22
+ validate: false
23
+
24
+ validates :name, presence: true, uniqueness: { scope: :organization }
25
+
26
+ before_destroy :abort_if_dependencies, :abort_if_department_admins
27
+
28
+ def self.log_presenter_class_for(_log)
29
+ Decidim::AdminLog::DepartmentPresenter
30
+ end
31
+
32
+ def translated_name
33
+ Decidim::DepartmentAdmin::DepartmentPresenter.new(self).translated_name
34
+ end
35
+
36
+ def has_dependencies?
37
+ Decidim.participatory_space_registry.manifests.any? do |manifest|
38
+ manifest
39
+ .participatory_spaces
40
+ .call(organization)
41
+ .any? do |space|
42
+ space.respond_to?(:department) && space.decidim_department_admin_department_id == id
43
+ end
44
+ end
45
+ end
46
+
47
+ # used on before_destroy
48
+ def abort_if_dependencies
49
+ throw(:abort) if has_dependencies?
50
+ end
51
+
52
+ def abort_if_department_admins
53
+ throw(:abort) if users.any?
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,6 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Radio buttons inside the search form so filter_search is submitted with the query
3
4
  Deface::Override.new(virtual_path: "decidim/admin/shared/_filters",
4
- name: "add_radio_buttons_to_filters",
5
+ name: "add_search_mode_radios_to_filters",
5
6
  insert_after: "div.input-group",
6
- partial: "decidim/admin/users/filters")
7
+ partial: "decidim/admin/users/search_mode_filter")
8
+
9
+ # Role select outside the form, navigates via URL
10
+ Deface::Override.new(virtual_path: "decidim/admin/shared/_filters",
11
+ name: "add_role_filter_to_filters",
12
+ insert_after: "div.filters__section",
13
+ partial: "decidim/admin/users/role_filter")
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ Deface::Override.new(
4
+ virtual_path: "decidim/admin/users/_form",
5
+ name: "add-department-select-to-users-form",
6
+ insert_bottom: "div.card-section",
7
+ text: <<-ERB
8
+ <div id="department-block" class="row column" style="<%= "display:none;" unless @form.role == "department_admin" %>">
9
+ <%= form.select :decidim_department_admin_department_id,
10
+ @form.available_departments_for_select,
11
+ { label: t("decidim.admin.users.form.department") },
12
+ { id: "user_department_id", disabled: current_user.roles.include?("department_admin") } %>
13
+ </div>
14
+
15
+ <%= javascript_tag do %>
16
+ $('#user_role').on('change', function(ev) {
17
+ if (ev.target.value == 'department_admin') {
18
+ $('#department-block').show()
19
+ } else {
20
+ $('#department-block').hide()
21
+ }
22
+ })
23
+ <% end %>
24
+ ERB
25
+ )