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

Inherits:
ApplicationController show all
Defined in:
decidim-elections/app/controllers/decidim/elections/admin/elections_controller.rb

Overview

This controller allows the create or update an election.

Instance Method Summary collapse

Methods inherited from Admin::Components::BaseController

#current_component, #current_participatory_space, #parent_path, #permission_class_chain, #permission_scope, #permissions_context, #set_component_breadcrumb_item, #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 Headers::HttpCachingDisabler

#disable_http_caching

Methods included from NeedsSnippets

#snippets

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject



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

def create
  enforce_permission_to :create, :election
  @form = form(ElectionForm).from_params(params, current_component:)

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

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

#destroyObject



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

def destroy
  enforce_permission_to(:delete, :election, election:)

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

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

  redirect_to elections_path
end

#editObject



36
37
38
39
# File 'decidim-elections/app/controllers/decidim/elections/admin/elections_controller.rb', line 36

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

#indexObject



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

def index
  flash.now[:alert] ||= I18n.t("elections.index.no_bulletin_board", scope: "decidim.elections.admin").html_safe unless Decidim::Elections.bulletin_board.configured?
end

#newObject



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

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

#publishObject



74
75
76
77
78
79
80
81
82
83
# File 'decidim-elections/app/controllers/decidim/elections/admin/elections_controller.rb', line 74

def publish
  enforce_permission_to(:publish, :election, election:)

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

#unpublishObject



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

def unpublish
  enforce_permission_to(:unpublish, :election, election:)

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

#updateObject



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

def update
  enforce_permission_to(:update, :election, election:)
  @form = form(ElectionForm).from_params(params, current_component:)

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

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