From 2e9af3638d950ef840e1287f99e323887ec6a4c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 29 Apr 2010 12:27:25 +0200 Subject: Move several configuration values from Hash to ActiveSupport::XmlMini, which both Hash and Array depends on. Also, refactored ActiveModel serializers to just use ActiveSupport::XmlMini.to_tag. As consequence, if a serialized attribute is an array or a hash, it's not encoded as yaml, but as a hash or array. --- .../active_record/serializers/xml_serializer.rb | 85 +++++++++------------- 1 file changed, 35 insertions(+), 50 deletions(-) (limited to 'activerecord/lib/active_record/serializers/xml_serializer.rb') diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 255b03433d..ed5964d923 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -182,16 +182,31 @@ module ActiveRecord #:nodoc: options[:except] |= Array.wrap(@serializable.class.inheritance_column) end + def add_extra_behavior + add_includes + end + + def add_includes + procs = options.delete(:procs) + @serializable.send(:serializable_add_includes, options) do |association, records, opts| + add_associations(association, records, opts) + end + options[:procs] = procs + end + + # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well. def add_associations(association, records, opts) + association_name = association.to_s.singularize + merged_options = options.merge(opts).merge!(:root => association_name) + if records.is_a?(Enumerable) - tag = reformat_name(association.to_s) - type = options[:skip_types] ? {} : {:type => "array"} + tag = ActiveSupport::XmlMini.rename_key(association.to_s, options) + type = options[:skip_types] ? { } : {:type => "array"} if records.empty? - builder.tag!(tag, type) + @builder.tag!(tag, type) else - builder.tag!(tag, type) do - association_name = association.to_s.singularize + @builder.tag!(tag, type) do records.each do |record| if options[:skip_types] record_type = {} @@ -200,60 +215,30 @@ module ActiveRecord #:nodoc: record_type = {:type => record_class} end - record.to_xml opts.merge(:root => association_name).merge(record_type) + record.to_xml merged_options.merge(record_type) end end end - else - if record = @serializable.send(association) - record.to_xml(opts.merge(:root => association)) - end - end - end - - def serialize - args = [root] - if options[:namespace] - args << {:xmlns=>options[:namespace]} - end - - if options[:type] - args << {:type=>options[:type]} - end - - builder.tag!(*args) do - add_attributes - procs = options.delete(:procs) - @serializable.send(:serializable_add_includes, options) { |association, records, opts| - add_associations(association, records, opts) - } - options[:procs] = procs - add_procs - yield builder if block_given? + elsif record = @serializable.send(association) + record.to_xml(merged_options) end end class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc: - protected - def compute_type - type = @serializable.class.serialized_attributes.has_key?(name) ? :yaml : @serializable.class.columns_hash[name].type - - case type - when :text - :string - when :time - :datetime - else - type - end - end - end + def compute_type + type = @serializable.class.serialized_attributes.has_key?(name) ? + super : @serializable.class.columns_hash[name].type - class MethodAttribute < Attribute #:nodoc: - protected - def compute_type - Hash::XML_TYPE_NAMES[@serializable.send(name).class.name] || :string + case type + when :text + :string + when :time + :datetime + else + type end + end + protected :compute_type end end end -- cgit v1.2.3 From 60504e62c8e2f5e137a0ac82aed67a6c0fe42447 Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Fri, 30 Apr 2010 10:39:52 +1000 Subject: to_xml with :include should skip_instruct on the included records [#4506 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/serializers/xml_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/serializers/xml_serializer.rb') diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index ed5964d923..b2d4a48945 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -197,7 +197,7 @@ module ActiveRecord #:nodoc: # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well. def add_associations(association, records, opts) association_name = association.to_s.singularize - merged_options = options.merge(opts).merge!(:root => association_name) + merged_options = options.merge(opts).merge!(:root => association_name, :skip_instruct => true) if records.is_a?(Enumerable) tag = ActiveSupport::XmlMini.rename_key(association.to_s, options) -- cgit v1.2.3