rabl icon indicating copy to clipboard operation
rabl copied to clipboard

avoid xml root for children

Open Raven24 opened this issue 13 years ago • 8 comments
trafficstars

Hi!

Maybe I just missed it, but is there an option for generating child objects directly inside the parent (without an intermediate root node)? If not, please add one ;)

What I get currently:

<post>
  ...

  <comments>
    <comment>
    ...
    </comment>
    <comment>
    ...
    </comment>
  </comments>
</post>

What I (very desperately) need:

<post>
  ...

  <comment>
    ...
  </comment>
  <comment>
    ...
  </comment>
</post>

I tried pretty much every combination of node and child and even had a quite pleasant read of your source in search for such an option, but I couldn't find it.

Raven24 avatar May 13 '12 23:05 Raven24

Can you give an example or two of the templates you've tried?

Does this not work?

child :comments do
  attributes :id, :title
end

# or even
child :comments => :comments do
  attributes :id, :title
end

databyte avatar May 16 '12 04:05 databyte

they all produce a root node for the children... I have config.include_xml_root = false in the config

object @something                 |  <something>
                                  |    <text>...</text><handle>...</handle>
attributes :text, :handle         |    <comments>
                                  |      <comment>...</comment>
child :comments do                |      <comment>...</comment>
  extends "comments/show"         |    </comments>
end                               |  </something>
object @something                 |  <something>
                                  |    <text>...</text><handle>...</handle>
attributes :text, :handle         |    <comments>
                                  |      <comment>...</comment>
child :comments => :comments do   |      <comment>...</comment>
  extends "comments/show"         |    </comments>
end                               |  </something>
object @something                 |  <something>
                                  |    <text>...</text><handle>...</handle>
attributes :text, :handle         |    <comments>
                                  |      <comment>...</comment>
node :comments => do |something|  |      <comment>...</comment>
  something.comments.map { |cmmt| |    </comments>
    { :text => cmmt.text }        |  </something>
  }                               |
end                               |

or produce nothing of use (no comments at all)

object @something                 |  <something>
                                  |    <text>...</text><handle>...</handle>
attributes :text, :handle         |  </something>
                                  |  
node do |something|               |  
  something.comments.map { |cmmt| |  
    { :text => cmmt.text }        |  
  }                               |
end    

My google searches indicated that this might be a limitation of the to_xml method, because it doesn't handle arrays very well and it seems therefore xml serialzation isn't bijective. (also, see this discussion: https://groups.google.com/d/topic/rubyonrails-talk/6El1xNwKhSw/discussion)

I think what I need is the serialization of something like this

:something => {
  :attr1 => "val",
  :attr2 => "val",
  :children => [
    { :attr3=>"val", :attr5=>"val"},
    { :attr4=>"val", :attr6=>"val"},
  ]
}

only with the array not using the hash key as root node but directly inserting them into the parent xml... to become

<something>
  <attr1>val</attr1>
  <attr2>val</attr2>
  <child><attr3>val</attr3><attr5>val</attr5></child>
  <child><attr4>val</attr4><attr6>val</attr6></child>
</something>

Raven24 avatar May 16 '12 12:05 Raven24

Yeah, what you want is child_root's to be excluded. The use of include_xml_root is for the top level root (afaik).

This is currently being discussed here in Issue #178

We need to get our asses back into gear on that redesign... @nesquena - how busy is your weekend? :grin: :sleepy:

databyte avatar May 17 '12 05:05 databyte

@databyte Thanks for the reminder, should probably merge #178 this weekend...

nesquena avatar May 17 '12 06:05 nesquena

Yeah, I think caching had to be decided on (probably as an option on the collection/object method) but it was close. You knocked most of it out quickly.

databyte avatar May 17 '12 06:05 databyte

Yeah I forget where I left it but I do remember it more or less worked. If we can fix the caching issues, then maybe we can get it in a usable state this weekend.

nesquena avatar May 17 '12 06:05 nesquena

That would be great! I'd be happy to test it, when you've got something ;)

Raven24 avatar May 17 '12 10:05 Raven24

I saw @nesquena just merged in the config.include_child_root as an option. Try that out in v0.6.13.

databyte avatar Jun 05 '12 15:06 databyte