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

Inherits:
Command
  • Object
show all
Includes:
ProcessesFileLocally
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 Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(form, current_user) ⇒ CreateDataset

Returns a new instance of CreateDataset.



15
16
17
18
19
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 15

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

Executes the command. Broadcast this events:

  • :ok when everything is valid

  • :invalid when the form was not valid and could not proceed-

Returns nothing.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 26

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

  process_file_locally(form.file) do |file_path|
    @file_path = file_path
    dataset = create_census_dataset!

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

    if dataset
      CSV.foreach(file_path, col_sep: Decidim.default_csv_col_sep, headers: true, converters: ->(f) { f&.strip }) do |row|
        CreateDatumJob.perform_later(current_user, dataset, row.fields)
      end
    end
  end

  broadcast(:ok)
end