Skip to content

Commit

Permalink
refactor: update run_after_hooks method to handle also participatory_…
Browse files Browse the repository at this point in the history
…process and update tests
  • Loading branch information
Stef-Rousset committed May 21, 2024
1 parent d0cd212 commit 5ac5c40
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ def extra_params
end

def run_after_hooks
# A hook to destroy the follows of user on private non transparent assembly and its children
# when private user is destroyed
return unless resource.privatable_to_type == "Decidim::Assembly"
# When private user is destroyed, a hook to destroy the follows of user on private non transparent assembly
# or private participatory process and the follows of their children
space = resource.privatable_to_type.constantize.find(resource.privatable_to_id)

assembly = Decidim::Assembly.find(resource.privatable_to_id)
return unless assembly.private_space == true && assembly.is_transparent == false
return unless space.private_space?

return if space.respond_to?("is_transparent") && space.is_transparent?

user = Decidim::User.find(resource.decidim_user_id)
ids = []
ids << Decidim::Follow.where(user:)
.where(decidim_followable_type: "Decidim::Assembly")
.where(decidim_followable_id: assembly.id)
.first.id
children_ids = Decidim::Follow.where(user:)
.select { |follow| find_object(follow).respond_to?("decidim_component_id") }
.select { |follow| assembly.components.ids.include?(find_object(follow).decidim_component_id) }
.map(&:id)
follows = Decidim::Follow.where(user:)
ids << follows.where(decidim_followable_type: resource.privatable_to_type)
.where(decidim_followable_id: space.id)
.first.id
children_ids = 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)
ids << children_ids
Decidim::Follow.where(user:).where(id: ids.flatten).destroy_all if ids.present?
follows.where(id: ids.flatten).destroy_all if ids.present?
end

def find_object(follow)
def find_object_followed(follow)
follow.decidim_followable_type.constantize.find(follow.decidim_followable_id)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,33 @@ module Decidim::Admin
end
end
end

context "when participatory process is private" do
let(:normal_user) { create(:user, organization:) }
let(:participatory_process) { create(:participatory_process, :private, :published, organization: user.organization) }
let!(:participatory_space_private_user) { create(:participatory_space_private_user, user: normal_user, privatable_to: participatory_process) }

context "and user follows process" do
let!(:follow) { create(:follow, followable: participatory_process, user: normal_user) }

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)
end

context "and user follows meeting belonging to process" do
let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: participatory_process) }
let(:meeting) { create(:meeting, component: meetings_component) }
let!(:second_follow) { create(:follow, followable: meeting, user: normal_user) }

it "destroys all follows" do
expect(Decidim::Follow.where(user: normal_user).count).to eq(2)
subject.call
expect(Decidim::Follow.where(user: normal_user).count).to eq(0)
end
end
end
end
end
end

0 comments on commit 5ac5c40

Please sign in to comment.