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.10.1 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.10.0 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.9.3 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.9.2 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.9.1 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.9.0 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.8.4 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.8.3 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.8.2 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.8.1 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.8.0 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.7.4 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.7.3 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.7.2 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.7.1 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.7.0 app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.8 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.8 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb
decidim-surveys-0.6.7 app/commands/decidim/surveys/answer_survey.rb
decidim-0.6.7 decidim-surveys/app/commands/decidim/surveys/answer_survey.rb