Class: Decidim::Proposals::ProposalsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, AttachmentsHelper, FilterResource, Flaggable, FormFactory, Decidim::Paginable, Orderable, Withdrawable
Defined in:
decidim-proposals/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

Constants included from Decidim::Paginable

Decidim::Paginable::OPTIONS

Constants included from AmendmentsHelper

AmendmentsHelper::TOTAL_STEPS

Instance Method Summary collapse

Methods included from AttachmentsHelper

#attachment_title, #attachments_for, #attachments_tab_panel_items

Methods included from IconHelper

#component_icon, #manifest_icon, #resource_icon, #resource_type_icon, #resource_type_icon_key, #text_with_resource_icon

Methods included from LayoutHelper

#_icon_classes, #apple_favicon, #application_path, #current_url, #current_user_unread_data, #extended_navigation_bar, #external_icon, #favicon, #icon, #legacy_favicon, #role

Methods included from TooltipHelper

#with_tooltip

Methods included from ModalHelper

#decidim_drawer, #decidim_modal

Methods included from OrganizationHelper

#current_organization_name, #organization_colors, #organization_description_label, #organization_name

Methods included from TranslatableAttributes

#default_locale?

Methods included from ApplicationHelper

#add_body_classes, #cell, #edit_link, #extra_admin_link, #html_truncate, #present, #prevent_timeout_seconds, #resolve_presenter_class, #text_initials

Methods included from CacheHelper

#cache

Methods included from AmendmentsHelper

#accept_and_reject_buttons_for, #action_button_card_for, #allowed_to_accept_and_reject?, #allowed_to_promote?, #amendments_enabled?, #amendments_form_field_for, #amendments_form_fields_label, #amendments_form_fields_value, #can_participate_in_private_space?, #can_react_to_emendation?, #current_step, #emendation_actions_for, #emendation_announcement_for, #promote_button_for, #render_emendation_body, #total_steps, #wizard_aside_back_url, #wizard_header_title

Methods included from RichTextEditorHelper

included, #text_editor_for

Methods included from ContextualHelpHelper

#floating_help

Methods included from ScopesHelper

#has_visible_scopes?, #scope_name_for_picker, #scopes_picker_field, #scopes_picker_filter, #scopes_select_field, #scopes_select_tag

Methods included from DecidimFormHelper

#areas_for_select, #base_error_messages, #decidim_form_for, #decidim_form_slug_url, #editor_field_tag, #form_field_has_error?, #form_required_explanation, #name_with_locale, #ordered_scopes_descendants, #ordered_scopes_descendants_for_select, #scopes_picker_field_tag, #tab_element_class_for, #translated_field_tag

Methods included from OmniauthHelper

#normalize_provider_name, #oauth_icon, #provider_name

Methods inherited from ApplicationController

#proposal_limit, #proposal_limit_reached?, #proposals

Methods inherited from Components::BaseController

#current_component, #current_manifest, #current_participatory_space, #permission_class_chain, #permission_scope, #redirect_unless_feature_private, #set_component_breadcrumb_item

Methods included from RegistersPermissions

register_permissions

Methods inherited from ApplicationController

#store_share_token

Methods included from UserBlockedChecker

#check_user_block_status, #check_user_not_blocked

Methods included from NeedsSnippets

#snippets

Methods included from Headers::HttpCachingDisabler

#disable_http_caching

Methods included from HasStoredPath

#skip_store_location?, #store_current_location

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#createObject



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

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



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

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



160
161
162
# File 'decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb', line 160

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

#edit_draftObject



121
122
123
124
# File 'decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb', line 121

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
# File 'decidim-proposals/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(:taxonomies, :attachments, :coauthorships)
                 .order(position: :asc)
    render "decidim/proposals/proposals/participatory_texts/participatory_text"
  else
    @proposals = search.result

    @all_geocoded_proposals = @proposals.geocoded if Decidim::Map.available?(:geocoding, :dynamic) && component_settings.geocoding_enabled?

    @proposals = reorder(@proposals)
    @proposals = paginate(@proposals)
    @proposals = @proposals.includes(:component, :coauthorships, :attachments)

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

#newObject



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

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



99
100
101
102
103
# File 'decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb', line 99

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

#publishObject



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

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)


65
66
67
# File 'decidim-proposals/app/controllers/decidim/proposals/proposals_controller.rb', line 65

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

#updateObject



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

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



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

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



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

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