Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections are not wrapped with with the right root tag in XML #59

Open
dtshepherd opened this issue Nov 23, 2013 · 4 comments
Open

Collections are not wrapped with with the right root tag in XML #59

dtshepherd opened this issue Nov 23, 2013 · 4 comments

Comments

@dtshepherd
Copy link

When serializing using XML, the root tag is incorrect. It is either or depending on the version of rails. I believe this ultimately tracks back to here: https://github.com/apotonick/representable/blob/master/lib/representable.rb#L58

The self.class.name does not properly track back to the model when it is a collection. A work-around for now is to place self.representation_wrap = :collection_name in the collection representer. Maybe this can be set by roar-rails, since I don't think representable is the right place to fix it.

@dtshepherd
Copy link
Author

I have a patch that can work around this issue... Haven't tested with parsing though.

module Roar::Rails::ControllerAdditions
  def prepare_model_for(format, model, options)
    representer = representer_for(format, model, options)
    if representer.representable_attrs.wrap == true
      representer.representation_wrap = model.table_name.to_sym if format == :xml and detect_collection(model)
    end
    representer.prepare(model)
  end

  def detect_collection(model)
    return true if model.kind_of?(Array)
    return true if Object.const_defined?("ActiveRecord") and model.kind_of?(ActiveRecord::Relation)
  end
end

Need to restrict to relations only or things don't work right (no table_name)

@dtshepherd
Copy link
Author

The above patch does not fix wrapping when the ":as =>" directive is used on a collection. The collection will use the self.class.name which is the original collection name, not the :as alias. Still looking for a fix for that.

@apotonick
Copy link
Member

A test case/concrete example would be great. Thanks!

@dtshepherd
Copy link
Author

Sure, I'll try to get you something soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants