Active_model_serializers: Using serializers outside of ActionController

Created on 4 Aug 2015  路  5Comments  路  Source: rails-api/active_model_serializers

1) It would be awesome if the docs could be updated to show how to use AMS outside of a controller. If you want to do some checking in the console, it's not clear how to view serializer output. I believe it comes down to creating the eg. ArticleSerializer object and passing it to an ie. FlatternJsonAdapter but I can never remember how to do it - and I can't quickly find it even now.

2) Is there a way to get the default serializer for a class, like how render json: @article automatically knows how to get the ArticleSerializer? I tried looking through the source for AMS but got lost. It seems the get_serializer method might be the key but it seems to only be able to be called from within a controller.

Most helpful comment

Found the answers:

1) Using AMS outside of controllers:

serializer = ArticleSerializer.new(article)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
adapter.to_json # or adapter.as_json or adapter.serializable_attributes

2) Default serializer for a class

serializer_klass = ActiveModel::Serializer.serializer_for(article) # or an articles collection, which gives back the ArraySerializer class
serializer = serializer_klass.new(article)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
adapter.to_json # or adapter.as_json or adapter.serializable_attributes

I'm leaving this issue open because I think this should be in the README

All 5 comments

Found the answers:

1) Using AMS outside of controllers:

serializer = ArticleSerializer.new(article)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
adapter.to_json # or adapter.as_json or adapter.serializable_attributes

2) Default serializer for a class

serializer_klass = ActiveModel::Serializer.serializer_for(article) # or an articles collection, which gives back the ArraySerializer class
serializer = serializer_klass.new(article)
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
adapter.to_json # or adapter.as_json or adapter.serializable_attributes

I'm leaving this issue open because I think this should be in the README

Hey @yjukaku, first of all thanks you for your issue!

Indeed we need to update our docs! I'm glad to announce that we started working on an official docs (that we are not kind spreading yet), but you can check it here.

The code you posted above was right, but we have updated the way you use serializer in other to make it easier to use outside the controller.

ActiveModel::SerializableResource.serialize(resource, options)

You can check the methods here

In order to get a serializer to a resource you can use the Serializer class

Serializer.serializer_for(resource, options={})

It would be awesome if you could write a piece of doc on our new docs covering how to use serializer outside the controller based on your experience! I'll keep it open until them.

Hey there.

Has anybody done this yet? Looking to knock out a few documentation requests in the next few days. Been using AMS inside and outside controllers on a current project. Just need to know what needs to be highlighted!

Amazing @CodedBeardedSignedTaylor!
I'm looking forward to have as many docs as possible, make sure to use the conventions on our new doc.
I think we discuss topics in other issue maybe a more generic one? Make yourself comfortable to create it.
I'm closing this one.

How can I pass current_user to serializer in rails console?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

greggawatt picture greggawatt  路  4Comments

kapso picture kapso  路  4Comments

attenzione picture attenzione  路  4Comments

AlexCppns picture AlexCppns  路  5Comments

adamcrown picture adamcrown  路  4Comments