Class: Exportable::ExportMethods::XmlExporter::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/exportable/export_methods/xml_exporter.rb

Overview

Exporter class for XML

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Exporter

Returns a new instance of Exporter.



17
18
19
# File 'lib/exportable/export_methods/xml_exporter.rb', line 17

def initialize(model)
  @model = model
end

Instance Method Details

#export(options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/exportable/export_methods/xml_exporter.rb', line 21

def export(options)
  Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.root do
      xml.send(@model.model_name.collection) do
        @model.all.find_each do |record|
          xml.send(@model.model_name.element) do
            options[:fields].each do |attr| 
              xml.send(attr, record.send(attr).to_s)
            end  
          end
        end
      end
    end
  end.to_xml
end