Sha256: 42b56bef737af55d9c20ad0b9893938e6496d1e7f4181f85357f66b2d0faaa94

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Admin
      # This command is executed when the user creates a Question
      # from the admin panel.
      class CreateQuestion < Rectify::Command
        def initialize(form)
          @form = form
        end

        # Creates the question if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:election_started) if form.election.started?
          return broadcast(:invalid) if form.invalid?

          create_question!

          broadcast(:ok, question)
        end

        private

        attr_reader :form, :question

        def create_question!
          attributes = {
            election: form.election,
            title: form.title,
            max_selections: form.max_selections,
            weight: form.weight,
            random_answers_order: form.random_answers_order,
            min_selections: form.min_selections
          }

          @question = Decidim.traceability.create!(
            Question,
            form.current_user,
            attributes,
            visibility: "all"
          )
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-elections-0.26.7 app/commands/decidim/elections/admin/create_question.rb
decidim-elections-0.26.5 app/commands/decidim/elections/admin/create_question.rb
decidim-elections-0.26.4 app/commands/decidim/elections/admin/create_question.rb
decidim-elections-0.26.3 app/commands/decidim/elections/admin/create_question.rb