Skip to content

Commit

Permalink
refactor: update run_after_hooks method
Browse files Browse the repository at this point in the history
  • Loading branch information
Stef-Rousset committed May 22, 2024
1 parent 148a906 commit 938615d
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ def run_after_hooks
user = Decidim::User.find(resource.decidim_user_id)
ids = []
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

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

def find_space_follow_id(follows, resource, space)
follows.where(decidim_followable_type: resource.privatable_to_type)
.where(decidim_followable_id: space.id)
&.first&.id
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)
end

def find_object_followed(follow)
follow.decidim_followable_type.constantize.find(follow.decidim_followable_id)
end
Expand Down

0 comments on commit 938615d

Please sign in to comment.