adjust shared templates

pull/696/head
Pete Matsyburka 1 month ago
parent 90c1a6f4ad
commit 432435aa23

@ -11,33 +11,32 @@ class TemplatesDashboardController < ApplicationController
helper_method :selected_order
def index
@default_folder = current_account.default_template_folder
@template_folders =
TemplateFolders.filter_active_folders(@template_folders.where(parent_folder_id: nil), @templates)
@template_folders = @template_folders.where.not(id: @default_folder.id) if params[:q].blank?
@template_folders = TemplateFolders.search(@template_folders, params[:q])
@template_folders = TemplateFolders.sort(@template_folders, current_user, selected_order)
@pagy, @template_folders = pagy(
@template_folders,
limit: FOLDERS_PER_PAGE,
page: @template_folders.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD ? params[:page] : 1
)
@shared_templates = Templates.shared(current_user).active
@pagy, @template_folders, @show_default_folder, @show_shared_folder, @show_shared_inline =
load_folders(@template_folders, @templates, @shared_templates)
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).preload(:author, :template_accesses)
@templates = Templates::Order.call(@templates, current_user, selected_order)
limit =
if @template_folders.size < 4
TEMPLATES_PER_PAGE
else
(@template_folders.size < 7 ? 9 : 6)
end
if @show_shared_inline
@templates = @shared_templates.preload(:template_sharings)
else
@templates = @templates.active
@templates = @templates.where(folder_id: @default_folder.id) if params[:q].blank?
end
@pagy, @templates = pagy_auto(@templates, limit:)
@pagy, @templates = load_templates(@templates, @pagy.count)
load_related_submissions if params[:q].present? && @templates.blank?
end
@ -45,28 +44,60 @@ class TemplatesDashboardController < ApplicationController
private
def filter_templates(templates)
rel = templates.active
def load_templates(templates, folders_count)
templates = templates.preload(:author, :template_accesses)
templates = Templates.search(current_user, templates, params[:q])
templates = Templates::Order.call(templates, current_user, selected_order)
if params[:q].blank?
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)
rel = Template.where(
Template.arel_table[:id].in(
rel.where(folder_id: current_account.default_template_folder.id).select(:id).arel
.union(:all, shared_template_ids.arel)
)
)
limit =
if folders_count < 4
TEMPLATES_PER_PAGE
else
rel = rel.where(folder_id: current_account.default_template_folder.id)
(folders_count < 7 ? 9 : 6)
end
pagy_auto(templates, limit:)
end
def load_folders(template_folders, templates, shared_templates)
if params[:q].present?
pagy(template_folders, limit: FOLDERS_PER_PAGE,
page: template_folders.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD ? params[:page] : 1)
else
load_folders_with_pinned(template_folders, templates, shared_templates)
end
end
def load_folders_with_pinned(template_folders, templates, shared_templates)
folders_count = template_folders.count
shared_exists = shared_templates.exists?
default_has_templates = templates.active.exists?(folder_id: current_account.default_template_folder.id)
show_inline_folders =
folders_count + (shared_exists && default_has_templates ? 1 : 0) <= SHOW_TEMPLATES_FOLDERS_THRESHOLD
show_shared_inline = shared_exists && !default_has_templates && show_inline_folders
show_shared_in_grid = shared_exists && !show_shared_inline
show_default_in_grid = !show_inline_folders && default_has_templates
pinned_count = (show_default_in_grid ? 1 : 0) + (show_shared_in_grid ? 1 : 0)
pagy = Pagy::Offset.new(count: folders_count + pinned_count,
page: show_inline_folders ? 1 : [params[:page].to_s.to_i, 1].max,
limit: FOLDERS_PER_PAGE,
raise_range_error: true)
show_default_folder = show_default_in_grid && pagy.page == 1
show_shared_folder = show_shared_in_grid && pagy.page == 1
folder_offset = pagy.page == 1 ? 0 : pagy.offset - pinned_count
folder_limit = pagy.page == 1 ? FOLDERS_PER_PAGE - pinned_count : FOLDERS_PER_PAGE
template_folders = template_folders.offset(folder_offset).limit(folder_limit)
Templates.search(current_user, rel, params[:q])
[pagy, template_folders, show_default_folder, show_shared_folder, show_shared_inline]
end
def selected_order

@ -0,0 +1,16 @@
# frozen_string_literal: true
class TemplatesSharedController < ApplicationController
def index
authorize!(:read, Template)
@templates = Templates.shared(current_user)
.active
.preload(:author, :template_accesses, :template_sharings)
.order(id: :desc)
@templates = Templates.search(current_user, @templates, params[:q])
@pagy, @templates = pagy_auto(@templates, limit: 12)
end
end

@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" class="<%= local_assigns[:class] %>" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M10 19h-5a2 2 0 0 1 -2 -2v-11a2 2 0 0 1 2 -2h4l3 3h7a2 2 0 0 1 2 2v2.5"></path>
<path d="M17.8 20.817l-2.172 1.138a.392 .392 0 0 1 -.568 -.41l.415 -2.411l-1.757 -1.707a.389 .389 0 0 1 .217 -.665l2.428 -.352l1.086 -2.193a.392 .392 0 0 1 .702 0l1.086 2.193l2.428 .352a.39 .39 0 0 1 .217 .665l-1.757 1.707l.414 2.41a.39 .39 0 0 1 -.567 .411l-2.172 -1.138"></path>
</svg>

After

Width:  |  Height:  |  Size: 662 B

@ -0,0 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg" class="<%= local_assigns[:class] %>" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M12 19h-7a2 2 0 0 1 -2 -2v-11a2 2 0 0 1 2 -2h4l3 3h7a2 2 0 0 1 2 2v3.5"></path>
<path d="M19 22v-6"></path>
<path d="M22 19l-3 -3l-3 3"></path>
</svg>

