jsonapi-serializers
jsonapi-serializers copied to clipboard
Additional options to :has_one/:has_many
Hi. I want to pass additional options for has_one
relations. In https://github.com/fotinakis/jsonapi-serializers/blob/master/lib/jsonapi-serializers/attributes.rb#L58 when make debug I see parent_name
hash in options
, but in https://github.com/fotinakis/jsonapi-serializers/blob/master/lib/jsonapi-serializers/serializer.rb#L174 attr_data[:options]
is empty. Maybe I something misunderstood =(
My sample code: Controller
serialize_models(categories, include: %w(image posts))
Serialisers
class API::V1::CategorySerializer < API::V1::BaseSerializer
has_one :image, parent_name: 'category'
has_many :posts
end
class API::V1::PostSerializer < API::V1::BaseSerializer
has_one :image, parent_name: 'post'
end
I think you might be misunderstanding, parent_name
is not part of the JSON-API spec so it's not an option we support on has_one or has_many. What is the result you are going for?
@fotinakis I know this) I actually try to understand process and make PR for add this functionality. But fail =(
In my ImageSerializer
I need additional options, for understand, who is call, to show proper Image. I'm thinking about: if we have options
hash already in attributes.rb
, maybe we can show it in Serializer. What do you think?
Can you use context
instead? Like: JSONAPI::Serializer.serialize(post, context: {parent: 'from_post'})
— you can then check context[:parent]
inside the serializer to make changes.
This is an undocumented feature because it is not quite complete yet. We need to get https://github.com/fotinakis/jsonapi-serializers/pull/53/files in first.
Let me know if that will solve your problem.
@fotinakis not fully understand, how I can provide context
for has_one
relation =( Can you make example?
@fotinakis Sorry for disturbing, can you help?
Hey @Jesterovskiy, sorry I don't totally understand what the goal is here. I think context
handling may solve your problem, so I will add documentation for that and you can let me know if it helps — you may be able to understand it from the tests in this commit: https://github.com/fotinakis/jsonapi-serializers/commit/f83d34f15b68a33e54f77a71d62a77cc4b1238a2 -- but docs will explain it better.
@fotinakis try to explain my relationships:
Category has one Image and has many Posts.
Post has one Image.
For each Image I need to know what relation(parent) he have to take right resolution.
For Category Index I need serialize categories with include: %w(image)
For Category Show I need serialize category with include: %w(image posts)
In my opinion, if I can in CategorySerializer add has_one :image, context: { parent_name: 'category' }
or options: { parent_name: 'category' }
and get this parameter in ImageSerializer is good solution. Maybe I something miss, but you propose add context in CategoryController and this context will be in ImageSerializer.
Can you show example, how to transform serialize_model(category, include: %w(image posts))
to add context for image in Category and image in Post?
Thank you very much for your patience =)
@fotinakis Hello =)