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
adding include_child_root configuration option for child method #178
Conversation
As one of the people who accepts pull requests for surveyor, I know that it's not always easy to find time to respond. I'm hoping you could provide some indication of how likely this one is to be accepted - which will help us make some decisions about the use of rabl within surveyor. |
+1 on this patch. For our iOS APIs, we often have a parent object including a has_many of children, and only want the resulting array to be identified, but the individual children to be root-less. Perhaps an alternative is to allow child to take a root=false type option (or did I miss that)? (Thanks yoon for this, I'm starting to use it) |
+1 This is working well for me. It's certainly better than the other workaround I've been using.
|
Just to be clear, I do plan to include this patch or achieve this functionality in an alternate way. I was thinking perhaps both include this and allow |
That's great news, thanks for a real gem of a gem. Keep up the good work. |
nesquena: Super cool! Both are great and definitely have their place. Perhaps the |
+1 We also need this as a top level config item, and an individual override ":root => false" Reference: |
@CasualNetworks Do you mean something like |
+1 |
+1, currently consuming RABL generated json where child is used is very annoying because of this extra level. |
@col Very thanks for the workaround ! But this pull request is better to manage this. +1 |
Cool thanks for showing your support, hopefully we can merge it this weekend |
adding include_child_root configuration option for child method
Merged this, updated readme and released 0.6.13. Thanks @yoon for putting this together. Sorry about the delay merging it. |
Sorry if I'm misunderstanding, but is node(:foo) { 'bar' }
child(@array_of_posts, :root => false) do
attributes :title
end supposed to work and generate something like this: {"foo": "bar", "posts": [{"title": "lorem..."}, {"title": "lorem..."}]} Also, I don't know if this is unrelated, but I'm having the hardest time getting a child to have a different name. Just as a little background, I'm trying to render something with Rabl::Renderer.json(nil, rabl_path, {
view_path: 'app/views',
@funky_obj
})
# Where @funky_obj is a pagination/thinking-sphinx search wrapper that has an
# instance variable @array that method_missing forwards most methods to. I'm trying, but can't for the life of me, figure out how to render something like the above JSON with rabl. Right now the only way I can seem to do it is with two templates like this: ### index.json.rabl
[
:current_page, :previous_page, :next_page, :per_page, :total_entries,
:total_pages, :params_current_f, :params_current_s, :params_current_sb,
:params_current_o
].each do |n|
node(n) { send(n) }
end
node :collection do
@array.map do |obj|
partial('extra', object: obj, root: false)
end
end
### extra.json.rabl
attributes :title, :author, :etc This doesn't work at all: ### index.json.rabl
[
:current_page, :previous_page, :next_page, :per_page, :total_entries,
:total_pages, :params_current_f, :params_current_s, :params_current_sb,
:params_current_o
].each do |n|
node(n) { send(n) }
end
child(@array => :collection, root: false) do
attributes :title, :author, :etc
end It generates: {"current_page": "...", ..., "collection": [{"collection": {}}, {"collection": {}}]} Instead of {"current_page": "...", ..., "collection": [{"title": "foo", ...}, {"title": "bar", ...}, ...]} And yes, those are empty objects in the first JSON. And strangely there are only two items in the first JSON's (Sorry!!! Posted prematurely and went back and edited it. Also sorry if this is the wrong place to post this kind of question, but this pull request seemed pretty close to the issue, especially with the root issue.) |
Ah, crap! Sorry again. Ok so please ignore the second part of that. The naming issue was because I didn't pass my arguments correctly: it should be |
Hmm, upgrading from <0.6.12 broke our views, because the default was effectively |
This allows one to have the json root object, but no root objects in child nodes.