Files
docuseal/app/controllers/templates_controller.rb
T

158 lines
5.3 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)
2024-11-23 01:45:10 +02:00
submissions = Submissions::Filter.call(submissions, current_user, params)
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'
2024-11-20 22:49:06 +02:00
2024-11-23 01:45:10 +02:00
submissions = if params[:completed_at_from].present? || params[:completed_at_to].present?
submissions.order(Submitter.arel_table[:completed_at].maximum.desc)
else
submissions.order(id: :desc)
end
2023-10-19 20:19:52 +03:00
2024-11-23 01:45:10 +02:00
@pagy, @submissions = pagy(submissions.preload(submitters: :start_form_submission_events))
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
2024-09-18 19:47:47 +03:00
@template.name = "#{@base_template.name} (#{I18n.t('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],
2024-07-16 01:40:38 +03:00
associations: [schema_documents: [:blob, { preview_images_attachments: :blob }]]
2023-09-17 00:45:57 +03:00
).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
2024-07-16 01:40:38 +03:00
ActiveRecord::Associations::Preloader.new(
records: [@base_template],
associations: [schema_documents: :preview_images_attachments]
).call
2023-12-18 13:38:01 +02:00
@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
enqueue_template_created_webhooks(@template)
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)
enqueue_template_updated_webhooks(@template)
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
2024-09-18 19:47:47 +03:00
I18n.t('template_has_been_removed')
2023-10-29 18:51:58 +02:00
else
2023-12-30 00:37:03 +02:00
@template.update!(archived_at: Time.current)
2023-10-29 18:51:58 +02:00
2024-09-18 19:47:47 +03:00
I18n.t('template_has_been_archived')
2023-10-29 18:51:58 +02:00
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]],
2024-09-12 17:31:37 +03:00
submitters: [%i[name uuid is_requester linked_to_uuid invite_by_uuid email]],
2024-03-23 23:16:40 +02:00
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)
2024-09-26 22:12:58 +03:00
true_user.account_id.to_s == account_id.to_s ||
true_user.account.linked_accounts.accessible_by(current_ability).exists?(id: account_id)
2024-02-15 11:13:05 +02:00
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
2024-09-18 19:47:47 +03:00
redirect_back(fallback_location: root_path, notice: I18n.t('template_has_been_cloned'))
2024-02-13 18:22:16 +02:00
end
end
def enqueue_template_created_webhooks(template)
2024-11-01 15:35:02 +02:00
WebhookUrls.for_account_id(template.account_id, 'template.created').each do |webhook_url|
SendTemplateCreatedWebhookRequestJob.perform_async('template_id' => template.id,
'webhook_url_id' => webhook_url.id)
end
end
def enqueue_template_updated_webhooks(template)
2024-11-01 15:35:02 +02:00
WebhookUrls.for_account_id(template.account_id, 'template.updated').each do |webhook_url|
SendTemplateUpdatedWebhookRequestJob.perform_async('template_id' => template.id,
'webhook_url_id' => webhook_url.id)
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