Class: Decidim::Elections::Admin::AnswersController

Inherits:
ApplicationController show all
Includes:
Proposals::Admin::Picker
Defined in:
decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb

Overview

This controller allows the create or update answers for a question.

Instance Method Summary collapse

Methods included from Proposals::Admin::Picker

#proposals_picker

Methods inherited from Admin::Components::BaseController

#current_component, #current_participatory_space, #parent_path, #permission_class_chain, #permission_scope, #permissions_context, #skip_manage_component_permission

Methods included from RegistersPermissions

register_permissions

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 NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#change_selected(selected) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 68

def change_selected(selected)
  enforce_permission_to :select, :answer, election: election, question: question

  UpdateAnswerSelection.call(answer, selected) do
    on(:ok) do
      flash[:notice] = if selected
                         I18n.t("answers.select.success", scope: "decidim.elections.admin")
                       else
                         I18n.t("answers.unselect.success", scope: "decidim.elections.admin")
                       end
    end

    on(:invalid) do
      flash.now[:alert] = if selected
                            I18n.t("answers.select.invalid", scope: "decidim.elections.admin")
                          else
                            I18n.t("answers.unselect.invalid", scope: "decidim.elections.admin")
                          end
    end
  end

  redirect_to election_question_answers_path(election, question)
end

#createObject


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 21

def create
  enforce_permission_to :update, :answer, election: election, question: question
  @form = form(AnswerForm).from_params(params, election: election, question: question)

  CreateAnswer.call(@form) do
    on(:ok) do
      flash[:notice] = I18n.t("answers.create.success", scope: "decidim.elections.admin")
      redirect_to election_question_answers_path(election, question)
    end

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

#destroyObject


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 92

def destroy
  enforce_permission_to :update, :answer, election: election, question: question

  DestroyAnswer.call(answer, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("answers.destroy.success", scope: "decidim.elections.admin")
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("answers.destroy.invalid", scope: "decidim.elections.admin")
    end
  end

  redirect_to election_question_answers_path(election, question)
end

#editObject


38
39
40
41
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 38

def edit
  enforce_permission_to :update, :answer, election: election, question: question
  @form = form(AnswerForm).from_model(answer)
end

#indexObject


12
13
14
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 12

def index
  flash.now[:alert] = I18n.t("answers.index.invalid_max_selections", scope: "decidim.elections.admin", missing_answers: missing_answers) if missing_answers.positive?
end

#newObject


16
17
18
19
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 16

def new
  enforce_permission_to :update, :answer, election: election, question: question
  @form = form(AnswerForm).instance
end

#selectObject


60
61
62
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 60

def select
  change_selected(true)
end

#unselectObject


64
65
66
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 64

def unselect
  change_selected(false)
end

#updateObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'decidim-elections/app/controllers/decidim/elections/admin/answers_controller.rb', line 43

def update
  enforce_permission_to :update, :answer, election: election, question: question
  @form = form(AnswerForm).from_params(params, election: election, question: question)

  UpdateAnswer.call(@form, answer) do
    on(:ok) do
      flash[:notice] = I18n.t("answers.update.success", scope: "decidim.elections.admin")
      redirect_to election_question_answers_path(election, question)
    end

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