Class: Decidim::Votings::Census::Admin::CreateDataset
- Includes:
- HasBlobFile
- 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
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#dataset ⇒ Object
Returns the value of attribute dataset.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
Instance Method Summary collapse
- #ballot_style_headers ⇒ Object
-
#call ⇒ Object
Executes the command.
- #create_census_dataset! ⇒ Object
- #csv_header_invalid? ⇒ Boolean
- #csv_row_count ⇒ Object
- #csv_rows ⇒ Object
- #expected_headers ⇒ Object
- #file_lines_count(file_path) ⇒ Object
- #headers ⇒ Object
-
#initialize(form, current_user) ⇒ CreateDataset
constructor
A new instance of CreateDataset.
Methods inherited from Command
call, #evaluate, #method_missing, #respond_to_missing?, #transaction
Constructor Details
#initialize(form, current_user) ⇒ CreateDataset
Returns a new instance of CreateDataset.
14 15 16 17 18 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 14 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 Attribute Details
#current_user ⇒ Object (readonly)
Returns the value of attribute current_user.
44 45 46 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 44 def current_user @current_user end |
#dataset ⇒ Object
Returns the value of attribute dataset.
45 46 47 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 45 def dataset @dataset end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
44 45 46 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 44 def form @form end |
Instance Method Details
#ballot_style_headers ⇒ Object
69 70 71 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 69 def ballot_style_headers headers.push(:ballot_style_code) end |
#call ⇒ Object
Executes the command. Broadcast this events:
-
:ok when everything is valid
-
:invalid when the form wasn't valid and couldn't proceed-
Returns nothing.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 25 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(blob_path, col_sep: ";", headers: true, converters: ->(f) { f&.strip }) do |row| CreateDatumJob.perform_later(current_user, dataset, row.fields) end end broadcast(:ok) end |
#create_census_dataset! ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 47 def create_census_dataset! Decidim.traceability.create( Decidim::Votings::Census::Dataset, current_user, { voting: form.current_participatory_space, file: blob, csv_row_raw_count: csv_row_count, status: :creating_data }, visibility: "admin-only" ) end |
#csv_header_invalid? ⇒ Boolean
61 62 63 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 61 def csv_header_invalid? CSV.parse_line(File.open(blob_path), col_sep: ";", headers: true, header_converters: :symbol).headers != expected_headers end |
#csv_row_count ⇒ Object
81 82 83 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 81 def csv_row_count @csv_row_count ||= file_lines_count(blob_path) - 1 end |
#csv_rows ⇒ Object
77 78 79 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 77 def csv_rows @csv_rows ||= CSV.read(blob_path) end |
#expected_headers ⇒ Object
73 74 75 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 73 def expected_headers @expected_headers ||= form.current_participatory_space.has_ballot_styles? ? ballot_style_headers : headers end |
#file_lines_count(file_path) ⇒ Object
85 86 87 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 85 def file_lines_count(file_path) `wc -l "#{file_path.shellescape}"`.strip.split.first.to_i end |
#headers ⇒ Object
65 66 67 |
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_dataset.rb', line 65 def headers [:document_id, :document_type, :date_of_birth, :full_name, :full_address, :postal_code, :mobile_phone_number, :email_address] end |