Skip to content

Commit

Permalink
fix valuators bug using templates
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Apr 25, 2024
1 parent f676906 commit 7f79eb9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def avaliablity_options
@avaliablity_options.prepend [t("global_scope", scope: "decidim.templates.admin.proposal_answer_templates.index"), 0]
end

# This is overriden because a bug in decidim templates that prevents valuators to use templates
def fetch
enforce_permission_to(:read, :template, template:, proposal:)

response_object = {
state: template.field_values["internal_state"],
template: populate_template_interpolations(proposal)
}

respond_to do |format|
format.json do
render json: response_object.to_json
end
end
end

private

def accepted_components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module ValuationAssignmentsControllerOverride
included do
def create
enforce_permission_to :assign_to_valuator, :proposals
@form = form(Admin::ValuationAssignmentForm).from_params(params)
Admin::AssignProposalsToValuator.call(@form) do
@form = form(Decidim::Proposals::Admin::ValuationAssignmentForm).from_params(params)
Decidim::Proposals::Admin::AssignProposalsToValuator.call(@form) do
on(:ok) do |_proposal|
flash[:notice] = I18n.t("valuation_assignments.create.success", scope: "decidim.proposals.admin")
redirect_to after_add_evaluator_url
Expand All @@ -24,11 +24,11 @@ def create
end

def destroy
@form = form(Admin::ValuationAssignmentForm).from_params(destroy_params)
@form = form(Decidim::Proposals::Admin::ValuationAssignmentForm).from_params(destroy_params)

enforce_permission_to :unassign_from_valuator, :proposals, valuator: @form.valuator_user

Admin::UnassignProposalsFromValuator.call(@form) do
Decidim::Proposals::Admin::UnassignProposalsFromValuator.call(@form) do
on(:ok) do |_proposal|
flash.keep[:notice] = I18n.t("valuation_assignments.delete.success", scope: "decidim.proposals.admin")
if current_user == @form.valuator_user
Expand Down
50 changes: 50 additions & 0 deletions app/permissions/decidim/templates/admin/permissions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Decidim
module Templates
module Admin
# this is here due a bug in Decidim 0.28 when they introduced proposal answer templates
# they didn't take into account that valuators can answer proposals
class Permissions < Decidim::DefaultPermissions
def permissions
return permission_action if permission_action.scope != :admin
return permission_action unless user
return permission_action if context[:current_organization] != user.organization

if user_has_a_role? && (permission_action.subject == :template && permission_action.action == :read)
allow!
else
return permission_action unless user.admin?

case permission_action.subject
when :template
allow! if [:read, :create, :update, :destroy, :copy].include? permission_action.action
when :templates
allow! if permission_action.action == :index
when :questionnaire
allow!
end
end

permission_action
end

private

def participatory_space
@participatory_space ||= context[:proposal].try(:participatory_space)
end

def user_roles
@user_roles ||= participatory_space.try(:user_roles)
end

def user_has_a_role?
return unless user_roles

user_roles.exists?(user:)
end
end
end
end
end
49 changes: 17 additions & 32 deletions spec/system/admin/valuator_answers_with_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
require "decidim/templates/test/factories"

describe "Valuator answers with templates" do
let(:organization) { create(:organization) }
let!(:organization) { create(:organization) }
let(:user) { create(:user, :confirmed, :admin_terms_accepted, organization:) }
let!(:valuator_role) { create(:participatory_process_user_role, role: :valuator, user:, participatory_process:) }
let(:participatory_process) { create(:participatory_process, organization:) }
let!(:proposal_component) { create(:component, manifest_name: :proposals, participatory_space: participatory_process) }
let!(:reporting_component) { create(:component, manifest_name: :reporting_proposals, settings:, default_step_settings: settings, participatory_space: participatory_process) }
let(:settings) do
{ proposal_answering_enabled: true }
end
let(:participatory_process) { create(:participatory_process, title: { en: "A participatory process" }, organization:) }
let!(:reporting_component) { create(:component, manifest_name: :reporting_proposals, name: { en: "A reporting component" }, participatory_space: participatory_process) }
let(:component) { reporting_component }
let!(:template) { create(:template, :proposal_answer, description: { en: description }, field_values: values, organization:, templatable: reporting_component) }
let!(:proposal) { create(:proposal, component:) }
let!(:valuation_assignment) { create(:valuation_assignment, proposal:, valuator_role:) }
let!(:template) { create(:template, :proposal_answer, description: { en: description }, field_values:, organization:, templatable: component) }
let(:description) { "Some meaningful answer" }
let(:field_values) { { internal_state: "rejected" } }

before do
switch_to_host(organization.host)
Expand All @@ -25,33 +23,20 @@
find("a", class: "action-icon--show-proposal").click
end

describe "using a proposal_answer_template" do
let(:description) { "Some meaningful answer" }
let(:values) do
{ internal_state: "evaluating" }
it "uses the template" do
expect(proposal.reload.internal_state).to eq("not_answered")
within ".edit_proposal_answer" do
select template.name["en"], from: :proposal_answer_template_chooser
sleep 1
expect(page).to have_content(description)
click_on "Answer"
end

it "uses the template" do
within ".edit_proposal_answer" do
select template.name["en"], from: :proposal_answer_template_chooser
expect(page).to have_content(description)
click_link_or_button "Answer"
end

expect(page).to have_admin_callout("Proposal successfully answered")

within "tr", text: proposal.title["en"] do
expect(page).to have_content("Evaluating")
end
end
expect(page).to have_admin_callout("Proposal successfully answered")

it "shows an error message if the template is removed" do
within ".edit_proposal_answer" do
template.destroy!
select template.name["en"], from: :proposal_answer_template_chooser
expect(page).to have_no_content(description)
expect(page).to have_content("Couldn't find this template")
end
within "tr", text: proposal.title["en"] do
expect(page).to have_content("Rejected")
end
expect(proposal.reload.internal_state).to eq("rejected")
end
end

0 comments on commit 7f79eb9

Please sign in to comment.