Class: Decidim::Surveys::Admin::SurveysController

Inherits:
ApplicationController
  • Object
show all
Includes:
Forms::Admin::Concerns::HasQuestionnaire, Forms::Admin::Concerns::HasQuestionnaireAnswersUrlHelper, Filterable
Defined in:
app/controllers/decidim/surveys/admin/surveys_controller.rb

Overview

This controller allows the user create and edit questionnaires for Surveys.

Instance Method Summary collapse

Instance Method Details

#after_update_urlObject



102
103
104
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 102

def after_update_url
  surveys_path
end

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 16

def create
  enforce_permission_to(:create, :questionnaire)
  Decidim::Surveys::Admin::CreateSurvey.call(current_component) do
    on(:ok) do |survey|
      flash[:notice] = I18n.t("create.success", scope: "decidim.surveys.admin.surveys")
      redirect_to edit_survey_path(survey)
    end

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

#destroyObject



83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 83

def destroy
  enforce_permission_to(:destroy, :questionnaire, questionnaire:)
  Decidim::Commands::DestroyResource.call(survey, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("destroy.success", scope: "decidim.surveys.admin.surveys")

      redirect_to surveys_path
    end
  end
end

#editObject



31
32
33
34
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 31

def edit
  enforce_permission_to(:update, :questionnaire, questionnaire:)
  @form = form(Admin::SurveyForm).from_model(survey)
end

#edit_questionnaire_titleObject



115
116
117
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 115

def edit_questionnaire_title
  t(:title, scope: "decidim.forms.admin.questionnaires.form", questionnaire_for: translated_attribute(current_component.name))
end

#edit_questions_templateObject



94
95
96
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 94

def edit_questions_template
  "decidim/surveys/admin/surveys/edit_questions"
end

#indexObject



14
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 14

def index; end

#public_urlObject

Specify the public url from which the survey can be viewed and answered



111
112
113
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 111

def public_url
  Decidim::EngineRouter.main_proxy(current_component).survey_path(survey)
end

#publishObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 53

def publish
  enforce_permission_to(:update, :questionnaire, questionnaire:)
  Decidim::Surveys::Admin::PublishSurvey.call(survey, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("publish.success", scope: "decidim.surveys.admin.surveys")
      redirect_to surveys_path
    end

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

#questionnaire_forObject



98
99
100
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 98

def questionnaire_for
  survey
end

#questionnaire_participants_urlObject



106
107
108
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 106

def questionnaire_participants_url
  Decidim::EngineRouter.admin_proxy(survey.component).survey_answers_path(survey)
end

#unpublishObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 68

def unpublish
  enforce_permission_to(:update, :questionnaire, questionnaire:)
  Decidim::Surveys::Admin::UnpublishSurvey.call(survey, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("unpublish.success", scope: "decidim.surveys.admin.surveys")
      redirect_to surveys_path
    end

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

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/decidim/surveys/admin/surveys_controller.rb', line 36

def update
  enforce_permission_to(:update, :questionnaire, questionnaire:)
  @form = form(Admin::SurveyForm).from_params(params)

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

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