Files
docuseal/app/controllers/templates_dashboard_controller.rb
T

58 lines
1.7 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
2024-12-09 21:14:31 +02:00
@template_folders = @template_folders.where(id: @templates.active.select(:folder_id)).order(id: :desc)
@template_folders = TemplateFolders.search(@template_folders, params[:q])
2024-04-03 00:59:25 +03:00
@pagy, @template_folders = pagy(
@template_folders,
items: FOLDERS_PER_PAGE,
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)
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
2024-10-04 16:37:41 +03:00
@pagy, @templates = pagy(@templates, limit:)
2024-04-03 00:59:25 +03:00
end
end
private
def filter_templates(templates)
2024-12-08 01:37:20 +02:00
rel = templates.active.preload(:author, :template_accesses).order(id: :desc)
2024-04-03 00:59:25 +03:00
if params[:q].blank?
if Docuseal.multitenant? && !current_account.testing?
rel = rel.where(folder_id: current_account.default_template_folder.id)
else
shared_template_ids =
TemplateSharing.where(account_id: [current_account.id, TemplateSharing::ALL_ID]).select(:template_id)
rel = rel.where(folder_id: current_account.default_template_folder.id).or(rel.where(id: shared_template_ids))
end
end
Templates.search(rel, params[:q])
end
end