Class: Decidim::Votings::Census::Admin::CreateDataset

Inherits:
Rectify::Command
  • Object
show all
Defined in:
decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb

Overview

A command with the business logic to create census dataset for a voting space.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, current_user) ⇒ CreateDataset

Returns a new instance of CreateDataset.


12
13
14
15
16
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 12

def initialize(form, current_user)
  @form = form
  @current_user = current_user
  @dataset = nil
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user


42
43
44
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 42

def current_user
  @current_user
end

#datasetObject

Returns the value of attribute dataset


43
44
45
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 43

def dataset
  @dataset
end

#formObject (readonly)

Returns the value of attribute form


42
43
44
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 42

def form
  @form
end

Instance Method Details

#callObject

Executes the command. Broadcast this events:

  • :ok when everything is valid

  • :invalid when the form wasn't valid and couldn't proceed-

Returns nothing.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 23

def call
  return broadcast(:invalid) unless form.valid?

  dataset = create_census_dataset!

  if csv_header_invalid?
    dataset.destroy!
    return broadcast(:invalid_csv_header)
  end

  if dataset
    CSV.foreach(form.file.tempfile.path, col_sep: ";", headers: true) do |row|
      CreateDatumJob.perform_later(current_user, dataset, row.fields)
    end
  end

  broadcast(:ok)
end

#create_census_dataset!Object


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 45

def create_census_dataset!
  Decidim.traceability.create(
    Decidim::Votings::Census::Dataset,
    current_user,
    {
      voting: form.current_participatory_space,
      file: form.file.original_filename,
      csv_row_raw_count: csv_row_count,
      status: :creating_data
    },
    visibility: "admin-only"
  )
end

#csv_header_invalid?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 59

def csv_header_invalid?
  CSV.parse_line(File.open(form.file.tempfile.path, &:readline), col_sep: ";").size != expected_header_size
end

#csv_row_countObject


71
72
73
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 71

def csv_row_count
  @csv_row_count ||= file_lines_count(form.file.tempfile.path) - 1
end

#csv_rowsObject


67
68
69
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 67

def csv_rows
  @csv_rows ||= CSV.read(form.file.tempfile.path)
end

#expected_header_sizeObject


63
64
65
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 63

def expected_header_size
  @expected_header_size ||= form.current_participatory_space.has_ballot_styles? ? 9 : 8
end

#file_lines_count(file_path) ⇒ Object


75
76
77
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 75

def file_lines_count(file_path)
  `wc -l "#{file_path}"`.strip.split(" ")[0].to_i
end