jsonapi-serializers icon indicating copy to clipboard operation
jsonapi-serializers copied to clipboard

Additional options to :has_one/:has_many

Open Jesterovskiy opened this issue 8 years ago • 8 comments

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

Jesterovskiy avatar Apr 08 '16 10:04 Jesterovskiy

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 avatar Apr 08 '16 16:04 fotinakis

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

Jesterovskiy avatar Apr 08 '16 16:04 Jesterovskiy

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 avatar Apr 08 '16 16:04 fotinakis

@fotinakis not fully understand, how I can provide context for has_one relation =( Can you make example?

Jesterovskiy avatar Apr 11 '16 11:04 Jesterovskiy

@fotinakis Sorry for disturbing, can you help?

Jesterovskiy avatar Apr 15 '16 15:04 Jesterovskiy

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 avatar Apr 15 '16 17:04 fotinakis

@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 =)

Jesterovskiy avatar Apr 18 '16 12:04 Jesterovskiy

@fotinakis Hello =)

Jesterovskiy avatar Apr 20 '16 14:04 Jesterovskiy