Class: Decidim::Templates::Admin::ProposalAnswerTemplatesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain

Instance Method Details

#copyObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 93

def copy
  enforce_permission_to :copy, :template

  CopyProposalAnswerTemplate.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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 24

def create
  enforce_permission_to :create, :template

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

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

    on(:component_selected) do
      render :new
    end

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

#destroyObject



46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 46

def destroy
  enforce_permission_to(:destroy, :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



19
20
21
22
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 19

def edit
  enforce_permission_to(:update, :template, template:)
  @form = form(ProposalAnswerTemplateForm).from_model(template)
end

#fetchObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 57

def fetch
  enforce_permission_to(:read, :template, template:, proposal:)

  return render json: { msg: I18n.t("templates.fetch.error", scope: "decidim.admin") }, status: :unprocessable_entity if template.blank?

  state = fetch_proposal_state(template)

  response_object = {
    state: state&.token,
    template: populate_template_interpolations(proposal)
  }

  respond_to do |format|
    format.json do
      render json: response_object.to_json
    end
  end
end

#indexObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 109

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



14
15
16
17
# File 'app/controllers/decidim/templates/admin/proposal_answer_templates_controller.rb', line 14

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

#updateObject



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

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

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