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

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

Overview

Controller that allows managing templates.

Instance Method Summary collapse

Methods included from Decidim::TranslatableAttributes

#default_locale?

Methods inherited from ApplicationController

#permission_class_chain, #template_types

Methods inherited from Admin::ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from NeedsSnippets

#snippets

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#applyObject


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'decidim-templates/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[:questionnaire][:questionnaire_template_id])

  ApplyQuestionnaireTemplate.call(questionnaire, template) do
    on(:ok) do
      flash[:notice] = I18n.t("templates.apply.success", scope: "decidim.admin")
      redirect_to params[:url]
    end
    on(:invalid) do
      flash.now[:error] = I18n.t("templates.apply.error", scope: "decidim.admin")
    end
  end
end

#copyObject


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

def copy
  enforce_permission_to :copy, :template

  CopyQuestionnaireTemplate.call(template) 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 'decidim-templates/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 'decidim-templates/app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 93

def destroy
  enforce_permission_to :destroy, :template, template: template

  DestroyTemplate.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 'decidim-templates/app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 70

def edit
  enforce_permission_to :update, :template, 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 'decidim-templates/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 'decidim-templates/app/controllers/decidim/templates/admin/questionnaire_templates_controller.rb', line 31

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

#previewObject


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

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


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

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

#updateObject


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

def update
  enforce_permission_to :update, :template, 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