Sha256: bc8042db2b6b8210b19f8baaa918e062c985c1f0f75d3c503ad802b281f742fd

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Decidim
  module Surveys
    module Admin
      # This controller allows the user to update a Page.
      class SurveysController < Admin::ApplicationController
        helper_method :survey, :blank_question, :blank_answer_option, :question_types

        def edit
          authorize! :edit, Survey
          @form = form(Admin::SurveyForm).from_model(survey)
        end

        def update
          authorize! :update, Survey
          params["published_at"] = Time.current if params.has_key? "save_and_publish"
          @form = form(Admin::SurveyForm).from_params(params)

          Admin::UpdateSurvey.call(@form, survey) do
            on(:ok) do
              flash[:notice] = I18n.t("surveys.update.success", scope: "decidim.surveys.admin")
              redirect_to parent_path
            end

            on(:invalid) do
              flash.now[:alert] = I18n.t("surveys.update.invalid", scope: "decidim.surveys.admin")
              render action: "edit"
            end
          end
        end

        private

        def survey
          @survey ||= Survey.find_by(component: current_component)
        end

        def blank_question
          @blank_question ||= Admin::SurveyQuestionForm.new
        end

        def blank_answer_option
          @blank_answer_option ||= Admin::SurveyAnswerOptionForm.new
        end

        def question_types
          @question_types ||= SurveyQuestion::TYPES.map do |question_type|
            [question_type, I18n.t("decidim.surveys.question_types.#{question_type}")]
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-surveys-0.11.2 app/controllers/decidim/surveys/admin/surveys_controller.rb
decidim-surveys-0.11.1 app/controllers/decidim/surveys/admin/surveys_controller.rb
decidim-surveys-0.11.0.pre1 app/controllers/decidim/surveys/admin/surveys_controller.rb