Files
docuseal/app/controllers/api/templates_controller.rb
T

66 lines
1.9 KiB
Ruby
Raw Normal View History

2023-05-31 23:19:20 +03:00
# frozen_string_literal: true
module Api
class TemplatesController < ApiBaseController
2023-09-17 00:45:57 +03:00
load_and_authorize_resource :template
2023-08-11 18:32:33 +03:00
2023-08-04 00:40:13 +03:00
def index
2023-10-01 01:57:47 +03:00
templates = Templates.search(@templates, params[:q])
templates = params[:archived] ? templates.archived : templates.active
2023-11-02 19:33:01 +02:00
templates = templates.where(application_key: params[:application_key]) if params[:application_key].present?
2023-11-29 20:23:35 +02:00
templates = templates.joins(:folder).where(folder: { name: params[:folder] }) if params[:folder].present?
2023-10-01 01:57:47 +03:00
templates = paginate(templates.preload(:author, documents_attachments: :blob))
render json: {
data: templates.as_json(serialize_params),
pagination: {
count: templates.size,
next: templates.last&.id,
prev: templates.first&.id
}
}
2023-08-04 00:40:13 +03:00
end
2023-08-11 18:32:33 +03:00
def show
2023-10-01 01:57:47 +03:00
render json: @template.as_json(serialize_params)
2023-08-11 18:32:33 +03:00
end
2023-05-31 23:19:20 +03:00
2023-08-11 18:32:33 +03:00
def update
2023-10-01 01:57:47 +03:00
if (folder_name = params.dig(:template, :folder_name))
@template.folder = TemplateFolders.find_or_create_by_name(current_user, folder_name)
end
2023-05-31 23:19:20 +03:00
@template.update!(template_params)
2023-10-01 01:57:47 +03:00
render json: @template.as_json(only: %i[id updated_at])
end
def destroy
@template.update!(deleted_at: Time.current)
render json: @template.as_json(only: %i[id deleted_at])
2023-05-31 23:19:20 +03:00
end
private
2023-10-01 01:57:47 +03:00
def serialize_params
{
include: { author: { only: %i[id email first_name last_name] },
documents: { only: %i[id uuid], methods: %i[url filename] } }
}
end
2023-05-31 23:19:20 +03:00
def template_params
2023-11-05 01:41:54 +02:00
params.require(:template).permit(
:name,
schema: [%i[attachment_uuid name]],
submitters: [%i[name uuid]],
fields: [[:uuid, :submitter_uuid, :name, :type, :required, :readonly, :default_value,
2023-11-13 01:46:28 +02:00
{ options: [%i[value uuid]], areas: [%i[x y w h cell_w attachment_uuid option_uuid page]] }]]
2023-11-05 01:41:54 +02:00
)
2023-05-31 23:19:20 +03:00
end
end
end