Files
docuseal/app/controllers/templates_controller.rb
T

61 lines
1.8 KiB
Ruby
Raw Normal View History

2023-05-31 23:19:20 +03:00
# frozen_string_literal: true
class TemplatesController < ApplicationController
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
2023-06-29 02:04:17 +03:00
@template = current_account.templates.active.find(params[:id])
2023-06-28 00:22:34 +03:00
2023-06-29 02:04:17 +03:00
@pagy, @submissions = pagy(@template.submissions.active.preload(:submitters).order(id: :desc))
rescue ActiveRecord::RecordNotFound
redirect_to root_path
2023-05-31 23:19:20 +03:00
end
def new
@template = current_account.templates.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
@template = current_account.templates.preload(documents_attachments: { preview_images_attachments: :blob })
.find(params[:id])
2023-06-18 16:26:51 +00:00
2023-06-28 00:22:34 +03:00
render :edit, layout: 'plain'
end
def create
@template = current_account.templates.new(template_params)
2023-05-31 23:19:20 +03:00
@template.author = current_user
2023-06-28 00:22:34 +03:00
@template.assign_attributes(@base_template.slice(:fields, :schema, :submitters)) if @base_template
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
2023-06-28 00:22:34 +03:00
redirect_to edit_template_path(@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
def destroy
@template = current_account.templates.find(params[:id])
@template.update!(deleted_at: Time.current)
2023-06-28 00:22:34 +03:00
redirect_back(fallback_location: root_path, notice: 'Template has been archived.')
2023-05-31 23:19:20 +03:00
end
private
def template_params
2023-06-16 23:39:55 +03:00
params.require(:template).permit(:name)
2023-05-31 23:19:20 +03:00
end
2023-06-18 16:26:51 +00:00
def load_base_template
return if params[:base_template_id].blank?
@base_template = current_account.templates
.preload(documents_attachments: :preview_images_attachments)
.find_by(id: params[:base_template_id])
end
2023-05-31 23:19:20 +03:00
end