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

adding include_child_root configuration option for child method #178

Merged
merged 1 commit into from Jun 4, 2012
Merged

adding include_child_root configuration option for child method #178

merged 1 commit into from Jun 4, 2012

Conversation

yoon
Copy link
Contributor

@yoon yoon commented Feb 29, 2012

This allows one to have the json root object, but no root objects in child nodes.

@yoon
Copy link
Contributor Author

yoon commented Mar 20, 2012

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.

@chaupt
Copy link

chaupt commented Mar 23, 2012

+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)

@col
Copy link

col commented Mar 25, 2012

+1

This is working well for me. It's certainly better than the other workaround I've been using.

node :comments do |article|
  article.comments.map do |comment|
    partial 'comments/show', object: comment, root: false
  end
end

@nesquena
Copy link
Owner

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 :root => false for child.

@yoon
Copy link
Contributor Author

yoon commented Mar 28, 2012

That's great news, thanks for a real gem of a gem. Keep up the good work.

@chaupt
Copy link

chaupt commented Mar 28, 2012

nesquena: Super cool! Both are great and definitely have their place. Perhaps the :root => false on child could then also override the global settings for when I turn it off generally, but need it in one case? Thank you for most excellent work.

@CasualNetworks
Copy link

+1

We also need this as a top level config item, and an individual override ":root => false"

Reference:
"phone_numbers":[{"phone_number":{"area_code":916}},{"phone_number":{"area_code":530}}]

@nesquena
Copy link
Owner

@CasualNetworks Do you mean something like collection @phone_numbers, :object_root => false

@ivanvanderbyl
Copy link
Contributor

+1
Whats the status of this? I could use this functionality.

@rurounijones
Copy link

+1, currently consuming RABL generated json where child is used is very annoying because of this extra level.

@kaluznyo
Copy link

@col Very thanks for the workaround ! But this pull request is better to manage this. +1

@nesquena
Copy link
Owner

Cool thanks for showing your support, hopefully we can merge it this weekend

nesquena added a commit that referenced this pull request Jun 4, 2012
adding include_child_root configuration option for child method
@nesquena nesquena merged commit e22a1ab into nesquena:master Jun 4, 2012
@nesquena
Copy link
Owner

nesquena commented Jun 4, 2012

Merged this, updated readme and released 0.6.13. Thanks @yoon for putting this together. Sorry about the delay merging it.

@agibralter
Copy link

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 collection while the node/partial method generates the correct JSON with 20 items.

(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.)

@agibralter
Copy link

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 child({@array => :collection}, {root: false}) { ... }.

@afeld
Copy link

afeld commented Jun 26, 2012

Hmm, upgrading from <0.6.12 broke our views, because the default was effectively false before. I understand the motivation behind the change, but you might want to make a note in the changelog about this or something?

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

Successfully merging this pull request may close these issues.

None yet

10 participants