Class: ActiveRecord::XmlSerializer

Inherits:
ActiveModel::Serializers::Xml::Serializer show all
Defined in:
activerecord/lib/active_record/serializers/xml_serializer.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary

Attributes inherited from ActiveModel::Serializers::Xml::Serializer

#options

Instance Method Summary collapse

Methods inherited from ActiveModel::Serializers::Xml::Serializer

#attributes_hash, #serializable_attributes, #serializable_methods, #serialize

Constructor Details

#initialize(*args) ⇒ XmlSerializer

Returns a new instance of XmlSerializer



180
181
182
183
# File 'activerecord/lib/active_record/serializers/xml_serializer.rb', line 180

def initialize(*args)
  super
  options[:except] |= Array.wrap(@serializable.class.inheritance_column)
end

Instance Method Details

#add_associations(association, records, opts) ⇒ Object

TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'activerecord/lib/active_record/serializers/xml_serializer.rb', line 198

def add_associations(association, records, opts)
  association_name = association.to_s.singularize
  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)
    type = options[:skip_types] ? { } : {:type => "array"}

    if records.empty?
      @builder.tag!(tag, type)
    else
      @builder.tag!(tag, type) do
        records.each do |record|
          if options[:skip_types]
            record_type = {}
          else
            record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
            record_type = {:type => record_class}
          end

          record.to_xml merged_options.merge(record_type)
        end
      end
    end
  elsif record = @serializable.send(association)
    record.to_xml(merged_options)
  end
end

#add_extra_behaviorObject



185
186
187
# File 'activerecord/lib/active_record/serializers/xml_serializer.rb', line 185

def add_extra_behavior
  add_includes
end

#add_includesObject



189
190
191
192
193
194
195
# File 'activerecord/lib/active_record/serializers/xml_serializer.rb', line 189

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