Sha256: a8d58b657eea1fa43933fb4365600c98851bd78167468008ed8ebd69223d5fef

Contents?: true

Size: 868 Bytes

Versions: 7

Compression:

Stored size: 868 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Admin
      # This command is executed when an admin marks an answer
      # as selected.
      class UpdateAnswerSelection < Decidim::Command
        def initialize(answer, selected)
          @answer = answer
          @selected = selected
        end

        # Selects the answer if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:invalid) if invalid?

          update_answer_selection!

          broadcast(:ok, answer)
        end

        private

        attr_reader :answer, :selected

        def invalid?
          !answer.question.results_total.positive?
        end

        def update_answer_selection!
          answer.update!(
            selected:
          )
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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