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

Dynamic serializer selection #1719

Closed
KevinColemanInc opened this issue May 7, 2016 · 6 comments
Closed

Dynamic serializer selection #1719

KevinColemanInc opened this issue May 7, 2016 · 6 comments

Comments

@KevinColemanInc
Copy link

KevinColemanInc commented May 7, 2016

Currently the serializer param on has_x takes an object.

It would be cool if it could accept a method name as well.

Use case:

Dynamically setting the serializer for polymorphic relationships:

has_one :user, polymorphic: true, serializer: :serializer_picker

def serializer_picker
   if user.class == 'Admin'
     SparseAdminSerializer
   end
 end
@cgmckeever
Copy link
Contributor

cgmckeever commented May 8, 2016

cant you just do this as an attribute then and make a method for said attribute?

class SomeSerializer < ApplicationSerializer
  attributes :id, :somefakerelationship

  def somefakerelationship
     ## do stuff
    ## profit?
  end

I think if the method generates a model, that models serializer should spawn?

@KevinColemanInc
Copy link
Author

KevinColemanInc commented May 12, 2016

@cgmckeever true, but I want to specify the serializer of the thing that spawned. For my particular use case, I am have a model called "Notification" It has a source_obj polymorphic relationship on it such that many different models can be a source_obj.

The default model's serializer is often too big for what I want. So I want to the source_obj to have use special "sparse" serializers for each object. e.g. sparse_user_serializer, sparse_profile_serializer, etc. etc. instead of the default model serializer of user_serializer or profile_serializer.

does that make sense?

I think my hack to fix this will be to do:

class SomeSerializer < ApplicationSerializer
  attributes :id, :source_obj

  def source_obj
    object.source_obj.as_json
  end
end

so I am at least not accidently getting any N+1s

@groyoh
Copy link
Member

groyoh commented May 12, 2016

@KevinColemanInc you can use this:

class MySerializer < ActiveModel::Serializer
  def self.serializer_for(model, options)
    return SparseAdminSerializer if model.class == 'Admin'
    super
  end

  # the rest of the serializer
end

At the moment there is no better solution available.

@cgmckeever
Copy link
Contributor

@groyoh NICE!

@KevinColemanInc
Copy link
Author

KevinColemanInc commented May 13, 2016

Cool! I will use that.

@beauby
Copy link
Contributor

beauby commented May 16, 2016

We should document this. PR anyone?

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

No branches or pull requests

4 participants