Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10162 (closed defect: fixed)

Opened 6 months ago

Last modified 6 months ago

[PATCH] AR#to_xml shouldn't pass procs to associations

Reported by: Catfish Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: serialization XML
Cc:

Description

ActiveRecord#to_xml currently passes procs to any associated objects. IMO, this is unlikely to be the desired or expected behaviour - you typically add a proc because you're adding custom tags to a specific object, rather than for adding tags for every child object.

This patch prevents the procs being passed to the child. Note that a child object could still have its own procs if it overrided to_xml.

Attachments

noChildProcs.diff (1.7 kB) - added by Catfish on 11/14/07 13:11:46.
to_xml_more_specific_procs.diff (7.7 kB) - added by chuyeow on 11/22/07 02:47:23.
Top-level :procs aren't applied to associations, but :procs on :include work

Change History

11/14/07 13:11:46 changed by Catfish

  • attachment noChildProcs.diff added.

11/14/07 18:46:41 changed by bitsweat

I'm not sure whether this is true. What do others using this API think?

11/14/07 23:01:54 changed by Catfish

Every time I've used procs, it's been with something like this :

class Author < ActiveRecord::Base
  has_many :posts

  def to_xml
     proc = Proc.new do |opts|
        opts[:builder].address(AddressFinder.lookup(self))
     end
     super.to_xml(:procs => [proc])
   end 
end

Author.find(:all).to_xml(:include => :posts)

...with the intent of adding an address tag to the author xml, but which would actually give every AR object in the xml - both authors and their posts - an address tag with the corresponding author's address.

(excuse any errors in the above, I've just typed it off the top of my head)

For me at least, the common use case is to define a local proc within an AR subclass's to_xml method, which adds additional tags relevant to that object. I struggle to think of many cases where you want the procs to be propogated down the association chain.

11/22/07 02:47:23 changed by chuyeow

  • attachment to_xml_more_specific_procs.diff added.

Top-level :procs aren't applied to associations, but :procs on :include work

11/22/07 02:50:05 changed by chuyeow

  • keywords set to serialization XML.

I haven't used :procs before but Catfish makes a good case. I do use the :methods option however and I think we can see some similarity with the rationale for r7156. With :procs you can't easily stop the XmlSerializer from passing the :procs option down to associations so I reckon this is a good reason to only apply it to the object it's called on.

I've attached a patch that prevents the top-level :procs options from being passed on to associations, but still allows you to specify :procs on any :include-d associations. E.g.

    posts_proc = Proc.new { |options| options[:builder].tag!('copyright', 'DHH') }
    xml = authors(:david).to_xml(
      :indent => 2,
      :include => {
        :posts => { :procs => [ posts_proc ] }
      }
    )

I think this is a happy medium :)

(PS. Cleaned up some trailing whitespace while making the changes.)

11/23/07 14:05:42 changed by Catfish

Looks good to me, though perhaps it would be cleaner if the proc-removing bit is moved to Serializer::add_includes. It means introducing :procs into the base Serializer class (which currently knows nothing about that option), but it does mean that you don't have to juggle :procs in and out of options, and it'll help for when to_json finally gets a :procs option too :)

11/23/07 17:12:49 changed by chuyeow

I've thought of that (the main reason I'm interested in this patch is because it is related to JSON serialization, one of my favorite areas in Rails ;)) but like you say, "It means introducing :procs into the base Serializer class (which currently knows nothing about that option)". Probably easier (for me) for now to defer that until :procs is introduced into the JsonSerializer :)

However, if you do think of an elegant solution to some form of Serializer-agnostic options exclusion, please do patch away!

12/03/07 01:47:25 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [8258]) Fixed that to_xml should not automatically pass :procs to associations included with :include (closes #10162) [chuyeow]