Files
docuseal/app/controllers/templates_dashboard_controller.rb
T

97 lines
3.4 KiB
Ruby
Raw Normal View History

2024-04-03 00:59:25 +03:00
# frozen_string_literal: true
class TemplatesDashboardController < ApplicationController
load_and_authorize_resource :template_folder, parent: false
load_and_authorize_resource :template, parent: false
SHOW_TEMPLATES_FOLDERS_THRESHOLD = 9
TEMPLATES_PER_PAGE = 12
FOLDERS_PER_PAGE = 18
def index
2025-01-31 23:29:01 +02:00
@template_folders = @template_folders.where(id: @templates.active.select(:folder_id))
2024-12-09 21:14:31 +02:00
@template_folders = TemplateFolders.search(@template_folders, params[:q])
2025-04-23 12:16:50 +03:00
@template_folders = sort_template_folders(@template_folders, current_user,
cookies.permanent[:dashboard_templates_order])
2024-04-03 00:59:25 +03:00
@pagy, @template_folders = pagy(
@template_folders,
2025-05-28 22:48:15 +03:00
limit: FOLDERS_PER_PAGE,
2024-04-03 00:59:25 +03:00
page: @template_folders.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD ? params[:page] : 1
)
if @pagy.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD
@templates = @templates.none
else
@template_folders = @template_folders.reject { |e| e.name == TemplateFolder::DEFAULT_NAME }
@templates = filter_templates(@templates)
2025-04-23 12:16:50 +03:00
@templates = Templates::Order.call(@templates, current_user, cookies.permanent[:dashboard_templates_order])
2024-04-03 00:59:25 +03:00
2024-10-04 16:37:41 +03:00
limit =
2024-04-03 00:59:25 +03:00
if @template_folders.size < 4
TEMPLATES_PER_PAGE
else
(@template_folders.size < 7 ? 9 : 6)
end
2025-05-28 22:48:15 +03:00
@pagy, @templates = pagy_auto(@templates, limit:)
2024-04-03 00:59:25 +03:00
end
end
private
def filter_templates(templates)
2025-01-31 23:29:01 +02:00
rel = templates.active.preload(:author, :template_accesses)
2024-04-03 00:59:25 +03:00
if params[:q].blank?
2025-05-11 16:11:02 +03:00
if Docuseal.multitenant? ? current_account.testing? : current_account.linked_account_account
shared_account_ids = [current_user.account_id]
shared_account_ids << TemplateSharing::ALL_ID if !Docuseal.multitenant? && !current_account.testing?
shared_template_ids = TemplateSharing.where(account_id: shared_account_ids).select(:template_id)
2024-04-03 00:59:25 +03:00
rel = rel.where(folder_id: current_account.default_template_folder.id).or(rel.where(id: shared_template_ids))
2025-05-11 16:11:02 +03:00
else
rel = rel.where(folder_id: current_account.default_template_folder.id)
2024-04-03 00:59:25 +03:00
end
end
Templates.search(rel, params[:q])
end
2025-01-31 23:29:01 +02:00
2025-04-23 12:16:50 +03:00
def sort_template_folders(template_folders, current_user, order)
2025-04-22 16:32:09 +03:00
case order
when 'used_at'
subquery =
Template.left_joins(:submissions)
.group(:folder_id)
2025-04-23 12:16:50 +03:00
.where(account_id: current_user.account_id)
2025-04-22 16:32:09 +03:00
.select(
:folder_id,
Template.arel_table[:updated_at].maximum.as('updated_at_max'),
Submission.arel_table[:created_at].maximum.as('submission_created_at_max')
)
template_folders = template_folders.joins(
Template.arel_table
.join(subquery.arel.as('templates'), Arel::Nodes::OuterJoin)
.on(TemplateFolder.arel_table[:id].eq(Template.arel_table[:folder_id]))
.join_sources
)
template_folders.order(
Arel::Nodes::Case.new
.when(Template.arel_table[:submission_created_at_max].gt(Template.arel_table[:updated_at_max]))
.then(Template.arel_table[:submission_created_at_max])
.else(Template.arel_table[:updated_at_max])
.desc
)
2025-01-31 23:29:01 +02:00
when 'name'
template_folders.order(name: :asc)
else
template_folders.order(id: :desc)
end
end
2024-04-03 00:59:25 +03:00
end