Files
docuseal/app/controllers/templates_controller.rb
T

133 lines
4.1 KiB
Ruby
Raw Normal View History

2023-05-31 23:19:20 +03:00
# frozen_string_literal: true
class TemplatesController < ApplicationController
2023-09-17 00:45:57 +03:00
load_and_authorize_resource :template
2023-06-18 16:26:51 +00:00
before_action :load_base_template, only: %i[new create]
2023-05-31 23:19:20 +03:00
def show
submissions = @template.submissions.accessible_by(current_ability)
2023-12-30 00:37:03 +02:00
submissions = submissions.active if @template.archived_at.blank?
2024-03-18 20:16:26 +02:00
submissions = Submissions.search(submissions, params[:q], search_values: true)
2023-06-28 00:22:34 +03:00
2023-10-19 20:19:52 +03:00
@base_submissions = submissions
submissions = submissions.pending if params[:status] == 'pending'
submissions = submissions.completed if params[:status] == 'completed'
2023-09-15 00:42:51 +03:00
@pagy, @submissions = pagy(submissions.preload(:submitters).order(id: :desc))
2023-06-29 02:04:17 +03:00
rescue ActiveRecord::RecordNotFound
redirect_to root_path
2023-05-31 23:19:20 +03:00
end
def new
2023-06-18 16:26:51 +00:00
@template.name = "#{@base_template.name} (Clone)" if @base_template
2023-05-31 23:19:20 +03:00
end
2023-06-28 00:22:34 +03:00
def edit
2023-09-17 00:45:57 +03:00
ActiveRecord::Associations::Preloader.new(
records: [@template],
associations: [schema_documents: { preview_images_attachments: :blob }]
).call
2023-06-18 16:26:51 +00:00
2023-10-22 01:54:20 +03:00
@template_data =
@template.as_json.merge(
documents: @template.schema_documents.as_json(
2024-02-17 22:14:28 +02:00
methods: %i[metadata signed_uuid],
2023-10-22 01:54:20 +03:00
include: { preview_images: { methods: %i[url metadata filename] } }
)
).to_json
2023-06-28 00:22:34 +03:00
render :edit, layout: 'plain'
end
def create
2023-12-18 13:38:01 +02:00
if @base_template
@template = Templates::Clone.call(@base_template, author: current_user,
name: params.dig(:template, :name),
folder_name: params[:folder_name])
else
@template.author = current_user
@template.folder = TemplateFolders.find_or_create_by_name(current_user, params[:folder_name])
end
2023-05-31 23:19:20 +03:00
2024-02-15 11:13:05 +02:00
if params[:account_id].present? && authorized_clone_account_id?(params[:account_id])
2024-02-13 18:22:16 +02:00
@template.account_id = params[:account_id]
2024-06-21 14:10:27 +03:00
@template.folder = @template.account.default_template_folder if @template.account_id != current_account.id
2024-02-13 18:22:16 +02:00
else
@template.account = current_account
end
2023-05-31 23:19:20 +03:00
if @template.save
2023-06-18 16:26:51 +00:00
Templates::CloneAttachments.call(template: @template, original_template: @base_template) if @base_template
2024-06-22 10:36:32 +03:00
SendTemplateUpdatedWebhookRequestJob.perform_later('template_id' => @template.id)
2024-05-12 11:37:33 +03:00
2024-02-13 18:22:16 +02:00
maybe_redirect_to_template(@template)
2023-05-31 23:19:20 +03:00
else
render turbo_stream: turbo_stream.replace(:modal, template: 'templates/new'), status: :unprocessable_entity
end
end
2024-03-23 23:16:40 +02:00
def update
@template.update!(template_params)
2024-06-22 10:36:32 +03:00
SendTemplateUpdatedWebhookRequestJob.perform_later('template_id' => @template.id)
2024-05-12 11:37:33 +03:00
2024-03-23 23:16:40 +02:00
head :ok
end
2023-05-31 23:19:20 +03:00
def destroy
2023-10-29 18:51:58 +02:00
notice =
if params[:permanently].present?
2023-10-29 18:51:58 +02:00
@template.destroy!
2023-05-31 23:19:20 +03:00
Rollbar.info("Remove template: #{@template.id}") if defined?(Rollbar)
2023-10-29 18:51:58 +02:00
'Template has been removed.'
else
2023-12-30 00:37:03 +02:00
@template.update!(archived_at: Time.current)
2023-10-29 18:51:58 +02:00
'Template has been archived.'
end
redirect_back(fallback_location: root_path, notice:)
2023-05-31 23:19:20 +03:00
end
private
2024-03-23 23:16:40 +02:00
def template_params
params.require(:template).permit(
:name,
{ schema: [%i[attachment_uuid name]],
submitters: [%i[name uuid]],
fields: [[:uuid, :submitter_uuid, :name, :type,
:required, :readonly, :default_value,
:title, :description,
{ preferences: {},
conditions: [%i[field_uuid value action]],
options: [%i[value uuid]],
validation: %i[message pattern],
areas: [%i[x y w h cell_w attachment_uuid option_uuid page]] }]] }
)
end
2024-02-15 11:13:05 +02:00
def authorized_clone_account_id?(account_id)
true_user.account_id.to_s == account_id.to_s || true_user.account.linked_accounts.exists?(id: account_id)
end
2024-02-13 18:22:16 +02:00
def maybe_redirect_to_template(template)
if template.account == current_account
redirect_to(edit_template_path(@template))
else
redirect_back(fallback_location: root_path, notice: 'Template has been clonned')
end
end
2023-06-18 16:26:51 +00:00
def load_base_template
return if params[:base_template_id].blank?
@base_template = Template.accessible_by(current_ability).find_by(id: params[:base_template_id])
2023-06-18 16:26:51 +00:00
end
2023-05-31 23:19:20 +03:00
end