https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2373-record-sensitive-procs-for-to_xml

Did anyone have a chance to look at this patch?  It's an noninvasive patch
that gives procs passed into to the to_xml method via the :procs option
access to the object being serialized.  The simple cases that I've used to
explain it's usefulness may not have been compelling enough, but visualize
this:

You need to return an array of Widget models via xml.  The xml generated by
array_of_widgets.to_xml has all the attributes you would normally need, but
you also want to add the URL to view each widget individually in the event
that consumer of this feed want to access that record again without having
to query the whole string.  Without this patch, you would have to either:
build an xml template (a chore just to get one extra attr) or break MVC by
hacking a URL generation method into the Widget model and referencing it via
to_xml's :method option.  (There are also other, even less ideal options.)
With this patch however, you can do this:

array_of_widgets.to_xml :procs => [ Proc.new { |options, record|
options[:builder].tag!('show-url', widget_url(record)]


The utility of this brief expression seems compelling to me.  Am I
overlooking some way that this is bad or redundant?

Thanks for your time :)

john


On Mon, Jun 1, 2009 at 10:23 AM, John Maxwell <[email protected]> wrote:

>
> https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2373-record-sensitive-procs-for-to_xml
>
> This is a patch that I contributed a couple of months ago. In short,
> the point is to enable procs to access the record being serialized by
> adding support for another argument in proc's goalposts. With this
> patch you can no do something like:
>
> proc = Proc.new { |options, record| options[:builder].tag!('name-
> reverse', record.name.reverse) }
> @records.to_xml :procs => [ proc ]
>
> Which would, of course, add a reversed string of the name to each
> serialized record.
>
> to_xml is incredibly convenient when used to serialize AR objects,
> because it provides a short-hand for expressing those objects without
> having to resort to builder templates, hand-drawn builder instances,
> and such.  I think that this patch will make that expression much more
> powerful without requiring invasive changes.
>
> Further:
> http://stackoverflow.com/questions/260668/building-dynamic-fields-using-activerecordserialization-toxml

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to