Class: Decidim::Elections::Admin::Censuses::TokenCsv

Inherits:
Command
  • Object
show all
Defined in:
decidim-elections/app/commands/decidim/elections/admin/censuses/token_csv.rb

Overview

A command with the business logic to create census data for an election.

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(form, election) ⇒ TokenCsv

Returns a new instance of TokenCsv.



10
11
12
13
# File 'decidim-elections/app/commands/decidim/elections/admin/censuses/token_csv.rb', line 10

def initialize(form, election)
  @form = form
  @election = election
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 these events:

  • :ok when everything is valid

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

Returns nothing.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'decidim-elections/app/commands/decidim/elections/admin/censuses/token_csv.rb', line 20

def call
  return broadcast(:invalid) if @form.invalid?
  return broadcast(:invalid) if @form.remove_all && @election.census.blank?

  # If the form is set to remove all, we just delete all voters
  if @form.remove_all
    @election.voters.delete_all
    return broadcast(:ok)
  end
  return broadcast(:invalid) unless @form.file

  rows = @form.data
  return broadcast(:invalid) if rows.blank?

  Voter.bulk_insert(@election, rows.map { |row| { email: row.first.downcase, token: row.second } })
  broadcast(:ok)
end