Skip to content

Commit

Permalink
[FEATURE] Do not allow to delete areas when they have dependent space…
Browse files Browse the repository at this point in the history
…s. (#5041)

* [FEATURE] Do not allow to delete areas when they have dependent spaces.

* Add changelog entry.

* [TEST] Set ParticipatoryProcess.area field in factory to nil as default.

* [REFACTOR] Remove dependency from ParticipatoryProcesses and Assembiles modules by invoking the manifest registry.

* [REFACTOR] Extract method from command to model where it belongs.

* [REFACTOR] Extract tests to the modules where they belong to.

* [REFACTOR] Use before_destroy callback so that code is more extendable.

* Rubocopify.
  • Loading branch information
tramuntanal committed Apr 3, 2019
1 parent ddc1a27 commit 8e740c5
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
## [Unreleased](https://github.com/decidim/decidim/tree/HEAD)

**Added**:
- **decidim-assemblies**, **decidim-conferences**, **decidim-participatory_processes** Space CTA button text changes when no components [\#5006](https://github.com/decidim/decidim/pull/5006)

- **decidim-admin**: Do not allow to delete areas when they have dependent spaces. [#5041](https://github.com/decidim/decidim/pull/5041)
- **decidim-assemblies**, **decidim-conferences**, **decidim-participatory_processes** Space CTA button text changes when no components [\#5006](https://github.com/decidim/decidim/pull/5006)
- **decidim-participatory_processes**: Add a select field for assign an area to participatory processes [#5011](https://github.com/decidim/decidim/pull/5011)
- **decidim-accountability**: Also display the main scope as a filter for accountability results [#5022](https://github.com/decidim/decidim/pull/5022)

Expand Down
2 changes: 2 additions & 0 deletions decidim-admin/app/commands/decidim/admin/destroy_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(area, current_user)
def call
destroy_area
broadcast(:ok)
rescue ActiveRecord::RecordNotDestroyed
broadcast(:has_spaces)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def destroy
flash[:notice] = I18n.t("areas.destroy.success", scope: "decidim.admin")
redirect_to areas_path
end
on(:has_spaces) do
flash[:alert] = I18n.t("areas.destroy.has_spaces", scope: "decidim.admin")
redirect_to areas_path
end
end
end

Expand Down
1 change: 1 addition & 0 deletions decidim-admin/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ en:
error: There was a problem creating a new area.
success: Area created successfully.
destroy:
has_spaces: Area has depedent spaces, it must not have dependencies in order to be deleted.
success: Area successfully destroyed
edit:
title: Edit area
Expand Down
20 changes: 20 additions & 0 deletions decidim-admin/spec/commands/decidim/admin/destroy_area_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,25 @@ module Decidim::Admin
action_log = Decidim::ActionLog.last
expect(action_log.version).to be_present
end

context "when a participatory process associated to a given area exist" do
let!(:process) { create(:participatory_process, organization: area.organization, area: area) }

it "can not be deleted" do
expect { subject.call }.to broadcast(:has_spaces)
area.reload
expect(area.destroyed?).to be false
end
end

context "when an assembly associated to a given area exist" do
let!(:process) { create(:assembly, organization: area.organization, area: area) }

it "can not be deleted" do
expect { subject.call }.to broadcast(:has_spaces)
area.reload
expect(area.destroyed?).to be false
end
end
end
end
26 changes: 23 additions & 3 deletions decidim-admin/spec/system/admin_manages_organization_areas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,36 @@
end

it "can delete them" do
within find("tr", text: translated(area.name)) do
accept_confirm { click_link "Delete" }
end
click_delete_area

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

within ".card-section" do
expect(page).to have_no_content(translated(area.name))
end
end

context "when a participatory space associated to a given area exist" do
let!(:process) { create(:participatory_process, organization: area.organization, area: area) }

it "can not be deleted" do
click_delete_area
expect(area.reload.destroyed?).to be false
expect(page).to have_admin_callout("Area has depedent spaces")
end
end
end
end

#---------------------------------------------------

private

#---------------------------------------------------

def click_delete_area
within find("tr", text: translated(area.name)) do
accept_confirm { click_link "Delete" }
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim::Admin
describe DestroyArea do
subject { described_class.new(area, user) }

let(:organization) { create :organization }
let(:user) { create :user, :admin, :confirmed, organization: organization }
let(:area) { create :area, organization: organization }

context "when an assembly associated to a given area exist" do
let!(:assembly) { create(:assembly, organization: organization, area: area) }

it "can not be deleted" do
expect { subject.call }.to broadcast(:has_spaces)
expect(area.reload.destroyed?).to be false
end
end
end
end
18 changes: 18 additions & 0 deletions decidim-core/app/models/decidim/area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ class Area < ApplicationRecord
validates :name, :organization, presence: true
validates :name, uniqueness: { scope: [:organization, :area_type] }

before_destroy :abort_if_dependencies

def self.log_presenter_class_for(_log)
Decidim::AdminLog::AreaPresenter
end

def translated_name
Decidim::AreaPresenter.new(self).translated_name
end

def has_dependencies?
Decidim.participatory_space_registry.manifests.any? do |manifest|
manifest
.participatory_spaces
.call(organization)
.any? do |space|
space.respond_to?(:area) && space.decidim_area_id == id
end
end
end

# used on before_destroy
def abort_if_dependencies
throw(:abort) if has_dependencies?
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
private_space { false }
start_date { Date.current }
end_date { 2.months.from_now }
area { nil }

trait :promoted do
promoted { true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim::Admin
describe DestroyArea do
subject { described_class.new(area, user) }

let(:organization) { create :organization }
let(:user) { create :user, :admin, :confirmed, organization: organization }
let(:area) { create :area, organization: organization }

context "when a participatory process associated to a given area exist" do
let!(:process) { create(:participatory_process, organization: organization, area: area) }

it "can not be deleted" do
expect { subject.call }.to broadcast(:has_spaces)
expect(area.reload.destroyed?).to be false
end
end
end
end

0 comments on commit 8e740c5

Please sign in to comment.