add folders/templates sorting

This commit is contained in:
Alex Turchyn
2025-01-31 23:29:01 +02:00
committed by Pete Matsyburka
parent b498291b62
commit e1cd62ff40
9 changed files with 144 additions and 17 deletions
@@ -7,11 +7,18 @@ class TemplatesDashboardController < ApplicationController
SHOW_TEMPLATES_FOLDERS_THRESHOLD = 9
TEMPLATES_PER_PAGE = 12
FOLDERS_PER_PAGE = 18
LAST_USED_SQL = <<~SQL.squish
GREATEST(
COALESCE(MAX(templates.updated_at), '1970-01-01'),
COALESCE(MAX(submissions.created_at), '1970-01-01')
)
SQL
def index
@template_folders = @template_folders.where(id: @templates.active.select(:folder_id)).order(id: :desc)
@template_folders = @template_folders.where(id: @templates.active.select(:folder_id))
@template_folders = TemplateFolders.search(@template_folders, params[:q])
@template_folders = sort_template_folders(@template_folders)
@pagy, @template_folders = pagy(
@template_folders,
@@ -24,6 +31,7 @@ class TemplatesDashboardController < ApplicationController
else
@template_folders = @template_folders.reject { |e| e.name == TemplateFolder::DEFAULT_NAME }
@templates = filter_templates(@templates)
@templates = sort_templates(@templates)
limit =
if @template_folders.size < 4
@@ -39,7 +47,7 @@ class TemplatesDashboardController < ApplicationController
private
def filter_templates(templates)
rel = templates.active.preload(:author, :template_accesses).order(id: :desc)
rel = templates.active.preload(:author, :template_accesses)
if params[:q].blank?
if Docuseal.multitenant? && !current_account.testing?
@@ -54,4 +62,42 @@ class TemplatesDashboardController < ApplicationController
Templates.search(rel, params[:q])
end
def sort_template_folders(template_folders)
return template_folders.order(id: :desc) if params[:q].present?
case cookies.permanent[:dashboard_templates_order]
when 'recently_used'
sorted_folders =
template_folders.left_joins(templates: :submissions)
.select("template_folders.*, #{LAST_USED_SQL} AS last_used_at")
.group('template_folders.id')
.order(Arel.sql("#{LAST_USED_SQL} DESC NULLS LAST"))
TemplateFolder.from(sorted_folders, :template_folders)
when 'name'
template_folders.order(name: :asc)
else
template_folders.order(id: :desc)
end
end
def sort_templates(templates)
return templates.order(id: :desc) if params[:q].present?
case cookies.permanent[:dashboard_templates_order]
when 'recently_used'
sorted_templates =
templates.left_joins(:submissions)
.select("templates.*, #{LAST_USED_SQL} AS last_used_at")
.group('templates.id')
.order(Arel.sql("#{LAST_USED_SQL} DESC NULLS LAST"))
Template.from(sorted_templates, :templates)
when 'name'
templates.order(name: :asc)
else
templates.order(id: :desc)
end
end
end