diff --git a/lib/templates/serialize_for_api.rb b/lib/templates/serialize_for_api.rb index fd18de77..9a014aec 100644 --- a/lib/templates/serialize_for_api.rb +++ b/lib/templates/serialize_for_api.rb @@ -9,9 +9,17 @@ module Templates module_function - def call(template, schema_documents = template.schema_documents, preview_image_attachments = []) + def call(template, schema_documents = template.schema_documents.preload(:blob), preview_image_attachments = nil) json = template.as_json(SERIALIZE_PARAMS) + 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) + json[:documents] = template.schema.map do |item| attachment = schema_documents.find { |e| e.uuid == item['attachment_uuid'] }