Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

xml support; tests still needed

  • Loading branch information...
commit 299b8b75c1530ad4636ecc34e7c0aa3778757cc2 1 parent afe54e8
Dan Gebhardt authored
20 lib/action_controller/serialization.rb
View
@@ -59,6 +59,26 @@ def _render_option_json(json, options)
super
end
+ def _render_option_xml(xml, options)
+ if xml.respond_to?(:to_ary)
+ options[:root] ||= controller_name
+ end
+
+ serializer = options.delete(:serializer) ||
+ (xml.respond_to?(:active_model_serializer) && xml.active_model_serializer)
+
+ if serializer
+ options[:scope] = serialization_scope
+
+ if default_options = default_serializer_options
+ options = options.merge(default_options)
+ end
+
+ xml = serializer.new(xml, options)
+ end
+ super
+ end
+
module ClassMethods
def serialization_scope(scope)
self._serialization_scope = scope
33 lib/active_model/serializer.rb
View
@@ -60,6 +60,23 @@ def as_json(*args)
array
end
end
+
+ def to_xml(*args)
+ @options[:hash] = hash = {}
+ @options[:unique_values] = {}
+
+ array = serializable_array.map(&:serializable_hash)
+
+ if root = @options[:root]
+ if hash.empty?
+ array.to_xml(:root => root)
+ else
+ hash.merge!(root => array).to_xml(:root => 'data')
+ end
+ else
+ array.to_xml()
+ end
+ end
end
# Active Model Serializer
@@ -415,6 +432,22 @@ def as_json(options=nil)
end
end
+ def to_xml(options=nil)
+ options ||= {}
+ primary_root = options.fetch(:root, @options.fetch(:root, _root))
+
+ @options[:hash] = hash = {}
+ @options[:unique_values] = {}
+
+ primary_hash = serializable_hash
+ if hash.empty?
+ primary_hash.to_xml(:root => primary_root)
+ else
+ hash.merge!(primary_root => primary_hash)
+ hash.to_xml(:root => :data)
+ end
+ end
+
# Returns a hash representation of the serializable
# object without the root.
def serializable_hash
Please sign in to comment.
Something went wrong with that request. Please try again.