Method: FactoryForm::Form#to_xml

Defined in:
lib/factoryform/form.rb

#to_xmlObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/factoryform/form.rb', line 75

def to_xml
  doc = REXML::Document.new
  root = doc.add_element("Form")

  title_element = root.add_element("Title")
  title_element.add_text("#{self.title}")

  out_string = ''
  @fields.each {|field|
    field_element = root.add_element("Field")
    field_type_element = field_element.add_element("#{field.class.name}")

    field_type_element.add_attribute("id", field.id)
    field_type_element.add_attribute("label", field.label)

    attributes = field.instance_variables.map{|a| a.gsub("@","")}
    attributes.each do |attrb|
      attrb_element = field_type_element.add_element("#{attrb}")

      # For option values of Multiple choice, separate them as option tags
      if attrb == "values"
        field.send(attrb).each do |option|
          option_element = attrb_element.add_element("option")
          option_element.add_text(option)
        end
      else
        attrb_element.add_text(field.send(attrb).to_s)
      end  
    end

    doc.write( out_string = "\n"+'<?xml version="1.0" encoding="UTF-8"?>'+"\n", 2 )

  }
  return out_string
end