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

added option to include nested associations #952

Closed
wants to merge 3 commits into from

Conversation

iMacTia
Copy link

@iMacTia iMacTia commented Jun 12, 2015

Devs can now use a new parameter in Serializer classes when defining associations (has_many, has_one, ...): include_nested_assosiations.

If this option is true, serialization will "follow" the association including not only its attributes, but also its associations.

@joaomdmoura
Copy link
Member

Great @iMacTia! I was really looking forward to implement it!
Could you write so tests to cover the usual and some corner cases? 😄

@iMacTia
Copy link
Author

iMacTia commented Jun 15, 2015

Added some basic tests, unfortunately was my first time with Minitest, I found myself a bit disoriented @_@

def test_no_option_is_passed_in
@post_serializer = PostSerializerWithNestedAssociations.new(@post)
json = ActiveModel::Serializer::Adapter::Json.new(@post_serializer).as_json
assert json[:author][:bio] != nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iMacTia could you just assert the content itself? So we also validate that is matches the expected one 😄

expected_return = {
  ...
}
assert_equal json[:author][:bio] , expected_return

item.attributes(opts)
opts[:include_nested_associations] ?
self.class.new(item).serializable_hash :
item.attributes(opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a good time to extract add_association to its own method, like the json_api adapter has

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bf4
Copy link
Member

bf4 commented Jul 9, 2015

@iMacTia need any help bringing this to completion? I'm happy to pair

@iMacTia
Copy link
Author

iMacTia commented Jul 10, 2015

I thought you guys were working on it (extracting add_association or whatever), I'm currently using my branch in my project and it works like a charm. CI is not passing because of tests, but I'm not very good with the tools you've chosen...

@joaomdmoura
Copy link
Member

Yeap @iMacTia, indeed I'm inclined to merge the Associations Refactoring, but I still think that nested associations are a need, we would still be willing to merge it. Maybe on top or the associations refactoring after I merge it.

@iMacTia
Copy link
Author

iMacTia commented Sep 2, 2015

Hi @joaomdmoura, just wanted to know what's the plan for this feature, are you still waiting for something from me? We've been using my branch in production for some months now, seems working with no problems. I see a similar problem has been addressed for side-loading, but looks to me like a different feature after all

@casiodk
Copy link

casiodk commented Sep 3, 2015

Hi what is the status here. I would also like to see the possibilty of deep nesting associations, as I´m converting from jbuilder to AMS right now...

@iMacTia
Copy link
Author

iMacTia commented Sep 3, 2015

Hi @casiodk, Just asked the same yesterday :)
Hopefully we'll get back from them shortly.
In the meantime, trust me if I tell you that you made the best choice in switching to AMS.

@casiodk
Copy link

casiodk commented Sep 3, 2015

Yes it seems like the best choice, but not as flexible

@joaomdmoura
Copy link
Member

Hey @iMacTia and @casiodk, first of all thank you for the support and interest on this. 😄

Some ppl have being addressing this with different approaches, I think this PR is pretty outdated right now, and I liked the discussion on #1113, and reviewed #1114.
So I'm closing this one in favor of #1114, It solves the same problem, but is up-to-date and also do some nice refactor on the adapter.

I totally agree with your comment on #1113

Recursive loop problem should be fixed as well, the problem is that if you want a standard behaviour (like you always want the comment author when you get a comment), you have to copy-and-paste the same "include" option in all serialisers, where with the include_nested_associations you delegate this to the serializer itself and you just need to manage special cases.

This might be a real need for some ppl, @NullVoxPopuli even proposed a solution for that here. Let's go for nested association and see if more ppl back up the nest everything approach, if this get backed then we can work on it, if you really wanted it, you could work on a PR on top of #1114 once it gets merged, it won't be a priority but would check it out 😄

@joaomdmoura joaomdmoura closed this Sep 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants