class Decidim::Exporters::Word

Exports any serialized object (Hash) into a readable Excel file. It transforms the columns using slashes in a way that can be afterwards reconstructed into the original nested hash.

For example, ‘{ name: { ca: “Hola”, en: “Hello” } }` would result into the columns: `name/ca` and `name/es`.

It will maintain types like Integers, Floats & Dates so Excel can deal with them.

Public Instance Methods

export() click to toggle source

Public: Exports a file in an Excel readable format.

Returns an ExportData instance.

# File lib/decidim/exporters/word.rb, line 20
def export
  # @docx
  init_docx

  @component = collection.first.component
  @participatory_space = collection.first.participatory_space

  print_titles @component.name

  print_metadata

  print_descriptions collection.first.participatory_space

  collection.each do |paragraph|
    next if paragraph.amended.present?

    print_paragraph paragraph
  end

  # @docx.p collection.inspect

  doxc_buffer = @docx.render
  doxc_buffer.rewind
  ExportData.new(doxc_buffer.sysread, "docx")
end

Private Instance Methods

hide_title_for(paragraph, title = "") click to toggle source
# File lib/decidim/exporters/word.rb, line 300
def hide_title_for(paragraph, title = "")
  return false unless paragraph.instance_of?(Decidim::EnhancedTextwork::Paragraph)

  title = paragraph.title.values.first if title.blank?

  paragraph.component.settings.hide_participatory_text_titles_enabled? && title !~ /\D/
end
init_docx() click to toggle source
# File lib/decidim/exporters/word.rb, line 48
def init_docx
  @docx = Caracal::Document.new("export.docx")

  @docx.style id: "metadata", name: "Metadata" do
    color "929292"
  end

  @docx.style id: "gray", name: "Gray" do
    color "929292"
  end

  @docx.style id: "green", name: "Green" do
    color "61D836"
  end

  @docx.style id: "red", name: "Red" do
    color "EE220C"
  end
end
print_amendment(paragraph, amendment) click to toggle source
print_comment(comment) click to toggle source
print_content(content, description: "") click to toggle source
print_descriptions(participatory_space) click to toggle source
print_metadata() click to toggle source
print_paragraph(paragraph) click to toggle source
print_titles(titles, paragraph: nil, heading: "h1", description: "") click to toggle source

def print_titles(titles)

if titles.keys.count > 1
  titles.each do |language, name|
    @docx.h1 "#{language}: #{name}"
  end
else
  @docx.h1 titles.values.first
end

end

show_title_for(paragraph, title = "") click to toggle source
# File lib/decidim/exporters/word.rb, line 294
def show_title_for(paragraph, title = "")
  return true unless paragraph.instance_of?(Decidim::EnhancedTextwork::Paragraph)

  !hide_title_for(paragraph, title)
end