From 1a97b89ce42ddb1c3b3dd409977ac94edf3a2252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Sat, 18 Feb 2023 20:42:48 +0100 Subject: [PATCH] configurable roles from initializer (#10) * translate values * make roles configurable * fix tests --- .../admin_accountability/admin/filterable.rb | 10 +-- .../admin/admin_accountability_controller.rb | 4 +- .../decidim_awesome/paper_trail_version.rb | 81 ++++++++++--------- .../admin/admin_accountability/index.html.erb | 4 +- .../admin/shared/_filters_with_date.html.erb} | 32 ++++---- config/locales/en.yml | 2 +- lib/decidim/decidim_awesome/awesome.rb | 2 +- spec/system/admin/filter_admin_action_spec.rb | 26 +++--- 8 files changed, 79 insertions(+), 82 deletions(-) rename app/views/decidim/{admin/shared/_filters.html.erb => decidim_awesome/admin/shared/_filters_with_date.html.erb} (64%) diff --git a/app/controllers/concerns/decidim/decidim_awesome/admin_accountability/admin/filterable.rb b/app/controllers/concerns/decidim/decidim_awesome/admin_accountability/admin/filterable.rb index 94a0df4a6..2a2c4daef 100644 --- a/app/controllers/concerns/decidim/decidim_awesome/admin_accountability/admin/filterable.rb +++ b/app/controllers/concerns/decidim/decidim_awesome/admin_accountability/admin/filterable.rb @@ -37,13 +37,11 @@ def dynamically_translated_filters end def translated_role_type_eq(role) - admin_action = collection.find { |action| action.item && action.item[:role] == role } - admin_action ? translated_attribute(admin_action.item[:role]) : nil + I18n.t(role, scope: "decidim.decidim_awesome.admin.admin_accountability.roles") end def translated_participatory_space_type_eq(item_type) - admin_action = collection.find { |action| action.item_type == item_type } - admin_action ? translated_attribute(admin_action.item_type.demodulize.gsub("UserRole", "")) : nil + item_type.gsub("UserRole", "").safe_constantize&.model_name&.human&.pluralize || item_type end def search_field_predicate @@ -55,11 +53,11 @@ def extra_allowed_params end def participatory_space_types - collection.pluck(:item_type).uniq.sort + @participatory_space_types ||= collection.pluck(:item_type).uniq.sort end def role_types - collection.map { |admin_action| admin_action.item&.[](:role) }.compact.uniq.sort + @role_types ||= collection.map { |admin_action| admin_action.item&.role }.compact.uniq.sort end end end diff --git a/app/controllers/decidim/decidim_awesome/admin/admin_accountability_controller.rb b/app/controllers/decidim/decidim_awesome/admin/admin_accountability_controller.rb index 02bc566a3..56ea5f237 100644 --- a/app/controllers/decidim/decidim_awesome/admin/admin_accountability_controller.rb +++ b/app/controllers/decidim/decidim_awesome/admin/admin_accountability_controller.rb @@ -15,9 +15,7 @@ class AdminAccountabilityController < DecidimAwesome::Admin::ApplicationControll enforce_permission_to :edit_config, :allow_admin_accountability end - def index - @render_date_fields = true - end + def index; end def export format = params[:format].to_s diff --git a/app/models/decidim/decidim_awesome/paper_trail_version.rb b/app/models/decidim/decidim_awesome/paper_trail_version.rb index 785c7d657..aa09ae5c1 100644 --- a/app/models/decidim/decidim_awesome/paper_trail_version.rb +++ b/app/models/decidim/decidim_awesome/paper_trail_version.rb @@ -4,10 +4,15 @@ module Decidim module DecidimAwesome class PaperTrailVersion < PaperTrail::Version default_scope { order("created_at DESC") } - scope :role_actions, -> { where(item_type: ::Decidim::DecidimAwesome.admin_user_roles, event: "create") } + + def self.safe_user_roles + DecidimAwesome.admin_user_roles.filter(&:safe_constantize) + end + + scope :role_actions, -> { where(item_type: PaperTrailVersion.safe_user_roles, event: "create") } def present(html: true) - @present ||= if item_type.in?(Decidim::DecidimAwesome.admin_user_roles) + @present ||= if item_type.in?(PaperTrailVersion.safe_user_roles) PaperTrailRolePresenter.new(self, html: html) else self @@ -15,55 +20,51 @@ def present(html: true) end ransacker :role_type do - Arel.sql(%{ - ( - SELECT cast("decidim_assembly_user_roles"."role" as text) FROM "decidim_assembly_user_roles" - WHERE "decidim_assembly_user_roles"."id" = "versions"."item_id" - AND item_type = 'Decidim::AssemblyUserRole' - UNION - SELECT cast("decidim_participatory_process_user_roles"."role" as text) FROM decidim_participatory_process_user_roles - WHERE decidim_participatory_process_user_roles.id = versions.item_id - AND item_type = 'Decidim::ParticipatoryProcessUserRole' - ) - }) + @role_type ||= begin + queries = PaperTrailVersion.safe_user_roles.map do |role_class| + table = role_class.safe_constantize.table_name + %{ + SELECT ("#{table}"."role")::text FROM "#{table}" + WHERE "#{table}"."id" = "versions"."item_id" + AND item_type = '#{role_class}' + } + end + Arel.sql("(#{queries.join(" UNION ")})") + end end ransacker :participatory_space_type do - Arel.sql(%{ (cast("item_type" as text))}) + Arel.sql(%{("item_type")::text}) end ransacker :user_email do - query = <<-SQL.squish - ( - SELECT decidim_users.email FROM decidim_users - JOIN decidim_assembly_user_roles ON decidim_users.id = decidim_assembly_user_roles.decidim_user_id - WHERE decidim_assembly_user_roles.id = versions.item_id - AND item_type = 'Decidim::AssemblyUserRole' - UNION - SELECT decidim_users.email FROM decidim_users - JOIN decidim_participatory_process_user_roles ON decidim_users.id = decidim_participatory_process_user_roles.decidim_user_id - WHERE decidim_participatory_process_user_roles.id = versions.item_id - AND item_type = 'Decidim::ParticipatoryProcessUserRole' + @user_email ||= begin + queries = PaperTrailVersion.safe_user_roles.map do |role_class| + table = role_class.safe_constantize.table_name + %( + SELECT decidim_users.email FROM decidim_users + JOIN #{table} ON decidim_users.id = #{table}.decidim_user_id + WHERE #{table}.id = versions.item_id + AND item_type = '#{role_class}' ) - SQL - Arel.sql(query) + end + Arel.sql("(#{queries.join(" UNION ")})") + end end ransacker :user_name do - query = <<-SQL.squish - ( - SELECT decidim_users.name FROM decidim_users - JOIN decidim_assembly_user_roles ON decidim_users.id = decidim_assembly_user_roles.decidim_user_id - WHERE decidim_assembly_user_roles.id = versions.item_id - AND item_type = 'Decidim::AssemblyUserRole' - UNION - SELECT decidim_users.name FROM decidim_users - JOIN decidim_participatory_process_user_roles ON decidim_users.id = decidim_participatory_process_user_roles.decidim_user_id - WHERE decidim_participatory_process_user_roles.id = versions.item_id - AND item_type = 'Decidim::ParticipatoryProcessUserRole' + @user_name ||= begin + queries = PaperTrailVersion.safe_user_roles.map do |role_class| + table = role_class.safe_constantize.table_name + %( + SELECT decidim_users.name FROM decidim_users + JOIN #{table} ON decidim_users.id = #{table}.decidim_user_id + WHERE #{table}.id = versions.item_id + AND item_type = '#{role_class}' ) - SQL - Arel.sql(query) + end + Arel.sql("(#{queries.join(" UNION ")})") + end end ransacker :created_at, type: :date do diff --git a/app/views/decidim/decidim_awesome/admin/admin_accountability/index.html.erb b/app/views/decidim/decidim_awesome/admin/admin_accountability/index.html.erb index 35a334863..03bf489ba 100644 --- a/app/views/decidim/decidim_awesome/admin/admin_accountability/index.html.erb +++ b/app/views/decidim/decidim_awesome/admin/admin_accountability/index.html.erb @@ -15,7 +15,9 @@ - <%= admin_filter_selector(:admin_actions) %> + + <%= render partial: "decidim/decidim_awesome/admin/shared/filters_with_date", locals: { i18n_ctx: "admin_accountability" } %> +

<%= t(".description") %>

diff --git a/app/views/decidim/admin/shared/_filters.html.erb b/app/views/decidim/decidim_awesome/admin/shared/_filters_with_date.html.erb similarity index 64% rename from app/views/decidim/admin/shared/_filters.html.erb rename to app/views/decidim/decidim_awesome/admin/shared/_filters_with_date.html.erb index bde77f996..34060ce9a 100644 --- a/app/views/decidim/admin/shared/_filters.html.erb +++ b/app/views/decidim/decidim_awesome/admin/shared/_filters_with_date.html.erb @@ -27,24 +27,22 @@
- <% if @render_date_fields %> -
-
- <%= form.label t("search_field_start_date", scope: "decidim.admin.filters.label") %> - <%= form.date_field(:created_at_gteq, - class: "input-group-field", - label: false, - datepicker: true) %> -
-
- <%= form.label t("search_field_end_date", scope: "decidim.admin.filters.label") %> - <%= form.date_field(:created_at_lteq, - class: "input-group-field", - label: false, - datepicker: true) %> -
+
+
+ <%= form.label t("search_field_start_date", scope: "decidim.admin.filters.label") %> + <%= form.date_field(:created_at_gteq, + class: "input-group-field", + label: false, + datepicker: true) %>
- <% end %> +
+ <%= form.label t("search_field_end_date", scope: "decidim.admin.filters.label") %> + <%= form.date_field(:created_at_lteq, + class: "input-group-field", + label: false, + datepicker: true) %> +
+
<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index daab2cced..b9802f87f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -60,7 +60,7 @@ en: decidim: admin: filters: - admin_actions: + admin_accountability: participatory_space_type_eq: label: Participatory space type role: diff --git a/lib/decidim/decidim_awesome/awesome.rb b/lib/decidim/decidim_awesome/awesome.rb index c5b3200da..7b75d30d5 100644 --- a/lib/decidim/decidim_awesome/awesome.rb +++ b/lib/decidim/decidim_awesome/awesome.rb @@ -229,7 +229,7 @@ module DecidimAwesome # Roles for which it is necessary to show admin_accountability config_accessor :admin_user_roles do - %w(Decidim::AssemblyUserRole Decidim::ParticipatoryProcessUserRole) + %w(Decidim::AssemblyUserRole Decidim::ParticipatoryProcessUserRole Decidim::ConferenceUserRole) end # diff --git a/spec/system/admin/filter_admin_action_spec.rb b/spec/system/admin/filter_admin_action_spec.rb index 5646f67a3..54fa5db0d 100644 --- a/spec/system/admin/filter_admin_action_spec.rb +++ b/spec/system/admin/filter_admin_action_spec.rb @@ -54,19 +54,19 @@ expect(page).to have_content("Role type") find("a", text: "Participatory space type").hover - expect(page).to have_content("Process") - expect(page).to have_content("Assembly") + expect(page).to have_content("Participatory processes") + expect(page).to have_content("Assemblies") find("a", text: "Role type").hover - expect(page).to have_content("admin") - expect(page).to have_content("collaborator") - expect(page).to have_content("moderator") - expect(page).to have_content("valuator") + expect(page).to have_content("Admin") + expect(page).to have_content("Collaborator") + expect(page).to have_content("Moderator") + expect(page).to have_content("Valuator") end context "when filtering admin_actions by PARTICIPATORY SPACE" do it "Assemblies space type" do - apply_filter("Participatory space type", "Assembly") + apply_filter("Participatory space type", "Assemblies") within "tbody" do expect(page).to have_content("Assemblies >", count: 4) @@ -74,7 +74,7 @@ end it "Processes space type" do - apply_filter("Participatory space type", "Process") + apply_filter("Participatory space type", "Participatory processes") within "tbody" do expect(page).to have_content("Processes >", count: 4) @@ -82,7 +82,7 @@ end it "exports the result" do - apply_filter("Participatory space type", "Process") + apply_filter("Participatory space type", "Participatory processes") find(".exports.dropdown").click perform_enqueued_jobs { click_link "Export as CSV" } @@ -99,7 +99,7 @@ context "when filtering admin_actions by ROLE TYPE" do it "Admin role type" do - apply_filter("Role type", "admin") + apply_filter("Role type", "Admin") within "tbody" do expect(page).to have_content("Administrator", count: 2) @@ -107,7 +107,7 @@ end it "Collaborator role type" do - apply_filter("Role type", "collaborator") + apply_filter("Role type", "Collaborator") within "tbody" do expect(page).to have_content("Collaborator", count: 2) @@ -115,7 +115,7 @@ end it "Moderator role type" do - apply_filter("Role type", "moderator") + apply_filter("Role type", "Moderator") within "tbody" do expect(page).to have_content("Moderator", count: 2) @@ -123,7 +123,7 @@ end it "Valuator role type" do - apply_filter("Role type", "valuator") + apply_filter("Role type", "Valuator") within "tbody" do expect(page).to have_content("Valuator", count: 2)