Skip to content

Commit

Permalink
Allow deletion of categories when there are no resources associated (d…
Browse files Browse the repository at this point in the history
…ecidim#12143)

* Destroy categorization when resource is being destroyed

* Add rake task and RELEASE NOTES

* Fix template

* Prevent destroy command to remove categories in use

* Apply suggestions from code review

Co-authored-by: Andrés Pereira de Lucena <[email protected]>

* Update decidim-core/lib/decidim/core/test/shared_examples/has_category.rb

Co-authored-by: Andrés Pereira de Lucena <[email protected]>

* Running linters

---------

Co-authored-by: Andrés Pereira de Lucena <[email protected]>
  • Loading branch information
alecslupu and andreslucena authored Feb 22, 2024
1 parent d8b277e commit a134dff
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
12 changes: 11 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,17 @@ In case you have modifications in your application's webpack configuration, adap

You can read more about this change on PR [\#12238](https://github.com/decidim/decidim/pull/12238).

### 3.4. [[TITLE OF THE ACTION]]
### 3.4. Allow removal of orphan categories

A bug was identified that prevented the deletion of categories lacking associated resources. This action is a one-time task that must be performed directly in the production database.

```console
bin/rails decidim:upgrade:fix_orphan_categorizations
```

You can read more about this change on PR [\#12143](https://github.com/decidim/decidim/pull/12143).

### 3.5. [[TITLE OF THE ACTION]]

You can read more about this change on PR [\#XXXX](https://github.com/decidim/decidim/pull/XXXX).

Expand Down
4 changes: 2 additions & 2 deletions decidim-admin/app/commands/decidim/admin/destroy_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Admin
# A command with all the business logic to destroy a category in the
# system.
class DestroyCategory < Decidim::Commands::DestroyResource
private
protected

def invalid?
resource.nil? || resource.subcategories.any?
resource.nil? || resource.subcategories.any? || !resource.unused?
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
<% end %>

<% if allowed_to? :destroy, :category, category: subcategory %>
<%= icon_link_to "delete-bin-line", category_path(current_participatory_space, subcategory), t("actions.destroy", scope: "decidim.admin"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin") } %>
<% if subcategory.unused? %>
<%= icon_link_to "delete-bin-line", category_path(current_participatory_space, subcategory), t("actions.destroy", scope: "decidim.admin"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin") } %>
<% else %>
<%= icon "delete-bin-line", class: "action-icon action-icon--disabled", role: "img", "aria-hidden": true %>
<% end %>
<% end %>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module Admin
end
end

context "when the category is being used by a resource" do
let(:component) { create(:dummy_component, participatory_space:) }
let!(:resource) { create(:dummy_resource, component:, category:) }

it "broadcasts invalid" do
expect { command.call }.to broadcast(:invalid)
end
end

context "when the category is not empty" do
let!(:subcategory) { create :subcategory, parent: category }

Expand Down
27 changes: 27 additions & 0 deletions decidim-core/lib/decidim/core/test/shared_examples/has_category.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
# frozen_string_literal: true

shared_examples_for "has category" do
let(:participatory_space) { subject.participatory_space }

context "when the category is from another organization" do
before do
subject.category = create(:category)
end

it { is_expected.not_to be_valid }
end

context "when the category is from the same organization" do
before do
subject.category = create(:category, participatory_space:)
end

it { is_expected.to be_valid }
end

context "when the resource is being deleted" do
before do
subject.category = create(:category, participatory_space:)
subject.save!
end

it "persists the categorization" do
expect(subject.categorization).to be_persisted
end

it "deletes the categorization" do
expect(Decidim::Categorization.count).to eq(1)
expect { subject.destroy }.to change(Decidim::Categorization, :count).by(-1)
expect(Decidim::Categorization.count).to eq(0)
end
end
end
2 changes: 1 addition & 1 deletion decidim-core/lib/decidim/has_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module HasCategory
extend ActiveSupport::Concern

included do
has_one :categorization, as: :categorizable
has_one :categorization, as: :categorizable, class_name: "Decidim::Categorization", dependent: :destroy
has_one :category, through: :categorization

scope :with_category, lambda { |category|
Expand Down
15 changes: 15 additions & 0 deletions decidim-core/lib/tasks/upgrade/decidim_fix_categorization.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

namespace :decidim do
namespace :upgrade do
desc "Remove orphan categorizations"
task fix_orphan_categorizations: :environment do
logger = Logger.new($stdout)
logger.info("Removing orphan categorizations...")

Decidim::Categorization.find_each do |categorization|
categorization.destroy if categorization.categorizable.nil?
end
end
end
end

0 comments on commit a134dff

Please sign in to comment.