def output(data)
if data.is_a? Array
data = data.collect{ |d| hash_for(d) }
else
data = hash_for(data)
end
respond_to do |format|
format.html { render :text => "
#{data.to_yaml}
" }
format.xml { render :xml => data.to_xml }
format.yaml { render :xml => data.to_yaml }
format.json { render :json => data.to_json }
end
end
def hash_for(record, fields = nil)
r = {}
fields ||= record.class::PUBLIC_COLUMNS
fields.each do |f|
v = record.send(f)
if v.is_a?(ActiveRecord::Base)
r[f] = hash_for(v)
elsif v.is_a?(Array)
r[f] = v.collect{|i| hash_for(i)}
else
r[f] = record.send(f)
end
end
r
end