Class: Decidim::Votings::Admin::VotingsController

Inherits:
ApplicationController show all
Includes:
Filterable
Defined in:
app/controllers/decidim/votings/admin/votings_controller.rb

Overview

This controller allows to create or update a voting.

Instance Method Summary collapse

Instance Method Details

#available_polling_officersObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 83

def available_polling_officers
  respond_to do |format|
    format.json do
      if (term = params[:term].to_s).present?
        query = current_voting.available_polling_officers.joins(:user)
        query = if term.start_with?("@")
                  query.where("nickname ILIKE ?", "#{term.delete("@")}%")
                else
                  query.where("name ILIKE ?", "%#{term}%").or(
                    query.where("email ILIKE ?", "%#{term}%")
                  )
                end
        render json: query.all.collect { |u| { value: u.id, label: "#{u.name} (@#{u.nickname}) #{u.email}" } }
      else
        render json: []
      end
    end
  end
end

#createObject



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

def create
  enforce_permission_to :create, :voting
  @form = form(VotingForm).from_params(params)

  CreateVoting.call(@form) do
    on(:ok) do
      flash[:notice] = I18n.t("votings.create.success", scope: "decidim.votings.admin")
      redirect_to votings_path
    end

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

#editObject



38
39
40
41
42
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 38

def edit
  enforce_permission_to :edit, :voting, voting: current_voting
  @form = form(VotingForm).from_model(current_voting)
  render layout: "decidim/admin/voting"
end

#indexObject



11
12
13
14
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 11

def index
  enforce_permission_to :read, :votings
  @votings = filtered_collection
end

#newObject



16
17
18
19
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 16

def new
  enforce_permission_to :create, :voting
  @form = form(VotingForm).instance
end

#polling_officers_pickerObject



103
104
105
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 103

def polling_officers_picker
  render :polling_officers_picker, layout: false
end

#publishObject



61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 61

def publish
  enforce_permission_to :publish, :voting, voting: current_voting

  PublishVoting.call(current_voting, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("votings.publish.success", scope: "decidim.votings.admin")
      redirect_to edit_voting_path(voting)
    end
  end
end

#unpublishObject



72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 72

def unpublish
  enforce_permission_to :unpublish, :voting, voting: current_voting

  UnpublishVoting.call(current_voting, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("votings.unpublish.success", scope: "decidim.votings.admin")
      redirect_to edit_voting_path(voting)
    end
  end
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/decidim/votings/admin/votings_controller.rb', line 44

def update
  enforce_permission_to :update, :voting, voting: current_voting
  @form = form(VotingForm).from_params(params, voting_id: current_voting.id)

  UpdateVoting.call(current_voting, @form) do
    on(:ok) do |voting|
      flash[:notice] = I18n.t("votings.update.success", scope: "decidim.votings.admin")
      redirect_to edit_voting_path(voting)
    end

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