Class: ActiveRecord::XmlSerializer
Overview
Defined Under Namespace
Classes: Attribute, MethodAttribute
Instance Attribute Summary
#options
Instance Method Summary
collapse
#add_includes, #initialize, #serializable_attribute_names, #serializable_method_names, #serializable_names, #serializable_record, #to_s
Instance Method Details
#add_associations(association, records, opts) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 198
def add_associations(association, records, opts)
if records.is_a?(Enumerable)
tag = association.to_s
tag = tag.dasherize if dasherize?
if records.empty?
builder.tag!(tag, :type => :array)
else
builder.tag!(tag, :type => :array) do
association_name = association.to_s.singularize
records.each do |record|
record.to_xml opts.merge(
:root => association_name,
:type => (record.class.to_s.underscore == association_name ? nil : record.class.name)
)
end
end
end
else
if record = @record.send(association)
record.to_xml(opts.merge(:root => association))
end
end
end
|
#add_attributes ⇒ Object
176
177
178
179
180
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 176
def add_attributes
(serializable_attributes + serializable_method_attributes).each do |attribute|
add_tag(attribute)
end
end
|
#add_procs ⇒ Object
182
183
184
185
186
187
188
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 182
def add_procs
if procs = options.delete(:procs)
[ *procs ].each do |proc|
proc.call(options)
end
end
end
|
#add_tag(attribute) ⇒ Object
190
191
192
193
194
195
196
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 190
def add_tag(attribute)
builder.tag!(
dasherize? ? attribute.name.dasherize : attribute.name,
attribute.value.to_s,
attribute.decorations(!options[:skip_types])
)
end
|
#builder ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 135
def builder
@builder ||= begin
options[:indent] ||= 2
builder = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
unless options[:skip_instruct]
builder.instruct!
options[:skip_instruct] = true
end
builder
end
end
|
#dasherize? ⇒ Boolean
154
155
156
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 154
def dasherize?
!options.has_key?(:dasherize) || options[:dasherize]
end
|
#root ⇒ Object
149
150
151
152
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 149
def root
root = (options[:root] || @record.class.to_s.underscore).to_s
dasherize? ? root.dasherize : root
end
|
#serializable_attributes ⇒ Object
To replicate the behavior in ActiveRecord#attributes, :except takes precedence over :only. If :only is not set for a N level model but is set for the N+1 level models, then because :except is set to a default value, the second level model can have both :except and :only set. So if :only is set, always delete :except.
165
166
167
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 165
def serializable_attributes
serializable_attribute_names.collect { |name| Attribute.new(name, @record) }
end
|
#serializable_method_attributes ⇒ Object
169
170
171
172
173
174
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 169
def serializable_method_attributes
Array(options[:methods]).inject([]) do |method_attributes, name|
method_attributes << MethodAttribute.new(name.to_s, @record) if @record.respond_to?(name.to_s)
method_attributes
end
end
|
#serialize ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/active_record/serializers/xml_serializer.rb', line 222
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)
add_includes { |association, records, opts| add_associations(association, records, opts) }
options[:procs] = procs
add_procs
yield builder if block_given?
end
end
|