module ActiveRecord::Serialization
def serializable_hash(options = nil)
def serializable_hash(options = nil) options = options.try(:clone) || {} options[:except] = Array(options[:except]).map { |n| n.to_s } options[:except] |= Array(self.class.inheritance_column) super(options) end
def to_xml(options = {}, &block)
end
end
xml.tag!(:second_level, 'content')
xml.level_one do
xml.instruct! unless options[:skip_instruct]
xml = options[:builder] ||= ::Builder::XmlMarkup.new(indent: options[:indent])
options[:indent] ||= 2
require 'builder'
def to_xml(options = {})
class IHaveMyOwnXML < ActiveRecord::Base
form of doing this is:
subclasses to have complete control about what's generated. The general
As noted above, you may override +to_xml+ in your ActiveRecord::Base
# ... normal attributes as shown above ...
end
end
xml.last_name "Heinemeier Hansson"
xml.first_name "David"
xml.creator do
firm.to_xml do |xml|
Alternatively, you can yield the builder object as part of the +to_xml+ call:
# ... normal attributes as shown above ...
firm.to_xml procs: [ proc ]
proc = Proc.new { |options| options[:builder].tag!('abc', 'def') }
modified version of the options hash that was given to +to_xml+:
To call any additional Procs use :procs. The Procs are passed a
# ... normal attributes as shown above ...
firm.to_xml methods: [ :calculated_earnings, :real_earnings ]
To include any methods on the model being called use :methods:
...
...
firm.to_xml include: {account: {}, clients: {include: :address}}
To include deeper levels of associations pass a hash like this:
# ... normal attributes as shown above ...
firm.to_xml procs: [ proc ]
proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) }
associated with models.
outside of the scope of the model -- for example, generating and appending URLs
closure created by a Proc, to_xml can be used to add elements that normally fall
incorporate the context of the record being serialized. And by leveraging the
parameter. This allows for ad hoc additions to the resultant document that
Additionally, the record being serialized will be passed to a Proc's second
firm.to_xml include: [ :account, :clients ]
To include first level associations use :include:
topic.to_xml(skip_instruct: true, except: [ :id, :bonus_time, :written_on, :replies_count ])
For instance:
To not have the column type included in the XML output set :skip_types to +true+.
to +true+ will camelize all column names - this also overrides :dasherize.
can disable this setting :dasherize to +false+. Setting :camelize
+attributes+ method. The default is to dasherize all column names, but you
The :only and :except options are the same as for the
:skip_instruct, :skip_types, :dasherize and :camelize .
This behavior can be controlled with :only, :except,
instruction and all the object's attributes. For example:
By default the generated XML document will include the processing
override ActiveRecord::Base#to_xml.
available through +options+. However more complicated cases should
Builds an XML document to represent the model. Some configuration is
def to_xml(options = {}, &block) XmlSerializer.new(self, options).serialize(&block) end