From 78b4edc7901791378ff8b5e7a08558801d7ba1f2 Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Thu, 23 May 2024 11:32:30 +0200 Subject: [PATCH] refactor: update after second review --- .../admin/destroy_participatory_space_private_user.rb | 11 ++++++----- .../destroy_participatory_space_private_user_spec.rb | 10 ++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/decidim-admin/app/commands/decidim/admin/destroy_participatory_space_private_user.rb b/decidim-admin/app/commands/decidim/admin/destroy_participatory_space_private_user.rb index 617d2a6e2f14..b09d5dd524a8 100644 --- a/decidim-admin/app/commands/decidim/admin/destroy_participatory_space_private_user.rb +++ b/decidim-admin/app/commands/decidim/admin/destroy_participatory_space_private_user.rb @@ -23,9 +23,8 @@ def run_after_hooks return if space.respond_to?("is_transparent") && space.is_transparent? - user = Decidim::User.find(resource.decidim_user_id) ids = [] - follows = Decidim::Follow.where(user:) + follows = Decidim::Follow.where(user: resource.decidim_user_id) ids << find_space_follow_id(follows, resource, space) ids << find_children_follows_ids(follows, space) follows.where(id: ids.flatten).destroy_all if ids.present? @@ -38,9 +37,11 @@ def find_space_follow_id(follows, resource, space) end def find_children_follows_ids(follows, space) - follows.select { |follow| find_object_followed(follow).respond_to?("decidim_component_id") } - .select { |follow| space.components.ids.include?(find_object_followed(follow).decidim_component_id) } - &.map(&:id) + follows.map do |follow| + object = find_object_followed(follow) || nil + next unless object.respond_to?("decidim_component_id") + return follow.id if space.components.ids.include?(object.decidim_component_id) + end end def find_object_followed(follow) diff --git a/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb b/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb index 4d8924899a8d..c1d254caa58a 100644 --- a/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb +++ b/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb @@ -48,8 +48,9 @@ module Decidim::Admin it "destroys the follow" do expect(Decidim::Follow.where(user: normal_user).count).to eq(1) - subject.call - expect(Decidim::Follow.where(user: normal_user).count).to eq(0) + expect do + subject.call + end.to change(Decidim::Follow, :count).by(-1) end context "and user follows meeting belonging to assembly" do @@ -61,8 +62,9 @@ module Decidim::Admin component: meetings_component, author: user) create(:follow, followable: meeting, user: normal_user) expect(Decidim::Follow.where(user: normal_user).count).to eq(2) - subject.call - expect(Decidim::Follow.where(user: normal_user).count).to eq(0) + expect do + subject.call + end.to change(Decidim::Follow, :count).by(-2) end end end