Class: Decidim::Votings::Census::AccessCodesExporter

Inherits:
Object
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
decidim-elections/app/services/decidim/votings/census/access_codes_exporter.rb

Overview

Public: Generates a 7z(seven zip) file with data files ready to be persisted somewhere so users can download their data.

The 7z file wraps a ZIP file which finally contains the data files.

Constant Summary collapse

FILE_NAME_PATTERN =
"%{voting_name}-voting-access-codes.csv"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Constructor Details

#initialize(dataset, path, password) ⇒ AccessCodesExporter

Public: Initializes the class.

dataset - The Voting::Census::Dataset to export the access codes for. path - The String path where to write the zip file. password - The password to protect the zip file.


25
26
27
28
29
# File 'decidim-elections/app/services/decidim/votings/census/access_codes_exporter.rb', line 25

def initialize(dataset, path, password)
  @dataset = dataset
  @path = File.expand_path path
  @password = password
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset


18
19
20
# File 'decidim-elections/app/services/decidim/votings/census/access_codes_exporter.rb', line 18

def dataset
  @dataset
end

#passwordObject (readonly)

Returns the value of attribute password


18
19
20
# File 'decidim-elections/app/services/decidim/votings/census/access_codes_exporter.rb', line 18

def password
  @password
end

#pathObject (readonly)

Returns the value of attribute path


18
19
20
# File 'decidim-elections/app/services/decidim/votings/census/access_codes_exporter.rb', line 18

def path
  @path
end

Instance Method Details

#exportObject


31
32
33
34
35
36
37
38
39
40
# File 'decidim-elections/app/services/decidim/votings/census/access_codes_exporter.rb', line 31

def export
  dirname = File.dirname(@path)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  File.open(@path, "wb") do |file|
    SevenZipRuby::Writer.open(file, password: password) do |szw|
      szw.header_encryption = true
      szw.add_data(csv_data.read, format(FILE_NAME_PATTERN, voting_name: translated_attribute(dataset.voting.title).parameterize))
    end
  end
end