Sha256: f9f59d79b9ab7ba06fd1b28aac2abe95d3406e967d7b6285f2b5e538f31a944a

Contents?: true

Size: 1.92 KB

Versions: 52

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

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

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

          Survey.transaction do
            update_survey_questions if @survey.questions_editable?
            update_survey
          end

          broadcast(:ok)
        end

        private

        def update_survey_questions
          @form.questions.each do |form_question|
            question_attributes = {
              body: form_question.body,
              position: form_question.position,
              mandatory: form_question.mandatory,
              question_type: form_question.question_type,
              answer_options: form_question.answer_options.map { |answer| { "body" => answer.body } }
            }

            if form_question.id.present?
              question = @survey.questions.where(id: form_question.id).first
              if form_question.deleted?
                question.destroy!
              else
                question.update_attributes!(question_attributes)
              end
            else
              @survey.questions.create!(question_attributes)
            end
          end
        end

        def update_survey
          @survey.update_attributes!(title: @form.title,
                                     description: @form.description,
                                     tos: @form.tos)
        end
      end
    end
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
decidim-surveys-0.4.1 app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.4.3 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.4.2 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.4.1 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb
decidim-surveys-0.4.0 app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.4.0 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb
decidim-surveys-0.3.2 app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.3.2 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb
decidim-surveys-0.3.1 app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.3.1 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb
decidim-surveys-0.3.0 app/commands/decidim/surveys/admin/update_survey.rb
decidim-0.3.0 decidim-surveys/app/commands/decidim/surveys/admin/update_survey.rb