-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathpublic_pages_controller.rb
More file actions
104 lines (95 loc) · 3.86 KB
/
public_pages_controller.rb
File metadata and controls
104 lines (95 loc) · 3.86 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true
# Controller for the Public DMPs and Funder Requirements pages
class PublicPagesController < ApplicationController
# GET template_index
# -----------------------------------------------------
# rubocop:disable Metrics/AbcSize
def template_index
@templates_query_params = {
page: paginable_params.fetch(:page, 1),
search: paginable_params.fetch(:search, ''),
sort_field: paginable_params.fetch(:sort_field, 'templates.title'),
sort_direction: paginable_params.fetch(:sort_direction, 'asc')
}
templates = Template.live(Template.families(Org.funder.pluck(:id)).pluck(:family_id))
.publicly_visible.pluck(:id) <<
Template.where(is_default: true).unarchived.published.pluck(:id)
@templates = Template.includes(:org)
.where(id: templates.uniq.flatten)
.unarchived.published
end
# rubocop:enable Metrics/AbcSize
# GET template_export/:id
# -----------------------------------------------------
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def template_export
# only export live templates, id passed is family_id
@template = Template.live(params[:id])
# covers authorization for this action.
# Pundit doesn't support passing objects into scoped policies
unless PublicPagePolicy.new(current_user, @template).template_export?
msg = 'You are not authorized to export that template'
redirect_to public_templates_path, notice: msg and return
# raise Pundit::NotAuthorizedError
end
# now with prefetching (if guidance is added, prefetch annottaions/guidance)
@template = Template.includes(
:org,
phases: {
sections: {
questions: %i[
question_options
question_format
annotations
]
}
}
).find(@template.id)
@formatting = Settings::Template::DEFAULT_SETTINGS[:formatting]
begin
file_name = @template.title.gsub(/[^a-zA-Z\d\s]/, '').tr(' ', '_')
file_name = "#{file_name}_v#{@template.version}"
respond_to do |format|
format.docx do
render docx: 'template_exports/template_export', filename: "#{file_name}.docx"
end
format.pdf do
render pdf: file_name,
template: 'template_exports/template_export',
margin: @formatting[:margin],
footer: {
center: format(_('Template created using the %{application_name} service. Last modified %{date}'),
application_name: ApplicationService.application_name,
date: l(@template.updated_at.to_date, formats: :short)),
font_size: 8,
spacing: (@formatting[:margin][:bottom] / 2) - 4,
right: '[page] of [topage]',
encoding: 'utf8'
}
end
end
rescue ActiveRecord::RecordInvalid
# What scenario is this triggered in? it's common to our export pages
redirect_to public_templates_path,
alert: _('Unable to download the DMP Template at this time.')
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# GET /plans_index
# ------------------------------------------------------------------------------------
def plan_index
@plans = Plan.publicly_visible.includes(:template, roles: { user: :org })
render 'plan_index', locals: {
query_params: {
page: paginable_params.fetch(:page, 1),
search: paginable_params.fetch(:search, ''),
sort_field: paginable_params.fetch(:sort_field, 'plans.updated_at'),
sort_direction: paginable_params.fetch(:sort_direction, 'desc')
}
}
end
private
def paginable_params
params.permit(:page, :search, :sort_field, :sort_direction)
end
end