From f2888a08dcd2c48cb51f064ea43405f7fb05d0b9 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sat, 14 Jun 2025 08:29:00 +0300 Subject: [PATCH] fix query --- app/controllers/templates_dashboard_controller.rb | 13 ++++++++++--- lib/abilities/template_conditions.rb | 7 +------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/controllers/templates_dashboard_controller.rb b/app/controllers/templates_dashboard_controller.rb index 3649ac0f..af47d179 100644 --- a/app/controllers/templates_dashboard_controller.rb +++ b/app/controllers/templates_dashboard_controller.rb @@ -25,7 +25,7 @@ class TemplatesDashboardController < ApplicationController @templates = @templates.none else @template_folders = @template_folders.reject { |e| e.name == TemplateFolder::DEFAULT_NAME } - @templates = filter_templates(@templates) + @templates = filter_templates(@templates).preload(:author, :template_accesses) @templates = Templates::Order.call(@templates, current_user, cookies.permanent[:dashboard_templates_order]) limit = @@ -42,7 +42,7 @@ class TemplatesDashboardController < ApplicationController private def filter_templates(templates) - rel = templates.active.preload(:author, :template_accesses) + rel = templates.active if params[:q].blank? if Docuseal.multitenant? ? current_account.testing? : current_account.linked_account_account @@ -51,7 +51,14 @@ class TemplatesDashboardController < ApplicationController shared_template_ids = TemplateSharing.where(account_id: shared_account_ids).select(:template_id) - rel = rel.where(folder_id: current_account.default_template_folder.id).or(rel.where(id: shared_template_ids)) + rel = Template.where( + Template.arel_table[:id].in( + Arel::Nodes::Union.new( + rel.where(folder_id: current_account.default_template_folder.id).select(:id).arel, + shared_template_ids.arel + ) + ) + ) else rel = rel.where(folder_id: current_account.default_template_folder.id) end diff --git a/lib/abilities/template_conditions.rb b/lib/abilities/template_conditions.rb index e39019b2..32afd8a3 100644 --- a/lib/abilities/template_conditions.rb +++ b/lib/abilities/template_conditions.rb @@ -13,12 +13,7 @@ module Abilities TemplateSharing.where({ ability:, account_id: [user.account_id, TemplateSharing::ALL_ID] }.compact) .select(:template_id) - join_query = - Template.arel_table - .join(Arel::Nodes::TableAlias.new(templates.select(:id).arel.union(shared_ids.arel), 'union_ids')) - .on(Template.arel_table[:id].eq(Arel::Table.new(:union_ids)[:id])) - - Template.joins(join_query.join_sources.first) + Template.where(Template.arel_table[:id].in(Arel::Nodes::Union.new(templates.select(:id).arel, shared_ids.arel))) end def entity(template, user:, ability: nil)