app/commands/decidim/elections/admin/setup_election.rb in decidim-elections-0.24.3 vs app/commands/decidim/elections/admin/setup_election.rb in decidim-elections-0.25.0.rc1

- old
+ new

@@ -55,81 +55,71 @@ election.blocked_at = Time.current election.bb_created! end def election_data + @election_data ||= begin + ret = base_election_data + election.participatory_space.try(:complete_election_data, election, ret) + ret + end + end + + def base_election_data { - iat: Time.current.to_i, - scheme: bulletin_board.scheme, - authority: { - name: bulletin_board.authority_name, - public_key: bulletin_board.public_key - }, - trustees: - trustees.collect do |trustee| - { - name: trustee.name, - public_key: trustee.public_key - } - end, - description: { - name: { - text: [{ - value: election.title[current_organization.default_locale.to_s], - language: current_organization.default_locale.to_s - }] - }, - start_date: election.start_time, - end_date: election.end_time, - candidates: - answers.collect do |answer| - { - object_id: answer.id.to_s, - ballot_name: { - text: [{ - value: answer.title[current_organization.default_locale.to_s], - language: current_organization.default_locale.to_s - }] - } - } - end, - contests: - questions.collect do |question| - { - "@type": "CandidateContest", - object_id: question.id.to_s, - sequence_order: question.weight, - vote_variation: question.vote_variation, - name: question.title[current_organization.default_locale.to_s], - number_elected: question.answers.count, - votes_allowed: 1, - ballot_title: { - text: [{ - value: question.title[current_organization.default_locale.to_s], - language: current_organization.default_locale.to_s - }] - }, - ballot_subtitle: { - text: [{ - value: question.description[current_organization.default_locale.to_s], - language: current_organization.default_locale.to_s - }] - }, - ballot_selections: - question.answers.collect do |answer| - { - object_id: answer.id.to_s, - sequence_order: answer.weight, - candidate_id: answer.id.to_s - } - end - } - end + trustees: trustees_data, + default_locale: current_organization.default_locale, + title: flatten_translations(election.title), + start_date: election.start_time, + end_date: election.end_time, + questions: questions_data, + answers: answers_data, + ballot_styles: {} + } + end + + def trustees_data + trustees.map do |trustee| + { + name: trustee.name, + slug: trustee.bulletin_board_slug, + public_key: JSON.parse(trustee.public_key) } - }.to_h + end end + def questions_data + questions.map do |question| + { + slug: question.slug, + weight: question.weight, + max_selections: question.max_selections, + title: flatten_translations(question.title), + description: flatten_translations(question.description), + answers: question_answers_data(question) + } + end + end + + def question_answers_data(question) + question.answers.map do |answer| + { + slug: answer.slug, + weight: answer.weight + } + end + end + + def answers_data + answers.map do |answer| + { + slug: answer.slug, + title: flatten_translations(answer.title) + } + end + end + def setup_election bb_election = bulletin_board.create_election(election.id, election_data) raise StandardError, "Wrong status for the created election" if bb_election.status != "created" end @@ -151,9 +141,25 @@ resource: election, affected_users: trustee } Decidim::EventsManager.publish(data) + end + + # Since machine_translations return a nested hash but Electionguard and other + # schemes expect the translations to be returned in a "simple" hash, we need to + # flatten the translations. + # { + # "language": "en", + # "value": "Jubilee Alliance" + # } + # You can read more about the Civics Common Standard Data Specification here: + # https://developers.google.com/civics-data/reference/internationalized-text + def flatten_translations(translated_attribute) + translated_attribute.deep_symbolize_keys! + machine_translations = translated_attribute.delete(:machine_translations) || {} + + machine_translations.merge(translated_attribute).reject { |_k, v| v.empty? } end end end end end