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.23.6 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.24.2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.5 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.24.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.24.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.24.0.rc2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.4 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.24.0.rc1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.3 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.2 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.1.rc1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.23.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.22.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.21.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.20.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.20.0 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.19.1 app/commands/decidim/consultations/admin/update_question_configuration.rb
decidim-consultations-0.19.0 app/commands/decidim/consultations/admin/update_question_configuration.rb