Class: Decidim::Votings::Census::Admin::CreateDatum

Inherits:
Command
  • Object
show all
Defined in:
decidim-elections/app/commands/decidim/votings/census/admin/create_datum.rb

Overview

A command with the business logic to create the datum for a dataset row.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(form, dataset) ⇒ CreateDatum

Returns a new instance of CreateDatum.



10
11
12
13
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_datum.rb', line 10

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

Dynamic Method Handling

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

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



27
28
29
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_datum.rb', line 27

def dataset
  @dataset
end

#formObject (readonly)

Returns the value of attribute form.



27
28
29
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_datum.rb', line 27

def form
  @form
end

Instance Method Details

#ballot_style_for_code(voting, code) ⇒ Object



48
49
50
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_datum.rb', line 48

def ballot_style_for_code(voting, code)
  Decidim::Votings::Admin::BallotStyleByVotingCode.for(voting, code)
end

#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.



20
21
22
23
24
25
# File 'decidim-elections/app/commands/decidim/votings/census/admin/create_datum.rb', line 20

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

  create_census_datum!
  broadcast(:ok)
end

#create_census_datum!Object



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_datum.rb', line 29

def create_census_datum!
  attributes = {
    hashed_in_person_data: form.hashed_in_person_data,
    hashed_check_data: form.hashed_check_data,

    full_name: form.full_name,
    full_address: form.full_address,
    postal_code: form.postal_code,
    mobile_phone_number: form.mobile_phone_number,
    email: form.email,
    decidim_votings_ballot_style_id: ballot_style_for_code(dataset.voting, form.ballot_style_code)&.id
  }

  Decidim::Votings::Census::Datum.create(
    dataset:,
    attributes:
  )
end