Class: Decidim::Initiatives::Admin::InitiativesController

Inherits:
ApplicationController show all
Includes:
Filterable, NeedsInitiative, SingleInitiativeType, TypeSelectorOptions
Defined in:
decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb

Overview

Controller used to manage the initiatives

Instance Method Summary collapse

Methods included from TranslationsHelper

empty_translatable, ensure_translatable, multi_translation, translated_in_current_locale?

Methods included from TranslatableAttributes

#default_locale?

Methods inherited from ApplicationController

#permission_class_chain, #permissions_context

Methods inherited from Admin::ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

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

#acceptObject

POST /admin/initiatives/:id/accept


95
96
97
98
99
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 95

def accept
  enforce_permission_to :accept, :initiative, initiative: current_initiative
  current_initiative.accepted!
  redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
end

#discardObject

DELETE /admin/initiatives/:id/discard


88
89
90
91
92
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 88

def discard
  enforce_permission_to :discard, :initiative, initiative: current_initiative
  current_initiative.discarded!
  redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
end

#editObject

GET /admin/initiatives/:id/edit


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 30

def edit
  enforce_permission_to :edit, :initiative, initiative: current_initiative

  form_attachment_model = form(AttachmentForm).from_model(current_initiative.attachments.first)
  @form = form(Decidim::Initiatives::Admin::InitiativeForm)
          .from_model(
            current_initiative,
            initiative: current_initiative
          )
  @form.attachment = form_attachment_model

  render layout: "decidim/admin/initiative"
end

#exportObject

GET /admin/initiatives/export


125
126
127
128
129
130
131
132
133
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 125

def export
  enforce_permission_to :export, :initiatives

  Decidim::Initiatives::ExportInitiativesJob.perform_later(current_user, params[:format] || default_format)

  flash[:notice] = t("decidim.admin.exports.notice")

  redirect_back(fallback_location: initiatives_path)
end

#export_pdf_signaturesObject

GET /admin/initiatives/:id/export_pdf_signatures.pdf


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 152

def export_pdf_signatures
  enforce_permission_to :export_pdf_signatures, :initiative, initiative: current_initiative

  @votes = current_initiative.votes

  output = render_to_string(
    pdf: "votes_#{current_initiative.id}",
    layout: "decidim/admin/initiatives_votes",
    template: "decidim/initiatives/admin/initiatives/export_pdf_signatures.pdf.erb"
  )
  output = pdf_signature_service.new(pdf: output).signed_pdf if pdf_signature_service

  respond_to do |format|
    format.pdf do
      send_data(output, filename: "votes_#{current_initiative.id}.pdf", type: "application/pdf")
    end
  end
end

#export_votesObject

GET /admin/initiatives/:id/export_votes


136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 136

def export_votes
  enforce_permission_to :export_votes, :initiative, initiative: current_initiative

  votes = current_initiative.votes.map(&:sha1)
  csv_data = CSV.generate(headers: false) do |csv|
    votes.each do |sha1|
      csv << [sha1]
    end
  end

  respond_to do |format|
    format.csv { send_data csv_data, file_name: "votes.csv" }
  end
end

#indexObject

GET /admin/initiatives


19
20
21
22
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 19

def index
  enforce_permission_to :list, :initiative
  @initiatives = filtered_collection
end

#publishObject

POST /admin/initiatives/:id/publish


66
67
68
69
70
71
72
73
74
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 66

def publish
  enforce_permission_to :publish, :initiative, initiative: current_initiative

  PublishInitiative.call(current_initiative, current_user) do
    on(:ok) do
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#rejectObject

DELETE /admin/initiatives/:id/reject


102
103
104
105
106
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 102

def reject
  enforce_permission_to :reject, :initiative, initiative: current_initiative
  current_initiative.rejected!
  redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
end

#send_to_technical_validationObject

GET /admin/initiatives/:id/send_to_technical_validation


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

def send_to_technical_validation
  enforce_permission_to :send_to_technical_validation, :initiative, initiative: current_initiative

  SendInitiativeToTechnicalValidation.call(current_initiative, current_user) do
    on(:ok) do
      redirect_to EngineRouter.main_proxy(current_initiative).initiatives_path(initiative_slug: nil), flash: {
        notice: I18n.t(
          "success",
          scope: %w(decidim initiatives admin initiatives edit)
        )
      }
    end
  end
end

#showObject

GET /admin/initiatives/:id


25
26
27
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 25

def show
  enforce_permission_to :read, :initiative, initiative: current_initiative
end

#unpublishObject

DELETE /admin/initiatives/:id/unpublish


77
78
79
80
81
82
83
84
85
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 77

def unpublish
  enforce_permission_to :unpublish, :initiative, initiative: current_initiative

  UnpublishInitiative.call(current_initiative, current_user) do
    on(:ok) do
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#updateObject

PUT /admin/initiatives/:id


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 45

def update
  enforce_permission_to :update, :initiative, initiative: current_initiative

  params[:id] = params[:slug]
  @form = form(Decidim::Initiatives::Admin::InitiativeForm)
          .from_params(params, initiative: current_initiative)

  Decidim::Initiatives::Admin::UpdateInitiative.call(current_initiative, @form, current_user) do
    on(:ok) do |initiative|
      flash[:notice] = I18n.t("initiatives.update.success", scope: "decidim.initiatives.admin")
      redirect_to edit_initiative_path(initiative)
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("initiatives.update.error", scope: "decidim.initiatives.admin")
      render :edit, layout: "decidim/admin/initiative"
    end
  end
end