Class: Decidim::AmendmentsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, FormFactory
Defined in:
app/controllers/concerns/decidim/amendments_controller.rb

Instance Method Summary collapse

Methods included from ApplicationHelper

#cell, #edit_link, #extra_admin_link, #html_truncate, #present, #resolve_presenter_class, #step_cta_url

Methods included from AmendmentsHelper

#accept_and_reject_buttons_for, #action_button_card_for, #allowed_to_accept_and_reject?, #allowed_to_promote?, #amend_button_for, #amenders_list_for, #amendments_enabled?, #amendments_for, #amendments_form_field_for, #amendments_form_fields_label, #amendments_form_fields_value, #can_participate_in_private_space?, #can_react_to_emendation?, #emendation_actions_for, #emendation_announcement_for, #promote_button_for, #render_emendation_body, #user_group_select_field

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_picker_tag

Methods included from TranslatableAttributes

#default_locale?

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, #foundation_datepicker_locale_tag, #name_with_locale, #scopes_picker_field_tag, #tab_element_class_for, #translated_field_tag

Methods included from OmniauthHelper

#normalize_provider_name, #oauth_icon, #provider_name

Methods included from NeedsSnippets

#snippets

Methods included from HttpCachingDisabler

#disable_http_caching

Methods included from RegistersPermissions

register_permissions

Methods included from NeedsOrganization

enhance_controller, extended, included

Instance Method Details

#acceptObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 158

def accept
  enforce_permission_to :accept, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::ReviewForm).from_params(params)

  Decidim::Amendable::Accept.call(@form) do
    on(:ok) do |emendation|
      flash[:notice] = t("accepted.success", scope: "decidim.amendments")
      redirect_to Decidim::ResourceLocatorPresenter.new(emendation).path
    end

    on(:invalid) do
      flash.now[:alert] = t("accepted.error", scope: "decidim.amendments")
      render :review
    end
  end
end

#compare_draftObject



45
46
47
48
49
50
51
52
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 45

def compare_draft
  enforce_permission_to :create, :amendment, current_component: amendable.component

  if similar_emendations.empty?
    flash[:notice] = t("no_similars_found", scope: "decidim.amendments.compare_draft")
    redirect_to edit_draft_amend_path(amendment)
  end
end

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 27

def create
  enforce_permission_to :create, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::CreateForm).from_params(params)

  Decidim::Amendable::CreateDraft.call(@form) do
    on(:ok) do |amendment|
      flash[:notice] = t("created.success", scope: "decidim.amendments")
      redirect_to compare_draft_amend_path(amendment)
    end

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

#destroy_draftObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 78

def destroy_draft
  enforce_permission_to :create, :amendment, current_component: amendable.component

  Decidim::Amendable::DestroyDraft.call(amendment, current_user) do
    on(:ok) do |amendable|
      flash[:notice] = t("success", scope: "decidim.amendments.destroy_draft")
      redirect_to new_amend_path(amendable_gid: amendable.to_sgid.to_s)
    end

    on(:invalid) do
      flash[:alert] = t("error", scope: "decidim.amendments.destroy_draft")
      redirect_to edit_draft_amend_path(amendment)
    end
  end
end

#edit_draftObject



54
55
56
57
58
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 54

def edit_draft
  enforce_permission_to :create, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::EditForm).from_model(amendment)
end

#newObject

Raises:

  • (ActionController::RoutingError)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 13

def new
  raise ActionController::RoutingError, "Not Found" unless amendable

  enforce_permission_to :create, :amendment, current_component: amendable.component

  amendment_draft = amendable.amendments.find_by(amender: current_user.id, state: "draft")

  if amendment_draft
    redirect_to edit_draft_amend_path(amendment_draft)
  else
    @form = form(Decidim::Amendable::CreateForm).from_params(params)
  end
end

#preview_draftObject



94
95
96
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 94

def preview_draft
  enforce_permission_to :create, :amendment, current_component: amendable.component
end

#promoteObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 134

def promote
  enforce_permission_to :promote, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::PromoteForm).from_model(amendment)

  Decidim::Amendable::Promote.call(@form) do
    on(:ok) do |promoted_resource|
      flash[:notice] = I18n.t("promoted.success", scope: "decidim.amendments")
      redirect_to Decidim::ResourceLocatorPresenter.new(promoted_resource).path
    end

    on(:invalid) do
      flash.now[:alert] = t("promoted.error", scope: "decidim.amendments")
      redirect_to Decidim::ResourceLocatorPresenter.new(emendation).path
    end
  end
end

#publish_draftObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 98

def publish_draft
  enforce_permission_to :create, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::PublishForm).from_model(amendment)

  Decidim::Amendable::PublishDraft.call(@form) do
    on(:ok) do |emendation|
      flash[:notice] = t("success", scope: "decidim.amendments.publish_draft")
      redirect_to Decidim::ResourceLocatorPresenter.new(emendation).path
    end

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

#rejectObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 116

def reject
  enforce_permission_to :reject, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::RejectForm).from_model(amendment)

  Decidim::Amendable::Reject.call(@form) do
    on(:ok) do
      flash[:notice] = t("rejected.success", scope: "decidim.amendments")
    end

    on(:invalid) do
      flash[:alert] = t("rejected.error", scope: "decidim.amendments")
    end

    redirect_to Decidim::ResourceLocatorPresenter.new(emendation).path
  end
end

#reviewObject



152
153
154
155
156
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 152

def review
  enforce_permission_to :accept, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::ReviewForm).from_params(params)
end

#update_draftObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 60

def update_draft
  enforce_permission_to :create, :amendment, current_component: amendable.component

  @form = form(Decidim::Amendable::EditForm).from_params(params)

  Decidim::Amendable::UpdateDraft.call(@form) do
    on(:ok) do |amendment|
      flash[:notice] = t("success", scope: "decidim.amendments.update_draft")
      redirect_to preview_draft_amend_path(amendment)
    end

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

#withdrawObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/concerns/decidim/amendments_controller.rb', line 176

def withdraw
  enforce_permission_to :withdraw, :amendment, amendment: amendment, current_component: amendable.component

  Decidim::Amendable::Withdraw.call(amendment, current_user) do
    on(:ok) do |withdrawn_emendation|
      flash[:notice] = t("success", scope: "decidim.amendments.withdraw")
      redirect_to Decidim::ResourceLocatorPresenter.new(withdrawn_emendation).path
    end

    on(:invalid) do
      flash[:alert] = t("error", scope: "decidim.amendments.withdraw")
      redirect_to Decidim::ResourceLocatorPresenter.new(emendation).path
    end
  end
end