Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Define custom serializer for a class #515

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
8 participants

How to set a serializer for given class, if I don't want to create another serializer with "xxxSerializer" name?

f.e.

class AbstractImage
  def serializer_class
    ImageSerializer
  end
end

class AvatarImage < AbstractImage; end
class CoverImage < AbstractImage; end

# I want all of those classes to be serialized with ImageSerializer, not AbstractImageSerializer, AvatarImageSerializer or DefaultSerializer

AFAIR there used to be #serializer_class method, but seems like it got removed from ActiveModel::Serializer ? Why?

If there's no particular reason, I'll send a PR fixing it back.

@jtomaszewski jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Feb 19, 2014

@jtomaszewski jtomaszewski Allow to define custom serializer for given class
.. by defining #active_model_serializer method on serialized object. Resolves #515.
86a8873

@jtomaszewski jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Feb 19, 2014

@jtomaszewski jtomaszewski Allow to define custom serializer for given class
.. by defining #active_model_serializer method on serialized object. Resolves #515.
8d20fcd

The #active_model_serializer factory method on every object has been replaced by a single factory method, ActiveModel::Serializer#serializer_for.

Yeah, it's okay, but it forces me to create new serializer for every single class, which is unneeded when I f.e. use inheritance with my classes.

See my little commit - changes nothing but allows me to be DRY.

+1
As @jtomaszewski says it'd be so useful for STI models where all descendants can be serialized with same serializer - you would specify it once in base class and then it wouldn't be needed to create serializer class for each descendant and specify serializer in all respond_with calls.

I'd use method #serializer_class too keep convention same as Pundit with its policy_class or Draper with its decorator_class.

+1 👍

Better to configure than to pollute the project with files one doesn't need. I'd go the serializer_class way too.

@xymbol xymbol self-assigned this Mar 12, 2014

loe commented Mar 18, 2014

+1 I need this too.

👍

@jtomaszewski jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Apr 11, 2014

@jtomaszewski jtomaszewski Allow to define custom serializer for given class
.. by defining #active_model_serializer method on serialized object. Resolves #515.
c043b1d
@jtomaszewski jtomaszewski Allow to define custom serializer for given class
.. by defining #active_model_serializer method on serialized object. Resolves #515.
4820e0f

I changed this into #serializer_class and rebased to origin/master. Merge it, please? ; P

Contributor

steveklabnik commented Jul 28, 2014

This is a very tiny patch, so I don't mind adding it. Can I get at least one test though, please?

Contributor

steveklabnik commented Aug 22, 2014

I'd like to merge this, but I need a test, plus it needs to be rebased. Please rebase, add a test, and send to the 0-9-stable branch I'm making in a few hours, thanks!

@jtomaszewski jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Feb 14, 2015

@jtomaszewski @jtomaszewski jtomaszewski + jtomaszewski Allow to define custom serializer for given class
.. by defining #serializer_class method in serialized object's class. Resolves #515.
8e6a0e8

@jtomaszewski jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Mar 8, 2015

@jtomaszewski @jtomaszewski jtomaszewski + jtomaszewski Allow to define custom serializer for given class
.. by defining #serializer_class method in serialized object's class. Resolves #515.
d8b78a3

@imanel imanel added a commit to imanel/active_model_serializers that referenced this pull request May 3, 2015

@imanel imanel Allow to define custom serializer for given class by defining #serial…
…izer_class method in serialized object's class. Resolves #515.
c91b649
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment