Migrating from ActiveModelSerializers to Representable

Nadiya Karachevska
Nov 14, 2018 · 3 min read

At Innocode, we use active_model_serializers gem for one of our products and one day it became no longer supported. Therefore we made a decision to look for another gem to replace it.

There are many great articles on the Internet what choices do you have if the project uses JSON API format for API responses. But what alternatives are there if you use a default ActiveModel::Serializer config adapter :attributes instead of :json_api?

I decided to start with alternatives mentioned at the ActiveModelSerializers gem page. The first three (jsonapi-rb, fast_jsonapi and jsonapi-resources) don’t support changing the format (they offer only JSON API), so they were not suitable for our needs. The last one, blueprinter, does not have the possibility to wrap the response (for example, in data), also not good.

And then I recalled, that the other Innocode project I work with uses Trailblazer and the part of it, Representable gem, felt like just what I was looking for.

Moving from ActiveModelSerializers to Representable took only a few steps that are described below.

Inheritance

First of all, you need to inherit from Representable::Decorator instead of ActiveModel::Serializer and include Representable::JSON module. It’s very easy to do so by creating the base decorator class and inheriting all the existing serializers from it.

Before:

After:

Rendering JSON

Representable doesn’t automatically detect which serializer to use. You have to explicitly define it.

Before:

After:

Attributes

The next step is to update the attributes declaration of the object. With ActiveModelSerializers we listed all of them with the attributes option. Now we need to declare one at a line with a property option.

Before:

After:

The object inside this serializer can be accessed by represented and all the properties are usually called on it. But what should you do if you want to call some property on the decorator instead, the helper method? Use :exec_context.

Before:

After:

Wrapping

root becomes representation_wrap.

Before:

After:

There’s also an ability to pass the wrap dynamically, as representation_wrap is a dynamic function option.

Associations

With Representable, has_one association is declared with the same property, has_many— with collection option. Note that you can turn the wrap off.

Before:

After:

ActiveModelSerializers magic

ActiveModelSerializers gem is able to create a serialized collection given a serializer for a singular instance. Representable doesn’t know how to do it automatically, separate serializer for collection should be created.

Before:

After:

External parameter

Sometimes object attribute depends on the additional options, that can be passed alongside.

Before:

After:

These are the only changes needed to switch from ActiveModelSerializers to Representable in our project.

Representable is a great choice to select among the other options, as it has very similar API to ActiveModelSerializers and it’s not supposed to be challenging to start using it instead.

Check out the full documentation on Representable if you got interested!

Innocode Stories

Posts by folks who make Innocode

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store