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

Inherits:
ApplicationController show all
Includes:
Admin::ParticipatorySpaceAdminBreadcrumb, Filterable
Defined in:
decidim-elections/app/controllers/decidim/votings/admin/votings_controller.rb

Overview

This controller allows to create or update a voting.

Instance Method Summary collapse

Methods inherited from Admin::ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Methods included from Headers::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

#available_polling_officersObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'decidim-elections/app/controllers/decidim/votings/admin/votings_controller.rb', line 63

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



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

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



40
41
42
43
44
# File 'decidim-elections/app/controllers/decidim/votings/admin/votings_controller.rb', line 40

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

#indexObject



12
13
14
15
16
# File 'decidim-elections/app/controllers/decidim/votings/admin/votings_controller.rb', line 12

def index
  enforce_permission_to :read, :votings
  @votings = filtered_collection
  render layout: "decidim/admin/votings"
end

#newObject



18
19
20
21
# File 'decidim-elections/app/controllers/decidim/votings/admin/votings_controller.rb', line 18

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

#updateObject



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

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