Class: Decidim::Elections::Admin::QuestionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/decidim/elections/admin/questions_controller.rb

Overview

This controller allows the create or update questions for an election.

Instance Method Summary collapse

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/decidim/elections/admin/questions_controller.rb', line 15

def create
  enforce_permission_to :create, :question, election: election
  @form = form(QuestionForm).from_params(params, election: election)

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

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

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

#destroyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/decidim/elections/admin/questions_controller.rb', line 59

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

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

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

  redirect_to election_questions_path(election)
end

#editObject



37
38
39
40
# File 'app/controllers/decidim/elections/admin/questions_controller.rb', line 37

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

#newObject



10
11
12
13
# File 'app/controllers/decidim/elections/admin/questions_controller.rb', line 10

def new
  enforce_permission_to :create, :question, election: election
  @form = form(QuestionForm).instance
end

#updateObject



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

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

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

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