Class: Decidim::Templates::Admin::QuestionnaireTemplatesController

Inherits:
ApplicationController
  • Object
show all
Includes:
Decidim::TranslatableAttributes
Defined in:
app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb

Overview

Controller that allows managing templates.

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain

Instance Method Details

#applyObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 104

def apply
  questionnaire = Decidim::Forms::Questionnaire.find_by(id: params[:questionnaire_id])
  template = Decidim::Templates::Template.find_by(id: params.dig(:questionnaire, :questionnaire_template_id))

  ApplyQuestionnaireTemplate.call(questionnaire, template) do
    on(:ok) do
      flash[:notice] = I18n.t("templates.apply.success", scope: "decidim.admin")
      redirect_to URI.parse(params[:url]).path
    end
    on(:invalid) do
      flash[:error] = I18n.t("templates.apply.error", scope: "decidim.admin")
      redirect_to EngineRouter.admin_proxy(questionnaire.questionnaire_for.component).survey_path
    end
  end
end

#copyObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 54

def copy
  enforce_permission_to :copy, :template

  CopyQuestionnaireTemplate.call(template, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("templates.copy.success", scope: "decidim.admin")
      redirect_to action: :index
    end

    on(:invalid) do
      flash[:alert] = I18n.t("templates.copy.error", scope: "decidim.admin")
      redirect_to action: :index
    end
  end
end

#createObject



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

def create
  enforce_permission_to :create, :template

  @form = form(TemplateForm).from_params(params)

  CreateQuestionnaireTemplate.call(@form) do
    on(:ok) do |questionnaire_template|
      flash[:notice] = I18n.t("templates.create.success", scope: "decidim.admin")
      redirect_to edit_questionnaire_template_path(questionnaire_template)
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("templates.create.error", scope: "decidim.admin")
      render :new
    end
  end
end

#destroyObject



93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 93

def destroy
  enforce_permission_to(:destroy, :template, template:)

  DestroyQuestionnaireTemplate.call(template, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("templates.destroy.success", scope: "decidim.admin")
      redirect_to action: :index
    end
  end
end

#editObject



70
71
72
73
74
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 70

def edit
  enforce_permission_to(:update, :template, template:)
  @form = form(TemplateForm).from_model(template)
  @preview_form = form(Decidim::Forms::QuestionnaireForm).from_model(template.templatable)
end

#indexObject



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

def index
  enforce_permission_to :index, :templates
  @templates = collection

  respond_to do |format|
    format.html { render :index }
    format.json do
      term = params[:term]

      @templates = search(term)

      render json: @templates.map { |t| { value: t.id, label: translated_attribute(t.name) } }
    end
  end
end

#newObject



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

def new
  enforce_permission_to :create, :template
  @form = form(TemplateForm).instance
end

#previewObject



120
121
122
123
124
125
126
127
128
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 120

def preview
  respond_to do |format|
    format.js do
      @template = template
      @questionnaire = @template.templatable
      @preview_form = form(Decidim::Forms::QuestionnaireForm).from_model(@questionnaire)
    end
  end
end

#skipObject



130
131
132
133
134
135
136
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 130

def skip
  questionnaire = Decidim::Forms::Questionnaire.find_by(id: params[:questionnaire_id])
  # rubocop:disable Rails/SkipsModelValidations
  questionnaire.touch
  # rubocop:enable Rails/SkipsModelValidations
  redirect_to URI.parse(params[:url]).path
end

#updateObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 76

def update
  enforce_permission_to(:update, :template, template:)
  @form = form(TemplateForm).from_params(params)
  UpdateTemplate.call(template, @form, current_user) do
    on(:ok) do |questionnaire_template|
      flash[:notice] = I18n.t("templates.update.success", scope: "decidim.admin")
      redirect_to edit_questionnaire_template_path(questionnaire_template)
    end

    on(:invalid) do |template|
      @template = template
      flash.now[:error] = I18n.t("templates.update.error", scope: "decidim.admin")
      render action: :edit
    end
  end
end