Class: Decidim::Elections::Admin::ElectionsController

Inherits:
ApplicationController
  • Object
show all
Includes:
Admin::HasTrashableResources, ApplicationHelper, Filterable
Defined in:
app/controllers/decidim/elections/admin/elections_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 25

def create
  enforce_permission_to :create, :election

  @form = form(Decidim::Elections::Admin::ElectionForm).from_params(params, current_component:)

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

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

#dashboardObject



98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 98

def dashboard
  enforce_permission_to :dashboard, :election, election: election

  respond_to do |format|
    format.html { render :dashboard }
    format.json do
      render json: election.presenter.to_json(admin: true) # Admins see all votes, not just the published results
    end
  end
end

#editObject



43
44
45
46
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 43

def edit
  enforce_permission_to :update, :election, election:
  @form = form(Decidim::Elections::Admin::ElectionForm).from_model(election)
end

#indexObject



14
15
16
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 14

def index
  enforce_permission_to :read, :election
end

#newObject



18
19
20
21
22
23
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 18

def new
  enforce_permission_to :create, :election
  @form = form(Decidim::Elections::Admin::ElectionForm).from_params(
    attachment: form(AttachmentForm).from_params({})
  )
end

#publishObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 66

def publish
  enforce_permission_to :publish, :election, election: election

  PublishElection.call(election, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("elections.publish.success", scope: "decidim.elections.admin")
      redirect_to dashboard_election_path(election)
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("elections.publish.invalid", scope: "decidim.elections.admin")
      render action: "index", status: :unprocessable_entity
    end
  end
end

#unpublishObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 82

def unpublish
  enforce_permission_to :unpublish, :election, election: election

  Decidim::Elections::Admin::UnpublishElection.call(election, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("elections.unpublish.success", scope: "decidim.elections.admin")
      redirect_to elections_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("elections.unpublish.invalid", scope: "decidim.elections.admin")
      render action: "index", status: :unprocessable_entity
    end
  end
end

#updateObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/decidim/elections/admin/elections_controller.rb', line 48

def update
  enforce_permission_to :update, :election, election: election

  @form = form(Decidim::Elections::Admin::ElectionForm).from_params(params, current_component:, election:)

  UpdateElection.call(@form, election) do
    on(:ok) do
      flash[:notice] = I18n.t("elections.update.success", scope: "decidim.elections.admin")
      redirect_to election.published? ? dashboard_election_path(election) : edit_questions_election_path(election)
    end

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

#update_statusObject



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

def update_status
  enforce_permission_to :update, :election, election: election

  status_action = params[:status_action]
  UpdateElectionStatus.call(status_action, election) do
    on(:ok) do
      flash[:notice] = I18n.t("statuses.#{status_action}.success", scope: "decidim.elections.admin")
    end

    on(:invalid) do
      flash[:alert] = I18n.t("statuses.unknown", scope: "decidim.elections.admin")
    end
  end
  redirect_to dashboard_election_path(election)
end