Responsáveis por pegarem as entidades, colunas do banco, e transforma-os em JSON.
Para gerar um serializer, basta executar o seguinte comando abaixo:
rails g serializer api/v1/nomedoserializer
Segue abaixo o exemplo de um serializer completo:
class Api::V1::WatchableSerializer
include FastJsonapi::ObjectSerializer
attributes :id, :title, :description, :thumbnail_key, :featured_thumbnail_key
attribute :type do |object|
object.model name
end
attribute :favorite do |object, params|
if params.present? && params.has key?(:user)
params[:user].favorites.where(favoritable: object).exists?
end
end
attribute :video key do |object|
if object[:video key].present?
object.video key
end
end
attribute :featured_thumbnail_key do |object|
if object[:featured_thumbnail_key].present?
object.:featured_thumbnail_key
end
end
end
O serializer acima, é um pouco mais complexo do que o usual, para ter um serializer default, sem atributos extras, ficaria da seguinte maneira:
class Api::V1::WatchableSerializer
include FastJsonapi::ObjectSerializer
attributes :id, :title, :description, :thumbnail_key, :featured_thumbnail_key
end