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

Additional options to :has_one/:has_many #65

Open
Jesterovskiy opened this issue Apr 8, 2016 · 8 comments
Open

Additional options to :has_one/:has_many #65

Jesterovskiy opened this issue Apr 8, 2016 · 8 comments

Comments

@Jesterovskiy
Copy link

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
@fotinakis
Copy link
Owner

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?

@Jesterovskiy
Copy link
Author

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

@fotinakis
Copy link
Owner

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.

@Jesterovskiy
Copy link
Author

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

@Jesterovskiy
Copy link
Author

@fotinakis Sorry for disturbing, can you help?

@fotinakis
Copy link
Owner

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: f83d34f -- but docs will explain it better.

@Jesterovskiy
Copy link
Author

Jesterovskiy commented Apr 18, 2016

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

@fotinakis Hello =)

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

2 participants