Class: Decidim::Proposals::ProposalsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, AttachmentsHelper, Orderable, FilterResource, Flaggable, FormFactory, Paginable, Withdrawable
Defined in:
app/controllers/decidim/proposals/proposals_controller.rb

Overview

Exposes the proposal resource so users can view and create them.

Constant Summary collapse

STEP1 =

rubocop:disable Naming/VariableNumber

:step_1
STEP2 =
:step_2

Instance Method Summary collapse

Methods inherited from ApplicationController

#proposal_limit, #proposal_limit_reached?, #proposals

Instance Method Details

#createObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 81

def create
  enforce_permission_to :create, :proposal
  @step = STEP1
  @form = form(ProposalForm).from_params(proposal_creation_params)

  CreateProposal.call(@form, current_user) do
    on(:ok) do |proposal|
      flash[:notice] = I18n.t("proposals.create.success", scope: "decidim")

      @proposal = proposal
      redirect_to "#{Decidim::ResourceLocatorPresenter.new(proposal).path}/preview"
    end

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

#destroy_draftObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 146

def destroy_draft
  enforce_permission_to :edit, :proposal, proposal: @proposal

  DestroyProposal.call(@proposal, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("proposals.destroy_draft.success", scope: "decidim")
      redirect_to new_proposal_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("proposals.destroy_draft.error", scope: "decidim")
      render :edit_draft
    end
  end
end

#editObject



162
163
164
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 162

def edit
  enforce_permission_to :edit, :proposal, proposal: @proposal
end

#edit_draftObject



123
124
125
126
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 123

def edit_draft
  @step = STEP1
  enforce_permission_to :edit, :proposal, proposal: @proposal
end

#indexObject

rubocop:enable Naming/VariableNumber



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 35

def index
  if component_settings.participatory_texts_enabled?
    @proposals = Decidim::Proposals::Proposal
                 .where(component: current_component)
                 .published
                 .not_hidden
                 .only_amendables
                 .includes(:category, :scope, :attachments, :coauthorships)
                 .order(position: :asc)
    render "decidim/proposals/proposals/participatory_texts/participatory_text"
  else
    @base_query = search
                  .result
                  .published
                  .not_hidden

    @proposals = @base_query.includes(:component, :coauthorships, :attachments)
    @all_geocoded_proposals = @base_query.geocoded

    @voted_proposals = if current_user
                         ProposalVote.where(
                           author: current_user,
                           proposal: @proposals.pluck(:id)
                         ).pluck(:decidim_proposal_id)
                       else
                         []
                       end
    @proposals = reorder(@proposals)
    @proposals = paginate(@proposals)
  end
end

#newObject



71
72
73
74
75
76
77
78
79
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 71

def new
  enforce_permission_to :create, :proposal
  @step = STEP1
  if proposal_draft.present?
    redirect_to edit_draft_proposal_path(proposal_draft, component_id: proposal_draft.component.id, question_slug: proposal_draft.component.participatory_space.slug)
  else
    @form = form(ProposalForm).from_params(body: translated_proposal_body_template)
  end
end

#previewObject



101
102
103
104
105
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 101

def preview
  enforce_permission_to :edit, :proposal, proposal: @proposal
  @step = STEP2
  @form = form(ProposalForm).from_model(@proposal)
end

#publishObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 107

def publish
  enforce_permission_to :edit, :proposal, proposal: @proposal
  @step = STEP2
  PublishProposal.call(@proposal, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("proposals.publish.success", scope: "decidim")
      redirect_to proposal_path(@proposal)
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("proposals.publish.error", scope: "decidim")
      render :edit_draft
    end
  end
end

#showObject

Raises:

  • (ActionController::RoutingError)


67
68
69
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 67

def show
  raise ActionController::RoutingError, "Not Found" if @proposal.blank? || !can_show_proposal?
end

#updateObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 166

def update
  enforce_permission_to :edit, :proposal, proposal: @proposal

  @form = form_proposal_params
  UpdateProposal.call(@form, current_user, @proposal) do
    on(:ok) do |proposal|
      flash[:notice] = I18n.t("proposals.update.success", scope: "decidim")
      redirect_to Decidim::ResourceLocatorPresenter.new(proposal).path
    end

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

#update_draftObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 128

def update_draft
  @step = STEP1
  enforce_permission_to :edit, :proposal, proposal: @proposal

  @form = form_proposal_params
  UpdateProposal.call(@form, current_user, @proposal) do
    on(:ok) do |proposal|
      flash[:notice] = I18n.t("proposals.update_draft.success", scope: "decidim")
      redirect_to "#{Decidim::ResourceLocatorPresenter.new(proposal).path}/preview"
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("proposals.update_draft.error", scope: "decidim")
      render :edit_draft
    end
  end
end

#withdrawObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/controllers/decidim/proposals/proposals_controller.rb', line 183

def withdraw
  enforce_permission_to :withdraw, :proposal, proposal: @proposal

  WithdrawProposal.call(@proposal, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("proposals.update.success", scope: "decidim")
      redirect_to Decidim::ResourceLocatorPresenter.new(@proposal).path
    end
    on(:has_votes) do
      flash[:alert] = I18n.t("proposals.withdraw.errors.has_votes", scope: "decidim")
      redirect_to Decidim::ResourceLocatorPresenter.new(@proposal).path
    end
  end
end