Class: Decidim::Votings::Census::Admin::CreateDataset
- Inherits:
-
Rectify::Command
- Object
- Rectify::Command
- Decidim::Votings::Census::Admin::CreateDataset
- 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 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
-
#call ⇒ Object
Executes the command.
- #create_census_dataset! ⇒ Object
- #csv_header_invalid? ⇒ Boolean
- #csv_row_count ⇒ Object
- #csv_rows ⇒ Object
- #expected_header_size ⇒ Object
- #file_lines_count(file_path) ⇒ Object
-
#initialize(form, current_user) ⇒ CreateDataset
constructor
A new instance of CreateDataset.
Constructor Details
#initialize(form, current_user) ⇒ CreateDataset
Returns a new instance of CreateDataset.
12 13 14 15 16 |
# File '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_user ⇒ Object (readonly)
Returns the value of attribute current_user.
42 43 44 |
# File 'app/commands/decidim/votings/census/admin/create_dataset.rb', line 42 def current_user @current_user end |
#dataset ⇒ Object
Returns the value of attribute dataset.
43 44 45 |
# File 'app/commands/decidim/votings/census/admin/create_dataset.rb', line 43 def dataset @dataset end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
42 43 44 |
# File 'app/commands/decidim/votings/census/admin/create_dataset.rb', line 42 def form @form end |
Instance Method Details
#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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File '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 '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
59 60 61 |
# File '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_count ⇒ Object
71 72 73 |
# File '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_rows ⇒ Object
67 68 69 |
# File '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_size ⇒ Object
63 64 65 |
# File '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 '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 |