Sha256: 7d52cb50734f5f6fb108b8fb029f468c2302cbedf6a791ab519a6a81054aa7d1

Contents?: true

Size: 1.06 KB

Versions: 54

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Decidim
  module Surveys
    # This command is executed when the user answers a Survey.
    class AnswerSurvey < Rectify::Command
      # Initializes a AnswerSurvey Command.
      #
      # form - The form from which to get the data.
      # survey - The current instance of the survey to be answered.
      def initialize(form, current_user, survey)
        @form = form
        @current_user = current_user
        @survey = survey
      end

      # Answers a survey if it is valid
      #
      # Broadcasts :ok if successful, :invalid otherwise.
      def call
        return broadcast(:invalid) if @form.invalid?

        answer_survey
        broadcast(:ok)
      end

      private

      def answer_survey
        SurveyAnswer.transaction do
          @form.answers.each do |form_answer|
            SurveyAnswer.create!(
              user: @current_user,
              survey: @survey,
              question: form_answer.question,
              body: form_answer.body
            )
          end
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
decidim-surveys-0.6.6 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.6 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.5 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.5 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.4 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.4 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.3 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.3 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.2 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.2 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.1 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.1 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.0 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.0 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.5.1 app/commands/decidim/surveys/answer_survey.rb
decidim-0.5.1 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.5.0 app/commands/decidim/surveys/answer_survey.rb
decidim-0.5.0 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.4.4 app/commands/decidim/surveys/answer_survey.rb
decidim-0.4.4 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb