Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

record sensitive procs for to_xml

#2373

This adds support for an additional argument to procs passed into to_xml via the :procs argument such that, if supplied, the additional argument will be filled with the record being serialized.

An example that this solves: adding data conventionally within the scope of the controller (urls, for example) to an xml-formatted series of ActiveRecord models. Possible alternatives to using record-sensitive procs include creating url-generating methods in the model and referencing them in to_xml's :methods -- a violation of MVC -- and writing a builder template -- a chore, considering to_xml gets you 99% there.

A longer description of what this solves appears here: http://stackoverflow.com/questio...

Reported by greenagain · March 30th, 2009 @ 01:41 AM

State: resolved
Milestone: 3.0.2
Assigned to: josh josh
Importance: none

Activity

  1. ppowpow
    ppowpow

    This would be awesome, and could actually make procs useful for serialization of arrays of AR objects. Why, O why is this patch sitting in here and not getting any love?

    May 27th, 2009 @ 10:51 PM

  2. greenagain
    greenagain

    Appreciate the support, ppowpow. I think that this would be extraordinarily useful too, especially for so tiny a change. I tried to tag and categorize the ticket properly. Does it seem like I filed it wrong or something?

    May 28th, 2009 @ 08:35 PM

  3. ppowpow
    ppowpow

    Looks like you did it by the book; did you try asking for feedback from the community at large? I just stumbled across the page at stackoverflow and found my way here.

    From the contrib guide:

    Once you have a patch and a lighthouse ticket you need to find some people to give you feedback. You can either email the core list or ask in #rails-contrib on irc.freenode.net

    May 28th, 2009 @ 10:39 PM

  4. greenagain
    greenagain

    Thanks for pointing that part of the contribution guide out. I posted to the Rails core group: http://groups.google.com/group/rubyonrails-core/t/d5276f6ecc6359cf?...

    Hopefully it will catch someone's eye now :)

    June 2nd, 2009 @ 02:38 PM

  5. Michael Koziarski
    Michael Koziarski

    Remove the ternary operator, it just obfuscates things:

    if proc.arity == 1
    proc.call ... else
    proc.call ... end

    Also update the docs so that it explains why you might want to do this, 9 times out of 10 you're better off overriding .to_xml in your object, but this 'include a url' case is a good reason not to.

    July 12th, 2009 @ 04:24 AM

  6. greenagain
  7. greenagain
    greenagain

    Thanks for the feedback, Koz :) I've implemented your suggestions and revised the patch, which is now attached. The implementation of AR's XML Serialization has moved into ActiveModel, so I moved my patch there. I also added unit tests for the :procs option in ActiveModel.

    July 15th, 2009 @ 04:50 AM

  8. Michael Koziarski
    Michael Koziarski
    • Milestone cleared.
    • Assigned user set to josh

    Josh is the active model guy

    July 15th, 2009 @ 05:10 AM

  9. Repository
    Repository
    • State changed from new to resolved

    (from [c39151a84768397d3bb025c6e8f877eac59ebbf9]) Patch to ActiveModel's (and ActiveRecord, by association) XML serialization: If two parameters are present in Procs supplied to to_xml's :procs option, the model being serialized will be passed as the second argument [#2373 record sensitive procs for to_xml state:resolved]

    Signed-off-by: Joshua Peek josh@joshpeek.com
    http://github.com/rails/rails/commit/c39151a84768397d3bb025c6e8f877...

    July 23rd, 2009 @ 03:18 AM

  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM