diff --git a/app/controllers/api/templates_controller.rb b/app/controllers/api/templates_controller.rb index 61d45a8b..b8be0e3a 100644 --- a/app/controllers/api/templates_controller.rb +++ b/app/controllers/api/templates_controller.rb @@ -7,10 +7,31 @@ module Api def index 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: { - 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: { count: templates.size, next: templates.last&.id, @@ -20,7 +41,7 @@ module Api end def show - render json: @template.as_json(serialize_params) + render json: Templates::SerializeForApi.call(@template) end def update @@ -61,14 +82,6 @@ module Api templates 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 permit_params = [ :name, diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb index d7a0d458..ef04d0ac 100644 --- a/config/initializers/active_storage.rb +++ b/config/initializers/active_storage.rb @@ -8,14 +8,6 @@ ActiveSupport.on_load(:active_storage_attachment) do def signed_uuid @signed_uuid ||= ApplicationRecord.signed_id_verifier.generate(uuid, expires_in: 6.hours, purpose: :attachment) 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 ActiveSupport.on_load(:active_storage_blob) do diff --git a/lib/templates/serialize_for_api.rb b/lib/templates/serialize_for_api.rb new file mode 100644 index 00000000..47c3c3dc --- /dev/null +++ b/lib/templates/serialize_for_api.rb @@ -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