Skip to content

Commit

Permalink
final touches to get templates and public plans served from active st…
Browse files Browse the repository at this point in the history
…orage rather than rendered on the fly
  • Loading branch information
briri committed Jun 24, 2024
1 parent eb35d5f commit a763f9b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
7 changes: 7 additions & 0 deletions app/controllers/org_admin/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ def publish
publishable, errors = template.publishability
if publishable
if template.publish!
if template.publicly_visible?
# Render the template and place it into ActiveStorage
template.publisher_job_status = 'enqueued'
template.save(touch: false)
PdfPublisherJob.set(wait: 10.seconds).perform_later(obj: template)
end

flash.now[:notice] = _("Your #{template_type(template)} has been published and is now available to users.")
else
flash.now[:alert] = _("Unable to publish your #{template_type(template)}.")
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/plan_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def show
@formatting = export_params[:formatting]
@formatting = @plan.settings(:export)&.formatting if @formatting.nil?
@public_plan = false
@from_public_plans_page = export_params[:pub].to_s.downcase.strip == 'true'
elsif publicly_authorized?
skip_authorization
@show_coversheet = true
Expand All @@ -57,6 +56,7 @@ def show
raise Pundit::NotAuthorizedError, _('are not authorized to view that plan')
end

@from_public_plans_page = export_params[:pub].to_s.downcase.strip == 'true'
@hash = @plan.as_pdf(current_user, @show_coversheet)
@formatting = export_params[:formatting] || @plan.settings(:export).formatting
if params.key?(:phase_id) && params[:phase_id].length.positive?
Expand Down
11 changes: 9 additions & 2 deletions app/jobs/pdf_publisher_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ def perform(obj:)
file_name = Zaru.sanitize!(obj.metadata['dmp']['title']).strip.gsub(/\s+/, '_')[0, 100]
_process_narrative_file(obj: obj, file_name: file_name, file: pdf)
elsif obj.is_a?(Template)
@formatting = {
font_face: 'Tinos, serif',
font_size: '11',
margin: { top: '20', right: '20', bottom: '20', left: '20' }
}
@template = obj
file_name = @template.title.gsub(/[^a-zA-Z\d\s]/, '').tr(' ', '_')
file_name = "#{file_name}_v#{@template.version}"
html = render_to_string(template: '/template_exports/template_export')
ac = ApplicationController.new # ActionController::Base.new
html = ac.render_to_string(template: '/template_exports/template_export', layout: false,
locals: { template: @template, formatting: @formatting})

grover_options = {
margin: {
Expand Down Expand Up @@ -88,7 +95,7 @@ def _process_narrative_file(obj:, file_name:, file:)

# Publish the PDF to local ActiveStorage
def _publish_locally(obj:, pdf_file_path:, pdf_file_name:)
key = obj.is_a(Template) ? 'templates' : 'narratives'
key = obj.is_a?(Template) ? 'templates' : 'narratives'

# Get rid of the existing one (if applicable)
obj.narrative.purge if obj.narrative.attached?
Expand Down
2 changes: 0 additions & 2 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,10 @@ def upgrade_customization!

def publish
update(published: true)
publish_narrative! if publicly_visible?
end

def publish!
update!(published: true)
publish_narrative! if publicly_visible?
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
Expand Down
23 changes: 13 additions & 10 deletions app/views/template_exports/template_export.pdf.erb
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
<%
formatting = formatting || @formatting
template = template || @template
margin = formatting.fetch(:margin, {}) %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>
<%= @template.title %>
<%= template.title %>
</title>
<style>
html {
font-family: <%= @formatting[:font_face].tr('"', '') -%>;
font-size: <%= @formatting[:font_size] -%>pt;
margin: <%= @formatting[:margin][:top] %>px <%= @formatting[:margin][:right] %>px <%= @formatting[:margin][:bottom] %>px <%= @formatting[:margin][:left] %>px; */
font-family: <%= formatting.fetch(:font_face, 'Tinos, serif').tr('"', '') -%>;
font-size: <%= formatting.fetch(:font_size, '11') -%>pt;
margin: <%= margin.fetch(:top, '76') %>px <%= margin.fetch(:right, '76') %>px <%= margin.fetch(:bottom, '76') %>px <%= margin.fetch(:left, '76') %>px;
}
h1 {
font-size: <%= @formatting[:font_size] + 3 -%>pt;
font-size: <%= formatting.fetch(:font_size, 11).to_i + 3 -%>pt;
font-face:bold;
padding: 0;
}
h2 {
font-size: <%= @formatting[:font_size] + 2 -%>pt;
font-size: <%= formatting.fetch(:font_size, 11).to_i + 2 -%>pt;
font-face:bold;
padding: 0;
margin: 1em 0 0 0;
}
h3 {
font-size: <%= @formatting[:font_size] + 1 -%>pt;
font-size: <%= formatting.fetch(:font_size, 11).to_i + 1 -%>pt;
font-face:bold;
padding: 0;
margin: 1em 0 0 0;
Expand Down Expand Up @@ -58,9 +61,9 @@
</style>
</head>
<body>
<% @template.phases.each do |phase| %>
<% template.phases.each do |phase| %>
<div style="page-break-before:always;"></div> <!-- Page break before each phase -->
<h1><%= "#{@template.org.name}: #{@template.title}" %><%= @template.phases.length > 1 ? " - #{phase.title}" : "" %></h1>
<h1><%= "#{template.org.name}: #{template.title}" %><%= template.phases.length > 1 ? " - #{phase.title}" : "" %></h1>
<% phase.sections.order(:number).each do |section| %>
<h2><%= section.title %></h2>
<% section.questions.each do |question| %>
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/v5.rake
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ namespace :v5 do
.publicly_visible.pluck(:id) <<
Template.where(is_default: true).unarchived.published.pluck(:id)
templates = Template.includes(:org)
.where(id: templates.uniq.flatten)
.where(id: template_ids.uniq.flatten)
.unarchived.published

templates.each do |tmplt|
Expand Down

0 comments on commit a763f9b

Please sign in to comment.