diff --git a/app/controllers/templates_dashboard_controller.rb b/app/controllers/templates_dashboard_controller.rb index d7e7554d..289afb43 100644 --- a/app/controllers/templates_dashboard_controller.rb +++ b/app/controllers/templates_dashboard_controller.rb @@ -36,7 +36,7 @@ class TemplatesDashboardController < ApplicationController @templates = @templates.where(folder_id: @default_folder.id) if params[:q].blank? end - @pagy, @templates = load_templates(@templates, @pagy.count) + @pagy, @templates = load_templates(@templates, @pagy.count, show_shared_inline: @show_shared_inline) if params[:q].present? && @templates.blank? @related_submissions_pagy, @related_submissions = load_related_submissions @@ -46,9 +46,16 @@ class TemplatesDashboardController < ApplicationController private - def load_templates(templates, folders_count) + def load_templates(templates, folders_count, show_shared_inline: false) templates = templates.preload(:author, :template_accesses) - templates = Templates.search(current_user, templates, params[:q]) + + templates = + if show_shared_inline + Templates.search_shared(current_user, templates, params[:q]) + else + Templates.search(current_user, templates, params[:q]) + end + templates = Templates::Order.call(templates, current_user, selected_order) limit = diff --git a/app/controllers/templates_shared_controller.rb b/app/controllers/templates_shared_controller.rb index 4487c56f..62efd524 100644 --- a/app/controllers/templates_shared_controller.rb +++ b/app/controllers/templates_shared_controller.rb @@ -15,7 +15,7 @@ class TemplatesSharedController < ApplicationController @templates = @templates.preload(:author, :template_accesses, :template_sharings) .order(id: :desc) - @templates = Templates.search(current_user, @templates, params[:q]) + @templates = Templates.search_shared(current_user, @templates, params[:q]) @pagy, @templates = pagy_auto(@templates, limit: 12) diff --git a/lib/templates.rb b/lib/templates.rb index 38cda986..26e58a3e 100644 --- a/lib/templates.rb +++ b/lib/templates.rb @@ -70,6 +70,21 @@ module Templates end end + def search_shared(current_user, templates, keyword) + return templates if keyword.blank? + + if Docuseal.fulltext_search? + templates.where( + id: SearchEntry.where(record_type: 'Template') + .where(account_id: current_user.account.linked_account_account&.account_id) + .where(*SearchEntries.build_tsquery(keyword)) + .select(:record_id) + ) + else + plain_search(templates, keyword) + end + end + def plain_search(templates, keyword) return templates if keyword.blank? @@ -83,8 +98,7 @@ module Templates templates.where( id: SearchEntry.where(record_type: 'Template') - .where(account_id: [current_user.account_id, - current_user.account.linked_account_account&.account_id].compact) + .where(account_id: current_user.account_id) .where(*SearchEntries.build_tsquery(keyword)) .select(:record_id) )