Sha256: fa0ecae89d924384e766a83a7b99b59d20174ff56f97459d9dc69dc56826db7b

Contents?: true

Size: 1.11 KB

Versions: 19

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Decidim
  module Meetings
    # This command is executed when the user creates an Answer in a meeting poll.
    class CreateAnswer < Decidim::Command
      def initialize(form, current_user, questionnaire)
        @form = form
        @current_user = current_user
        @questionnaire = questionnaire
      end

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

        transaction do
          answer_question
        end

        broadcast(:ok)
      end

      private

      attr_reader :form, :answer

      def invalid?
        form.invalid?
      end

      def answer_question
        answer = Answer.new(
          user: @current_user,
          questionnaire: @questionnaire,
          question: form.question
        )

        form.selected_choices.each do |choice|
          answer.choices.build(
            body: choice.body,
            decidim_answer_option_id: choice.answer_option_id
          )
        end

        answer.save!
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-meetings-0.28.4 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.9 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.28.3 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.8 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.28.2 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.7 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.28.1 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.6 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.28.0 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.5 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.28.0.rc5 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.28.0.rc4 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.4 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.3 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.2 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.1 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.0 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.0.rc2 app/commands/decidim/meetings/create_answer.rb
decidim-meetings-0.27.0.rc1 app/commands/decidim/meetings/create_answer.rb