Skip to content

Commit

Permalink
Backport 'Allow deletion of categories when there are no resources as…
Browse files Browse the repository at this point in the history
…sociated' to v0.27 (#12808)

* Allow deletion of categories when there are no resources associated

* 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 <andreslucena@users.noreply.github.com>

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

Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com>

* Running linters

---------

Co-authored-by: Andrés Pereira de Lucena <andreslucena@users.noreply.github.com>

* Fix ruby syntax for this version

* Fix conditional order

Suggested in code review

* Fix conditional order (part 2)

Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>

---------

Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
  • Loading branch information
andreslucena and alecslupu committed Apr 30, 2024
1 parent 946427a commit 6718a41
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ en:

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

#### 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).

### Added

Nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(category, user)
#
# Returns nothing.
def call
return broadcast(:invalid) if category.nil? || category.subcategories.any?
return broadcast(:invalid) if category.nil? || category.subcategories.any? || !category.unused?

destroy_category
broadcast(:ok)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@
<% end %>
<% if allowed_to? :destroy, :category, category: subcategory %>
<%= icon_link_to "circle-x", 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 "circle-x", 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 %>
<span class="action-icon" title="<%= t("categories.index.category_used", scope: "decidim.admin") %>" data-tooltip="true" data-disable-hover="false">
<%= icon "delete-bin-line", class: "action-icon action-icon--disabled", role: "img", "aria-hidden": true %>
</span>
<% 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: participatory_space) }
let!(:resource) { create(:dummy_resource, component: component, category: 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: 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: 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 6718a41

Please sign in to comment.