Skip to content

Commit

Permalink
Destroy current census (coopdevs#143)
Browse files Browse the repository at this point in the history
* add remove_census button

* add tests

* Move redirect_to out of the conditional

* change destroy action and add tests
  • Loading branch information
antopalidi committed Apr 18, 2023
1 parent 8a2f532 commit 709f552
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
Expand Up @@ -35,6 +35,17 @@ def create
redirect_to decidim_admin_action_delegator.setting_participants_path(current_setting)
end

def destroy_all
enforce_permission_to :destroy, :participant, resource: current_setting

participants_to_remove = current_setting.participants.reject(&:voted?)

participants_to_remove.each(&:destroy)

flash[:notice] = I18n.t("participants.remove_census.success", scope: "decidim.action_delegator.admin", participants_count: participants_to_remove.count)
redirect_to setting_participants_path(current_setting)
end

private

def current_setting
Expand Down
Expand Up @@ -7,6 +7,10 @@
<%= link_to t(".actions.csv_import"),
decidim_admin_action_delegator.new_setting_manage_participant_path(setting_id: current_setting.id),
class: "button tiny button--title" %>
<%= link_to t(".actions.remove_census"), decidim_admin_action_delegator.destroy_all_setting_manage_participants_path(current_setting),
method: :delete,
data: { confirm: t("participants.remove_census.confirm", scope: "decidim.action_delegator.admin") },
class: "button tiny alert button--title" %>
</h2>
</div>
<div class="card-section">
Expand Down
10 changes: 8 additions & 2 deletions config/locales/en.yml
Expand Up @@ -101,6 +101,7 @@ en:
actions:
csv_import: Import CSV
new_participant: New participant
remove_census: Remove census
created_at: Created at
email: Email
last_login: Last login
Expand All @@ -113,6 +114,11 @@ en:
new:
save: Create
title: New participant
remove_census:
confirm: Are you sure you want to remove the census? This operation cannot
be undone
success: "%{participants_count} entries were successfully deleted from
the census"
update:
error: There was a problem updating the participants
success: Participants saved successfully
Expand Down Expand Up @@ -167,8 +173,8 @@ en:
</ul>
check_verifier:
not_installed_html: It seems that the "Delegation Verifier" authorization
method is not installed. You cannot use the integrated census without it.
Please install it at <a href="/system">/system</a>.
method is not installed. You cannot use the integrated census without
it. Please install it at <a href="/system">/system</a>.
create:
error: There was a problem creating the settings
success: Settings created successfully
Expand Down
4 changes: 3 additions & 1 deletion lib/decidim/action_delegator/admin_engine.rb
Expand Up @@ -14,7 +14,9 @@ class AdminEngine < ::Rails::Engine
resources :delegations, only: [:index, :new, :create, :destroy]
resources :ponderations
resources :participants
resources :manage_participants, only: [:new, :create]
resources :manage_participants, only: [:new, :create, :destroy_all] do
delete :destroy_all, on: :collection
end
resources :permissions, only: [:create] do
post :sync, on: :collection
end
Expand Down
Expand Up @@ -11,6 +11,8 @@ module Admin
let(:organization) { create(:organization) }
let(:current_user) { create(:user, :confirmed, :admin, organization: organization) }
let(:consultation) { create(:consultation, organization: organization) }
let(:setting) { create(:setting, consultation: consultation, authorization_method: authorization_method) }
let(:authorization_method) { :both }

before do
request.env["decidim.current_organization"] = organization
Expand All @@ -19,9 +21,6 @@ module Admin
end

describe "GET #new" do
let(:setting) { create(:setting, consultation: consultation, authorization_method: authorization_method) }
let(:authorization_method) { :both }

before do
get :new, params: { setting_id: setting.id }
end
Expand All @@ -34,6 +33,40 @@ module Admin
expect(assigns(:errors)).to eq []
end
end

describe "DELETE #destroy_all" do
let(:question) { create(:question, consultation: consultation) }
let(:response) { create(:response, question: question) }
let!(:vote) { create(:vote, question: question, response: response) }
let!(:participants) { create_list(:participant, 3, setting: setting) }

let(:params) do
{ setting_id: setting.id }
end

it "authorizes the action" do
expect(controller).to receive(:allowed_to?).with(:destroy, :participant, resource: setting)

get :destroy_all, params: params
end

it "removes all and redirects to the participants page" do
expect { delete :destroy_all, params: params }.to change(Participant, :count).by(-3)
expect(flash[:notice]).to eq(I18n.t("participants.remove_census.success", scope: "decidim.action_delegator.admin", participants_count: participants.count))
expect(response).to redirect_to(setting_participants_path(setting))
end

context "when participant has voted" do
let!(:participant) { create(:participant, setting: setting, decidim_user: current_user) }
let!(:vote) { create(:vote, question: question, response: response, author: current_user) }

it "does not remove the voted participants" do
expect { delete :destroy_all, params: params }.to change(Participant, :count).by(-3)
expect(flash[:notice]).to eq(I18n.t("participants.remove_census.success", scope: "decidim.action_delegator.admin", participants_count: participants.count))
expect(response).to redirect_to(setting_participants_path(setting))
end
end
end
end
end
end
Expand Down
Expand Up @@ -100,4 +100,52 @@
end
end
end

context "when removing census" do
let(:consultation) { create(:consultation, organization: organization) }
let(:question) { create(:question, consultation: consultation) }
let(:response) { create(:response, question: question) }
let!(:vote) { create(:vote, question: question, response: response) }
let(:setting) { create(:setting, consultation: consultation) }
let!(:collection) { create_list :participant, 3, setting: setting }

before do
visit decidim_admin_action_delegator.setting_participants_path(setting)
end

it "removes the census" do
collection.each do |participant|
expect(page).to have_content(participant.email)
end

accept_confirm { click_link "Remove census" }

expect(page).to have_content("successfully")

collection.each do |participant|
expect(page).not_to have_content(participant.email)
end
end

context "when participant has voted" do
let!(:participant) { create(:participant, setting: setting, decidim_user: user) }
let!(:vote) { create(:vote, question: question, response: response, author: user) }

it "does not remove the census" do
expect(page).to have_content(user.email)

collection.each do |participant|
expect(page).to have_content(participant.email)
end

accept_confirm { click_link "Remove census" }

collection.each do |participant|
expect(page).not_to have_content(participant.email)
end

expect(page).to have_content(user.email)
end
end
end
end

0 comments on commit 709f552

Please sign in to comment.