-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdocuments_controller.rb
76 lines (64 loc) · 2.09 KB
/
documents_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
class DocumentsController < ApplicationController
def index
if filter_params[:filters].empty?
redirect_to documents_path(organisation: current_user.organisation_content_id)
return
end
filter = EditionFilter.new(filter_params)
@editions = filter.editions
@filter_params = filter.filter_params
@sort = filter.sort
end
def edit
@edition = Edition.find_current(document: params[:document])
@revision = @edition.revision
end
def show
@edition = Edition.find_current(document: params[:document])
end
def confirm_delete_draft
edition = Edition.find_current(document: params[:document])
redirect_to document_path(edition.document), confirmation: "documents/show/delete_draft"
end
def destroy
result = Documents::DestroyInteractor.call(params: params, user: current_user)
if result.api_error
redirect_to document_path(params[:document]),
alert_with_description: t("documents.show.flashes.delete_draft_error")
else
redirect_to documents_path
end
end
def update
result = Documents::UpdateInteractor.call(params: params, user: current_user)
edition, revision, issues, = result.to_h.values_at(:edition, :revision, :issues)
if issues
flash.now["alert_with_items"] = {
"title" => I18n.t!("documents.edit.flashes.requirements"),
"items" => issues.items,
}
render :edit,
assigns: { edition: edition, revision: revision },
status: :unprocessable_entity
elsif params[:submit] == "add_contact"
redirect_to search_contacts_path(edition.document)
else
redirect_to edition.document
end
end
def generate_path
edition = Edition.find_current(document: params[:document])
base_path = PathGeneratorService.new.path(edition.document, params[:title])
render plain: base_path
end
private
def filter_params
{
filters: params.slice(:title_or_url, :document_type, :status, :organisation).permit!,
sort: params[:sort],
page: params[:page],
per_page: 50,
}
end
end