Sha256: 1044df95465c7de1310674585ceeec952ef0e5abf60f0336d58bbddbbddf732f

Contents?: true

Size: 1.46 KB

Versions: 39

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 < Rectify::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

39 entries across 39 versions & 1 rubygems

Version Path
decidim-consultations-0.26.10 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.9 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.8 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.7 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.5 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.4 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.3 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.0.rc2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.26.0.rc1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.0.rc4 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.0.rc3 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.0.rc2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.25.0.rc1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.24.3 app/commands/decidim/consultations/admin/update_question_configuration.rb