Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define custom serializer for a class #515

Conversation

jtomaszewski
Copy link

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 added a commit to jtomaszewski/active_model_serializers that referenced this pull request Feb 19, 2014
.. by defining #active_model_serializer method on serialized object. Resolves rails-api#515.
jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Feb 19, 2014
.. by defining #active_model_serializer method on serialized object. Resolves rails-api#515.
@stevenharman
Copy link

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

@jtomaszewski
Copy link
Author

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
Copy link

+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
Copy link

+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
Copy link

loe commented Mar 18, 2014

+1 I need this too.

@JoeStanton
Copy link

👍

jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Apr 11, 2014
.. by defining #active_model_serializer method on serialized object. Resolves rails-api#515.
.. by defining #active_model_serializer method on serialized object. Resolves rails-api#515.
@jtomaszewski
Copy link
Author

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

@steveklabnik
Copy link
Contributor

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

@steveklabnik
Copy link
Contributor

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 added a commit to jtomaszewski/active_model_serializers that referenced this pull request Feb 14, 2015
.. by defining #serializer_class method in serialized object's class. Resolves rails-api#515.
jtomaszewski added a commit to jtomaszewski/active_model_serializers that referenced this pull request Mar 8, 2015
.. by defining #serializer_class method in serialized object's class. Resolves rails-api#515.
imanel added a commit to imanel/active_model_serializers that referenced this pull request May 3, 2015
…izer_class method in serialized object's class. Resolves rails-api#515.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants