From 960d6681ff4bd4fa80f3538dd8b3792b79d8fc97 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Thu, 3 Jun 2021 11:40:01 +0200 Subject: [PATCH] Add docx and pdf attributes to API report serializer [SCI-5640] --- app/serializers/api/v1/report_serializer.rb | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/serializers/api/v1/report_serializer.rb b/app/serializers/api/v1/report_serializer.rb index 94b666b22..487fea145 100644 --- a/app/serializers/api/v1/report_serializer.rb +++ b/app/serializers/api/v1/report_serializer.rb @@ -3,11 +3,33 @@ module Api module V1 class ReportSerializer < ActiveModel::Serializer + include Rails.application.routes.url_helpers + type :reports attributes :id, :name, :description + attribute :pdf_file_size, if: -> { object.pdf_file.attached? } + attribute :pdf_file_url, if: -> { object.pdf_file.attached? } + attribute :docx_file_size, if: -> { object.docx_file.attached? } + attribute :docx_file_url, if: -> { object.docx_file.attached? } belongs_to :user, serializer: UserSerializer belongs_to :project, serializer: ProjectSerializer, unless: -> { instance_options[:hide_project] } + + def pdf_file_size + object.pdf_file.blob.byte_size + end + + def pdf_file_url + rails_blob_path(object.pdf_file, disposition: 'attachment') + end + + def docx_file_size + object.docx_file.blob.byte_size + end + + def docx_file_url + rails_blob_path(object.docx_file, disposition: 'attachment') + end end end end