optimize templates api

pull/220/head^2
Pete Matsyburka 2 years ago
parent 02adbae758
commit b9a906b569

@ -7,10 +7,31 @@ module Api
def index def index
templates = filter_templates(@templates, params) templates = filter_templates(@templates, params)
templates = paginate(templates.preload(:author, documents_attachments: :blob)) templates = paginate(templates.preload(:author, :folder))
schema_documents =
ActiveStorage::Attachment.where(record_id: templates.map(&:id),
record_type: 'Template',
name: :documents,
uuid: templates.flat_map { |t| t.schema.pluck('attachment_uuid') })
.preload(:blob)
preview_image_attachments =
ActiveStorage::Attachment.joins(:blob)
.where(blob: { filename: '0.jpg' })
.where(record_id: schema_documents.map(&:id),
record_type: 'ActiveStorage::Attachment',
name: :preview_images)
.preload(:blob)
render json: { render json: {
data: templates.as_json(serialize_params), data: templates.map do |t|
Templates::SerializeForApi.call(
t,
schema_documents.select { |e| e.record_id == t.id },
preview_image_attachments
)
end,
pagination: { pagination: {
count: templates.size, count: templates.size,
next: templates.last&.id, next: templates.last&.id,
@ -20,7 +41,7 @@ module Api
end end
def show def show
render json: @template.as_json(serialize_params) render json: Templates::SerializeForApi.call(@template)
end end
def update def update
@ -61,14 +82,6 @@ module Api
templates templates
end end
def serialize_params
{
methods: %i[application_key],
include: { author: { only: %i[id email first_name last_name] },
documents: { only: %i[id uuid], methods: %i[url preview_image_url filename] } }
}
end
def template_params def template_params
permit_params = [ permit_params = [
:name, :name,

@ -8,14 +8,6 @@ ActiveSupport.on_load(:active_storage_attachment) do
def signed_uuid def signed_uuid
@signed_uuid ||= ApplicationRecord.signed_id_verifier.generate(uuid, expires_in: 6.hours, purpose: :attachment) @signed_uuid ||= ApplicationRecord.signed_id_verifier.generate(uuid, expires_in: 6.hours, purpose: :attachment)
end end
def preview_image_url
first_page = preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })
return unless first_page
ActiveStorage::Blob.proxy_url(first_page.blob)
end
end end
ActiveSupport.on_load(:active_storage_blob) do ActiveSupport.on_load(:active_storage_blob) do

@ -0,0 +1,33 @@
# frozen_string_literal: true
module Templates
module SerializeForApi
module_function
def call(template, schema_documents = template.schema_documents, preview_image_attachments = [])
json = template.as_json(
methods: %i[application_key],
include: { author: { only: %i[id email first_name last_name] } }
)
json[:folder_name] = template.folder.name
json[:documents] = template.schema.map do |item|
attachment = schema_documents.find { |e| e.uuid == item['attachment_uuid'] }
first_page_blob = preview_image_attachments.find { |e| e.record_id == attachment.id }&.blob
first_page_blob ||= attachment.preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })&.blob
{
id: attachment.id,
uuid: attachment.uuid,
url: ActiveStorage::Blob.proxy_url(attachment.blob),
preview_image_url: first_page_blob && ActiveStorage::Blob.proxy_url(first_page_blob),
filename: attachment.filename
}
end
json
end
end
end
Loading…
Cancel
Save