object_root for each object in collection #295

Open
GarPit opened this Issue Jul 21, 2012 · 3 comments

Comments

Projects
None yet
3 participants

GarPit commented Jul 21, 2012

Let's assume I have a collection with different object classes (STI). For example, I have a "Whoot" base class and "ReWhoot" as subclass and my API returns both Whoots and Rewhoots in a single collection.

Output for xml version looks like this:

<rewhoots type="array">
  <rewhoot>
  ..
  </rewhoot>
  <rewhoot>
  ...

But actually second object is instance of Whoot and it will be nice to output this node as

<whoot>..</whoot>

options[:root_name] is calculated only once and used for any object

  def compile_hash(options={})
    ....
    # Wrap result in root
    if options[:root_name].present?
      @_root_name = options[:root_name]
    else # no root
      @_root_name = nil
    end

It will be cool if we could call "class.model_name" or just "class.to_s" on each object (i.e. something like this):

def compile_hash(options={})
    ....
    # Wrap result in root
    if options[:root_name].present?
      @_root_name = options[:root_name]
    else # no root
      @_root_name = determine_object_root(@_object, options[:root])
    end
Collaborator

databyte commented Jul 21, 2012

I'm not using XML output in production anywhere so if you can submit a patch with tests, I'll gladly review it.

There are tests for everything in the engine and builder in the root test suite but also both JSON and XML output tests in the rails3_2 fixture under fixtures/rails3_2/test/functional/. Though I added the XML ones, it was just for caching so if you need to expand those - I'd be happy to receive those as well.

Owner

nesquena commented Aug 8, 2012

Yes, I suspect xml needs some love. If anyone can help by adding failing tests that would make it really easy for us to investigate.

Collaborator

databyte commented Nov 20, 2012

Leaving open. XML does need love in v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment