Sha256: b631e9259467b64a38ad9a6151e338df39f5a1fc54f1f7ceb8b4d9c7facc14ad

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Admin
      # This command gets called to publish the election results in the Bulletin Board.
      class PublishResults < Decidim::Command
        # Public: Initializes the command.
        #
        # form - An ActionForm object with the information needed to publish the results
        def initialize(form)
          @form = form
        end

        # Public: Publish the results for the Election.
        #
        # Broadcasts :ok if setup, :invalid otherwise.
        def call
          return broadcast(:invalid) if form.invalid?

          transaction do
            log_action
            publish_results
          end

          broadcast(:ok)
        rescue StandardError => e
          broadcast(:invalid, e.message)
        end

        private

        attr_accessor :form

        delegate :election, :bulletin_board, to: :form

        def log_action
          Decidim.traceability.perform_action!(
            :publish_results,
            election,
            form.current_user,
            visibility: "all"
          )
        end

        def publish_results
          bulletin_board.publish_results(election.id) do |message_id|
            create_election_action(message_id)
          end
        end

        def create_election_action(message_id)
          Decidim::Elections::Action.create!(
            election:,
            action: :publish_results,
            message_id:,
            status: :pending
          )
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-elections-0.28.5 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.4 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.3 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.2 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.1 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.0 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.0.rc5 app/commands/decidim/elections/admin/publish_results.rb
decidim-elections-0.28.0.rc4 app/commands/decidim/elections/admin/publish_results.rb