Sha256: c89a09b2e1696123e9d2749632a75c967c61cebde2da7de652e1eb6336006e68

Contents?: true

Size: 1.29 KB

Versions: 19

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Decidim
  module Votings
    # A command with all the business logic when creating a closure for a polling station
    class CreatePollingStationClosure < Rectify::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(form)
        @form = form
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the form wasn't valid and we couldn't proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?

        transaction do
          closure.save!
          create_total_ballot_results!
        end

        broadcast(:ok)
      end

      private

      attr_reader :form

      def closure
        @closure ||= PollingStationClosure.new(
          phase: :results,
          election: form.election,
          polling_station: form.polling_station,
          polling_officer: form.context.polling_officer,
          polling_officer_notes: form.polling_officer_notes
        )
      end

      def create_total_ballot_results!
        closure.results.create!(
          value: form.total_ballots_count,
          result_type: :total_ballots
        )
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-elections-0.26.10 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.9 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.8 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.7 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.5 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.4 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.3 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.2 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.1 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.0 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.0.rc2 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.26.0.rc1 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.2 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.1 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.0 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.0.rc4 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.0.rc3 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.0.rc2 app/commands/decidim/votings/create_polling_station_closure.rb
decidim-elections-0.25.0.rc1 app/commands/decidim/votings/create_polling_station_closure.rb