How to organize serializers using fastjsonapi.?

340 Views Asked by At

My application has following versions: rails 6.0.0, ruby 2.6.3.

I have around 20 controllers in the application and we have planned to use fast-json-api instead of rabl as serializer in the app. I found the number of serializer files growing very fast for each controller. (screenshot below).


Questions

Is there a better way to organise all the serializers?

I have tried the following 2 ways to organise:

  1. Organizing all serializers of one controller under its model-name (like rabl)

    serializer

    But it gave me this error:
    NameError (uninitialized constant Api::V1::LexiconsController::ListNodeSerializer)

  2. Can I render different api view in single Serializer, by keeping all the SerializerClassNames in the same serialzer file for that controller??

    Serializer for Category Controller

    class ShowSerializer < ApplicationSerializer
      cache_options enabled: true, cache_length: 24.hours

      attributes :name, :status, :description
    end

    class IndexSerializer < ApplicationSerializer
      attributes :name, :status, :thumbnail, :definition

      attributes :area do |area|
        area.country.try(:name).try(:html_safe)
      end
    end
0

There are 0 best solutions below