Skip to content

Commit

Permalink
configurable roles from initializer (#10)
Browse files Browse the repository at this point in the history
* translate values

* make roles configurable

* fix tests
  • Loading branch information
microstudi authored Feb 18, 2023
1 parent 6163113 commit 1a97b89
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 41 additions & 40 deletions app/models/decidim/decidim_awesome/paper_trail_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,67 @@ 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
end
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
</ul>
</div></h2>
</div>
<%= admin_filter_selector(:admin_actions) %>

<%= render partial: "decidim/decidim_awesome/admin/shared/filters_with_date", locals: { i18n_ctx: "admin_accountability" } %>

<div class="card-section">
<p class="help-text"><%= t(".description") %></p>
<div class="table-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,22 @@
</button>
</div>
</div>
<% if @render_date_fields %>
<div class="input-group mt-xs">
<div class="input-group-field">
<%= 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) %>
</div>
<div class="input-group-field">
<%= 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) %>
</div>
<div class="input-group mt-xs">
<div class="input-group-field">
<%= 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) %>
</div>
<% end %>
<div class="input-group-field">
<%= 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) %>
</div>
</div>
<% end %>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ en:
decidim:
admin:
filters:
admin_actions:
admin_accountability:
participatory_space_type_eq:
label: Participatory space type
role:
Expand Down
2 changes: 1 addition & 1 deletion lib/decidim/decidim_awesome/awesome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
26 changes: 13 additions & 13 deletions spec/system/admin/filter_admin_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,35 @@
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)
end
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)
end
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" }
Expand All @@ -99,31 +99,31 @@

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)
end
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)
end
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)
end
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)
Expand Down

0 comments on commit 1a97b89

Please sign in to comment.