Class: Decidim::Elections::Admin::SetupForm

Inherits:
Form show all
Defined in:
decidim-elections/app/forms/decidim/elections/admin/setup_form.rb

Overview

This class holds a form to setup elections from Decidim's admin panel.

Constant Summary

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Attribute Summary

Attributes inherited from AttributeObject::Form

#context

Instance Method Summary collapse

Methods inherited from AttributeObject::Form

ensure_hash, from_model, from_params, hash_from, infer_model_name, #map_model, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Method Details

#bulletin_boardObject


79
80
81
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 79

def bulletin_board
  @bulletin_board ||= context[:bulletin_board] || Decidim::Elections.bulletin_board
end

#censusObject


97
98
99
100
101
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 97

def census
  return unless needs_census?

  @census ||= election.component.participatory_space.dataset
end

#census_codes_generated?Boolean

Returns:


91
92
93
94
95
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 91

def census_codes_generated?
  return unless needs_census?

  census&.codes_generated? || census&.exporting_codes? || census&.freeze?
end

#census_messagesObject


65
66
67
68
69
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 65

def census_messages
  @census_messages ||= census_validations.to_h do |message, t_args, _valid|
    [message, I18n.t("steps.create_election.requirements.#{message}", **t_args, scope: "decidim.elections.admin")]
  end
end

#census_validationsObject


49
50
51
52
53
54
55
56
57
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 49

def census_validations
  return [] unless needs_census?

  @census_validations ||= [
    [:census_uploaded, {}, census.present? && census.data.exists?],
    [:census_codes_generated, {}, census_codes_generated?],
    [:census_frozen, {}, census&.freeze?]
  ].freeze
end

#current_stepObject


24
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 24

def current_step; end

#electionObject


75
76
77
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 75

def election
  @election ||= context[:election]
end

#main_button?Boolean

Returns:


103
104
105
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 103

def main_button?
  true
end

#messagesObject


59
60
61
62
63
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 59

def messages
  @messages ||= validations.to_h do |message, t_args, _valid|
    [message, I18n.t("steps.create_election.requirements.#{message}", **t_args, scope: "decidim.elections.admin")]
  end
end

#needs_census?Boolean

Returns:


83
84
85
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 83

def needs_census?
  vote_flow.is_a?(Decidim::Votings::CensusVoteFlow)
end

#participatory_space_trusteesObject


71
72
73
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 71

def participatory_space_trustees
  @participatory_space_trustees ||= Decidim::Elections::Trustees::ByParticipatorySpace.new(election.component.participatory_space).to_a
end

#pending_actionObject


26
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 26

def pending_action; end

#trustee_idsObject


28
29
30
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 28

def trustee_ids
  choose_random_trustees
end

#trusteesObject


32
33
34
35
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 32

def trustees
  ids = trustee_ids
  @trustees ||= Decidim::Elections::Trustees::ByParticipatorySpaceTrusteeIds.new(ids).to_a.sort_by(&:id)
end

#validationsObject


37
38
39
40
41
42
43
44
45
46
47
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 37

def validations
  @validations ||= [
    [:minimum_questions, {}, election.questions.any?],
    [:minimum_answers, {}, election.minimum_answers?],
    [:max_selections, {}, election.valid_questions?],
    [:published, {}, election.published_at.present?],
    [:component_published, {}, election.component.published?],
    [:time_before, { hours: Decidim::Elections.setup_minimum_hours_before_start }, election.minimum_hours_before_start?],
    [:trustees_number, { number: bulletin_board.number_of_trustees }, participatory_space_trustees_with_public_key.size >= bulletin_board.number_of_trustees]
  ].freeze
end

#vote_flowObject


87
88
89
# File 'decidim-elections/app/forms/decidim/elections/admin/setup_form.rb', line 87

def vote_flow
  @vote_flow ||= election.participatory_space.try(:vote_flow_for, election)
end