This project is archived and is in readonly mode.

#2241 ✓stale
David Burger

Empty Array to_xml Should Still Yield Builder

Reported by David Burger | March 15th, 2009 @ 07:52 PM | in 3.x

This patch makes it so that to_xml called on an empty array still yields the builder.


xml = [].to_xml do |builder|
  builder.count 2
end
xml.include?(%(<count>2</count>))
# => true

The current code will not yield the builder if the array is empty.

Comments and changes to this ticket

  • abriening

    abriening August 2nd, 2009 @ 05:27 PM

    • Tag changed from active_support core_ext, patch, tested to active_support core_ext, array, patch, tested, to_xml

    Here is the same thing but as an optional parameter. The default is still to ignore the block if empty.

  • blatyo

    blatyo August 3rd, 2009 @ 02:18 PM

    Can you explain your use case for adding this feature? This doesn't seem very intuitive and my gut says there is probably a better way.

  • abriening

    abriening August 5th, 2009 @ 04:22 AM

    I can't speak for David but I am implementing a REST XML API, and would like to add some meta-data to each returned array:

    <?xml version="1.0" encoding="UTF-8"?>
    <students type="array">
      <requested_at>2009-08-03T05:06:49Z</requested_at>
      <total_entries>0</total_entries>
      <total_pages>0</total_pages>
      <current_page>1</current_page>
      <per_page>20</per_page>
      <link href="http://example.com/students?page=1&amp;status=pending" rel="current_page"/>
      <link href="http://example.com/students?page=2&amp;status=pending" rel="next_page"/>
    </students>
    

    Currently there is no capacity to output this data if the Array is empty.

    I thought about having a separate entity to contain this meta-data:

    <?xml version="1.0" encoding="UTF-8"?>
    <collection rel="students">
      ...
      <per_page>20</per_page>
      <link href="http://example.com/students?page=1&amp;status=pending" rel="current_page"/>
      <link href="http://example.com/students?page=2&amp;status=pending" rel="next_page"/>
    </collection >
    <students type="array">
      ...
    </students>
    

    But this is less direct than the first solution. I also considered adding this meta-data as attributes on the root element:

    <?xml version="1.0" encoding="UTF-8"?>
    <students type="array" rel="students" current_page="1" total_entries="0" ... >
      <link href="http://example.com/students?page=1&amp;status=pending" rel="current_page"/>
      <link href="http://example.com/students?page=2&amp;status=pending" rel="next_page"/>
    </students>
    

    But in the last case I still wanted the ability to add an arbitrary number of "link" tags as we build more search options. And I haven't patched Array#to_xml to accept a :root_attributes option yet. These link tags should exist even without any nested student entities, as they should be used to drive the API.

  • Michael Koziarski

    Michael Koziarski August 5th, 2009 @ 07:04 AM

    You actually can just pass the builder in yourself:

      builder = ...
      builder.count 2
      xml = [].to_xml :builder=>builder
    

    Wouldn't that cover your situation?

  • abriening

    abriening August 5th, 2009 @ 01:32 PM

    I am building this result by hand now, with an instance of xml_builder ( copy/pasted the to_xml method body ). But passing in the builder still won't yield it back within the array entity if it is empty. It would just be more convenient to optionally choose to always yield the block, because this method does handle the rest of the minutiae and consistently with the Rails.

    As active_support is a framework for making things easier ( "sugar to make things sweeter" ), :always_yield and :root_attributes options would go a long way in adding flexibility to Array#to_xml without breaking the current API. If it doesn't get into core then I will just add it to my own libs.

  • abriening

    abriening August 5th, 2009 @ 04:06 PM

    Here is a new patch that adds both the "always_yield" feature and an "attributes" option for root element that I mentioned above.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:53 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:53 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages