Sha256: 003c0a693dfb279f1bf520dc6b4f822e95e5bf157616d1075f7192c6f032f28a

Contents?: true

Size: 1.46 KB

Versions: 12

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Decidim
  module Consultations
    module Admin
      # A command that configures how many and how much questions can be voted.
      class UpdateQuestionConfiguration < Decidim::Command
        # Public: Initializes the command.
        #
        # question - the Question to update
        # form - A form object with the params.
        def initialize(question, form)
          @question = question
          @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?

          # We don't have other question fields in this forms so any additional
          # validation error will be just shown in a flash message
          begin
            update_question!
            broadcast(:ok, question)
          rescue StandardError => e
            broadcast(:invalid, question, e.message)
          end
        end

        private

        attr_reader :form, :question

        def update_question!
          question.assign_attributes(attributes)
          question.save!
        end

        def attributes
          {
            max_votes: form.max_votes,
            min_votes: form.min_votes,
            instructions: form.instructions
          }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-consultations-0.27.9 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.8 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.7 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.6 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.5 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.4 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.3 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.0.rc2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.27.0.rc1 app/commands/decidim/consultations/admin/update_question_configuration.rb