Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Define custom serializer for a class #515

Closed
wants to merge 1 commit into from

8 participants

@jtomaszewski

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 referenced this pull request from a commit in jtomaszewski/active_model_serializers
@jtomaszewski jtomaszewski Allow to define custom serializer for given class
.. by defining #active_model_serializer method on serialized object. Resolves #515.
86a8873
@jtomaszewski jtomaszewski referenced this pull request from a commit in jtomaszewski/active_model_serializers
@jtomaszewski jtomaszewski Allow to define custom serializer for given class
.. by defining #active_model_serializer method on serialized object. Resolves #515.
8d20fcd
@stevenharman

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

@jtomaszewski

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.

@romansklenar

+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.

@AxisOfEval

+1 :thumbsup:

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
@loe

+1 I need this too.

@jtomaszewski jtomaszewski referenced this pull request from a commit in jtomaszewski/active_model_serializers
@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
@jtomaszewski

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

@steveklabnik

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

@steveklabnik

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!

@xymbol xymbol was unassigned by jtomaszewski
@xymbol xymbol was unassigned by jtomaszewski
@jtomaszewski jtomaszewski referenced this pull request from a commit in jtomaszewski/active_model_serializers
@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 referenced this pull request from a commit in jtomaszewski/active_model_serializers
@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 referenced this pull request from a commit in imanel/active_model_serializers
@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
Commits on Apr 11, 2014
  1. @jtomaszewski

    Allow to define custom serializer for given class

    jtomaszewski authored
    .. by defining #active_model_serializer method on serialized object. Resolves #515.
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 0 deletions.
  1. +2 −0  lib/active_model/serializer.rb
View
2  lib/active_model/serializer.rb
@@ -42,6 +42,8 @@ def embed(type, options={})
def serializer_for(resource)
if resource.respond_to?(:to_ary)
ArraySerializer
+ elsif resource.respond_to?(:serializer_class)
+ resource.serializer_class
else
begin
Object.const_get "#{resource.class.name}Serializer"
Something went wrong with that request. Please try again.