scinote-web/app/serializers/api/v1/result_serializer.rb

29 lines
972 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module Api
module V1
class ResultSerializer < ActiveModel::Serializer
type :results
2023-08-16 16:37:44 +08:00
attributes :name, :archived, :result_text, :result_table, :result_asset
belongs_to :user, serializer: UserSerializer
has_many :result_comments, key: :comments, serializer: CommentSerializer
2023-08-16 16:37:44 +08:00
has_many :result_orderable_elements, key: :result_elements, serializer: ResultOrderableElementSerializer
has_many :assets, serializer: AssetSerializer
include TimestampableModel
2023-08-16 16:37:44 +08:00
def result_text
Api::V1::ResultTextSerializer.new(object.result_texts.first).as_json if object.result_texts.any?
end
def result_table
Api::V1::ResultTableSerializer.new(object.result_tables.first).as_json if object.result_tables.any?
end
def result_asset
Api::V1::ResultAssetSerializer.new(object.result_assets.first).as_json if object.result_assets.any?
end
end
end
end