Skip to content

Commit

Permalink
Backport 'Fix n+1 query on assemblies permissions' to v0.27 (decidim#…
Browse files Browse the repository at this point in the history
…12042)

* Optimize query on asseblies page

* Fix permissions

* Running linters

Co-authored-by: Alexandru Emil Lupu <[email protected]>
  • Loading branch information
fblupi and alecslupu authored Nov 21, 2023
1 parent bf42a65 commit 4ded360
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ def user_can_list_assembly_list?
end

def allowed_list_of_assemblies?
parent_assemblies = assembly_admin_allowed_assemblies.flat_map { |assembly| [assembly.id] + assembly.ancestors.pluck(:id) }
parent_assemblies = assembly.ancestors.flat_map { |assembly| [assembly.id] + assembly.ancestors.pluck(:id) }

allowed_list_of_assemblies = Decidim::Assembly.where(id: assembly_admin_allowed_assemblies + parent_assemblies)
allowed_list_of_assemblies = Decidim::Assembly.where(id: [assembly.id] + parent_assemblies)
allowed_list_of_assemblies.uniq.member?(assembly)
end

Expand Down Expand Up @@ -308,10 +308,12 @@ def user_role
end

def assembly_admin_allowed_assemblies
assemblies = AssembliesWithUserRole.for(user, :admin)
child_assemblies = assemblies.flat_map { |assembly| [assembly.id] + assembly.children.pluck(:id) }
@assembly_admin_allowed ||= begin
assemblies = AssembliesWithUserRole.for(user, :admin).where(id: [assembly.id, assembly.parent_id])
child_assemblies = assemblies.flat_map { |assembly| [assembly.id] + assembly.children.pluck(:id) }

Decidim::Assembly.where(id: assemblies + child_assemblies)
Decidim::Assembly.where(id: assemblies + child_assemblies)
end
end
end
end
Expand Down

0 comments on commit 4ded360

Please sign in to comment.