After

Width:  |  Height:  |  Size: 447 B

@ -1,11 +1,12 @@
<% is_long = folder.name.size > 32 %>
<% icon = folder.default? ? 'folder_star' : 'folder' %>
<a href="<%= folder_path(folder) %>" class="flex h-full flex-col justify-between rounded-2xl py-5 px-6 w-full bg-base-200 before:border-2 before:border-base-300 before:border-dashed before:absolute before:left-0 before:right-0 before:top-0 before:bottom-0 before:hidden before:rounded-2xl relative" data-targets="dashboard-dropzone.folderCards" data-full-name="<%= folder.full_name %>">
<% if !is_long %>
<%= svg_icon('folder', class: 'w-6 h-6') %>
<%= svg_icon(icon, class: 'w-6 h-6') %>
<% end %>
<div class="text-lg font-semibold mt-1" style="overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: <%= is_long ? 2 : 1 %>;">
<% if is_long %>
<%= svg_icon('folder', class: 'w-6 h-6 inline') %>
<%= svg_icon(icon, class: 'w-6 h-6 inline') %>
<% end %>
<%= folder.name %>
</div>

@ -0,0 +1,6 @@
<a href="<%= templates_shared_index_path %>" class="flex h-full flex-col justify-between rounded-2xl py-5 px-6 w-full bg-base-200 before:border-2 before:border-base-300 before:border-dashed before:absolute before:left-0 before:right-0 before:top-0 before:bottom-0 before:hidden before:rounded-2xl relative">
<%= svg_icon('folder_up', class: 'w-6 h-6') %>
<div class="text-lg font-semibold mt-1 capitalize" style="overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 1;">
<%= t('shared') %>
</div>
</a>

@ -49,8 +49,14 @@
<%= render 'shared/templates_order_select', with_recently_used: @pagy.count.present? && @pagy.count < 10_000 && !can?(:manage, :countless), selected_order: %>
<% end %>
<% end %>
<% if @template_folders.present? %>
<% if @template_folders.present? || @show_default_folder || @show_shared_folder %>
<div class="grid gap-4 md:grid-cols-3 <%= 'mb-6' if @templates.present? %>">
<% if @show_default_folder %>
<%= render 'template_folders/folder', folder: @default_folder %>
<% end %>
<% if @show_shared_folder %>
<%= render 'template_folders/shared_folder' %>
<% end %>
<%= render partial: 'template_folders/folder', collection: @template_folders, as: :folder %>
</div>
<% end %>

@ -0,0 +1,29 @@
<div>
<%= link_to root_path, class: 'flex items-center' do %>
<%= svg_icon('chevron_left', class: 'w-5 h-5') %>
<span style="margin-left: 3px"><%= t('home') %></span>
<% end %>
</div>
<div class="relative flex justify-between items-center w-full mb-4">
<h1 class="text-2xl truncate md:text-3xl font-bold flex items-center flex-grow min-w-0 space-x-2 md:flex <%= 'hidden' if params[:q].present? %>">
<%= svg_icon('folder', class: 'w-9 h-9 flex-shrink-0') %>
<span class="truncate capitalize"><%= t('shared') %></span>
</h1>
<div class="flex space-x-2">
<% if params[:q].present? || @pagy.pages > 1 %>
<%= render 'shared/search_input' %>
<% end %>
</div>
</div>
<% if @pagy.count.nil? || @pagy.count > 0 %>
<div class="grid gap-4 md:grid-cols-3">
<%= render partial: 'templates/template', collection: @templates %>
</div>
<% elsif params[:q].present? %>
<div class="text-center">
<div class="mt-16 text-3xl font-semibold">
<%= t('templates_not_found') %>
</div>
</div>
<% end %>
<%= render 'shared/pagination', pagy: @pagy, items_name: 'templates' %>

@ -91,6 +91,7 @@ Rails.application.routes.draw do
resource :templates_upload, only: %i[show], path: 'new'
end
resources :templates_archived, only: %i[index], path: 'templates/archived'
resources :templates_shared, only: %i[index], path: 'templates/shared'
resources :folders, only: %i[show edit update destroy], controller: 'template_folders'
resources :template_sharings_testing, only: %i[create]
resources :templates, only: %i[index], controller: 'templates_dashboard'

@ -4,16 +4,8 @@ module Abilities
module TemplateConditions
module_function
def collection(user, ability: nil)
templates = Template.where(account_id: user.account_id)
return templates unless user.account.testing?
shared_ids =
TemplateSharing.where({ ability:, account_id: [user.account_id, TemplateSharing::ALL_ID] }.compact)
.select(:template_id)
Template.where(Template.arel_table[:id].in(templates.select(:id).arel.union(:all, shared_ids.arel)))
def collection(user)
Template.where(account_id: user.account_id)
end
def entity(template, user:, ability: nil)

@ -47,6 +47,21 @@ module Templates
nil
end
def shared(current_user)
account = current_user.account
return Template.none if Docuseal.multitenant? ? !account.testing? : !account.linked_account_account
shared_account_ids = [current_user.account_id]
shared_account_ids << TemplateSharing::ALL_ID if !Docuseal.multitenant? && !account.testing?
exists_access = TemplateAccess.where(TemplateAccess.arel_table[:template_id].eq(Template.arel_table[:id]))
.select(1).arel.exists
Template.where(id: TemplateSharing.where(account_id: shared_account_ids).select(:template_id))
.where.not(exists_access)
end
def search(current_user, templates, keyword)
if Docuseal.fulltext_search?
fulltext_search(current_user, templates, keyword)

Loading…
Cancel
Save