Class: Decidim::EnhancedTextwork::ParagraphsController

Inherits:
ApplicationController
  • Object
show all
Includes:
ApplicationHelper, Orderable, FilterResource, Flaggable, FormFactory, Paginable, Withdrawable
Defined in:
app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb

Overview

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#paragraph_limit, #paragraph_limit_reached?, #paragraphs

Instance Method Details

#compareObject



111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 111

def compare
  @step = :step_2
  @similar_paragraphs ||= Decidim::EnhancedTextwork::SimilarParagraphs
                         .for(current_component, @paragraph)
                         .all

  if @similar_paragraphs.blank?
    flash[:notice] = I18n.t("paragraphs.paragraphs.compare.no_similars_found", scope: "decidim")
    redirect_to "#{Decidim::ResourceLocatorPresenter.new(@paragraph).path}/complete"
  end
end

#completeObject



123
124
125
126
127
128
129
130
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 123

def complete
  enforce_permission_to :create, :paragraph
  @step = :step_3

  @form = form_paragraph_model

  @form.attachment = form_attachment_new
end

#createObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 92

def create
  enforce_permission_to :create, :paragraph
  @step = :step_1
  @form = form(ParagraphWizardCreateStepForm).from_params(paragraph_creation_params)

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

      redirect_to "#{Decidim::ResourceLocatorPresenter.new(paragraph).path}/compare"
    end

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

#destroy_draftObject



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

def destroy_draft
  enforce_permission_to :edit, :paragraph, paragraph: @paragraph

  DestroyParagraph.call(@paragraph, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("paragraphs.destroy_draft.success", scope: "decidim")
      redirect_to new_paragraph_path
    end

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

#editObject



191
192
193
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 191

def edit
  enforce_permission_to :edit, :paragraph, paragraph: @paragraph
end

#edit_draftObject



152
153
154
155
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 152

def edit_draft
  @step = :step_3
  enforce_permission_to :edit, :paragraph, paragraph: @paragraph
end

#indexObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 28

def index
  if component_settings.participatory_texts_enabled?
    @paragraphs = Decidim::EnhancedTextwork::Paragraph
                 .where(component: current_component)
                 .published
                 .not_hidden
                 .only_amendables
                 .includes(:category, :scope)
                 .order(position: :asc)
    render "decidim/enhanced_textwork/paragraphs/participatory_texts/participatory_text"
  else
    @base_query = search
                  .results
                  .published
                  .not_hidden

    @paragraphs = @base_query.includes(:component, :coauthorships)
    @all_geocoded_paragraphs = @base_query.geocoded

    @voted_paragraphs = if current_user
                         ParagraphVote.where(
                           author: current_user,
                           paragraph: @paragraphs.pluck(:id)
                         ).pluck(:decidim_paragraph_id)
                       else
                         []
                       end
    @paragraphs = paginate(@paragraphs)
    @paragraphs = reorder(@paragraphs)
  end
end

#newObject



82
83
84
85
86
87
88
89
90
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 82

def new
  enforce_permission_to :create, :paragraph
  @step = :step_1
  if paragraph_draft.present?
    redirect_to edit_draft_paragraph_path(paragraph_draft, component_id: paragraph_draft.component.id, question_slug: paragraph_draft.component.participatory_space.slug)
  else
    @form = form(ParagraphWizardCreateStepForm).from_params(body: translated_paragraph_body_template)
  end
end

#overviewObject

Raises:

  • (ActionController::RoutingError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 64

def overview
  @paragraphs = Decidim::EnhancedTextwork::Paragraph
               .where(component: current_component)
               .published
               .not_hidden
               .only_amendables
               .includes(:category, :scope)
               .order(position: :asc)

  @amendments = @paragraph.amendments.order(created_at: :desc)

  paragraph = @paragraphs.find(params[:id])

  raise ActionController::RoutingError, "Not Found" if paragraph.blank? || !can_show_paragraph?

  render "decidim/enhanced_textwork/paragraphs/participatory_texts/participatory_text", locals: { active_paragraph: paragraph }
end

#previewObject



132
133
134
135
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 132

def preview
  @step = :step_4
  @form = form(ParagraphForm).from_model(@paragraph)
end

#publishObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 137

def publish
  @step = :step_4
  PublishParagraph.call(@paragraph, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("paragraphs.publish.success", scope: "decidim")
      redirect_to paragraph_path(@paragraph)
    end

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

#showObject

Raises:

  • (ActionController::RoutingError)


60
61
62
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 60

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

#updateObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 195

def update
  enforce_permission_to :edit, :paragraph, paragraph: @paragraph

  @form = form_paragraph_params
  UpdateParagraph.call(@form, current_user, @paragraph) do
    on(:ok) do |paragraph|
      flash[:notice] = I18n.t("paragraphs.update.success", scope: "decidim")
      redirect_to Decidim::ResourceLocatorPresenter.new(paragraph).path
    end

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

#update_draftObject



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

def update_draft
  @step = :step_1
  enforce_permission_to :edit, :paragraph, paragraph: @paragraph

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

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

#withdrawObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/controllers/decidim/enhanced_textwork/paragraphs_controller.rb', line 212

def withdraw
  enforce_permission_to :withdraw, :paragraph, paragraph: @paragraph

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