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

Inherits:
ApplicationController show all
Includes:
Admin::ParticipatorySpaceAdminBreadcrumb, 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 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

#acceptObject

POST /admin/initiatives/:id/accept



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

def accept
  enforce_permission_to :accept, :initiative, initiative: current_initiative
  AcceptInitiative.call(current_initiative, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("initiatives.accept.success", scope: "decidim.initiatives.admin")
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#discardObject

DELETE /admin/initiatives/:id/discard



89
90
91
92
93
94
95
96
97
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 89

def discard
  enforce_permission_to :discard, :initiative, initiative: current_initiative
  DiscardInitiative.call(current_initiative, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("initiatives.discard.success", scope: "decidim.initiatives.admin")
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#editObject

GET /admin/initiatives/:id/edit



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

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



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

def export
  enforce_permission_to :export, :initiatives

  Decidim::Initiatives::ExportInitiativesJob.perform_later(
    current_user,
    current_organization,
    params[:format] || default_format,
    params[:collection_ids].presence&.map(&:to_i)
  )

  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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'decidim-initiatives/app/controllers/decidim/initiatives/admin/initiatives_controller.rb', line 170

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",
    format: [:pdf]
  )
  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
    format.html
  end
end

#export_votesObject

GET /admin/initiatives/:id/export_votes



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

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



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

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

#publishObject

POST /admin/initiatives/:id/publish



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

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

  PublishInitiative.call(current_initiative, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("initiatives.publish.success", scope: "decidim.initiatives.admin")
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#rejectObject

DELETE /admin/initiatives/:id/reject



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

def reject
  enforce_permission_to :reject, :initiative, initiative: current_initiative
  RejectInitiative.call(current_initiative, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("initiatives.reject.success", scope: "decidim.initiatives.admin")
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#send_to_technical_validationObject

GET /admin/initiatives/:id/send_to_technical_validation



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

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: "decidim.initiatives.admin.initiatives.edit"
        )
      }
    end
  end
end

#unpublishObject

DELETE /admin/initiatives/:id/unpublish



77
78
79
80
81
82
83
84
85
86
# 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
      flash[:notice] = I18n.t("initiatives.unpublish.success", scope: "decidim.initiatives.admin")
      redirect_to decidim_admin_initiatives.edit_initiative_path(current_initiative)
    end
  end
end

#updateObject

PUT /admin/initiatives/:id



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

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(@form, current_initiative) 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