aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/serializers
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-04-29 12:27:25 +0200
committerJosé Valim <jose.valim@gmail.com>2010-04-29 12:42:42 +0200
commit2e9af3638d950ef840e1287f99e323887ec6a4c4 (patch)
treeeb08bcf15f3096f15ffc705190df649cc208b071 /activerecord/lib/active_record/serializers
parent1bea5c755c25cd90171477fe725e64b26395b810 (diff)
downloadrails-2e9af3638d950ef840e1287f99e323887ec6a4c4.tar.gz
rails-2e9af3638d950ef840e1287f99e323887ec6a4c4.tar.bz2
rails-2e9af3638d950ef840e1287f99e323887ec6a4c4.zip
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.
Diffstat (limited to 'activerecord/lib/active_record/serializers')
-rw-r--r--activerecord/lib/active_record/serializers/xml_serializer.rb85
1 files changed, 35 insertions, 50 deletions
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