-
Notifications
You must be signed in to change notification settings - Fork 63
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
Setting custom classes #68
Comments
Hi @ZempTime
render jsonapi: @relationship, class: { 'Relationship::Approve': SerializableRelationship }
def jsonapi_class
super.merge(
'Relationship::Approve': SerializableRelationship
)
end
Override
The key is the class name of the object you are passing to
The value is a Serializable class (usually a subclass of
Not sure I understand but I'd say no: the models are not modified, and the serializable classes are just instantiated.
I agree and that would be of great help! (: |
Oh my gosh! So helpful! I'm not doing Ruby/Rails full time anymore, but next coding session I'll sit down and get a PR in before starting on my personal stuff. |
One thing that I am struggling with is how to "duck-type" the rendering; for example, I have more than one class (specifically, one is a model, the other is a double) that both respond to the same API and I want to render with the same subclass of |
@bprotas Yes, you were almost there: the render jsonapi: users, class: jsonapi_class.merge(Article: MyCustomSerializableArticle) or overriding def jsonapi_class
super.merge(Article: MyCustomSerializableArticle)
end |
Thanks @beauby ; I appreciate the response. This isn't quite what I was looking for - it assumes that here my class is always I ended up with this line of code to always use the same serializer, regardless of the class of object I'm serializing: render jsonapi: article, class: {article.class.name.to_sym => MyCustomSerializableArticle} This feels like a bit of a hack; I would rather not have to use the hash: render jsonapi: article, class: MyCustomSerializableArticle I might still be misunderstanding how the library is supposed to work? Thank you again for the quick response! |
@bprotas I see. The json:api standard is based on the idea of serializing a graph of resources, so the general case is that one needs to map each resource type to a serializer. render jsonapi: article, class: Hash.new { |h, k| h[k] = MyCustomSerializableArticle } i.e. use a hash with a default value, or even render jsonapi: article, class: ->(_) { MyCustomSerializableArticle } using a lambda. |
ah interesting - that is much cleaner, thank you! |
I've created a PR that allows customizing the default serializer mappings in the config file. #92 I think it makes sense for situations like this one. i.e. STI models normally will use the same serializer so would be good if you could manually specify that. |
I don't have a solid mental model for what's happening when I'm trying to set a class -> serializable class mapping.
I've got two classes:
I've got a
SerializableRelationship
class that's working. I'd like to reuse that with these subclasses.How do I:
I know that, once I get it, it'll probably be an "ohhhh" moment. I conceptually understand it's a big hash that makes certain keys to certain other classes that serialize those things. But I need to see an example, and get details, because I don't understand:
I've been guessing things and not getting feedback I know how to understand from the errors.
I think this would make some great addition to the docs! I'll check back in when I figure it out, and maybe PR in some updates if you're ok with that.
The text was updated successfully, but these errors were encountered: