Class: Decidim::Votings::Census::Admin::CreateDataset
- Inherits:
-
Command
- Object
- Command
- Decidim::Votings::Census::Admin::CreateDataset
- Includes:
- ProcessesFileLocally
- Defined in:
- 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
-
#call ⇒ Object
Executes the command.
-
#initialize(form, current_user) ⇒ CreateDataset
constructor
A new instance of CreateDataset.
Constructor Details
#initialize(form, current_user) ⇒ CreateDataset
Returns a new instance of CreateDataset.
15 16 17 18 19 |
# File '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 |
Instance Method Details
#call ⇒ Object
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 '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 |