From a763f9b81f2178045562522b5a3b00b9fb79b7d6 Mon Sep 17 00:00:00 2001 From: briri Date: Mon, 24 Jun 2024 10:08:11 -0700 Subject: [PATCH] final touches to get templates and public plans served from active storage rather than rendered on the fly --- .../org_admin/templates_controller.rb | 7 ++++++ app/controllers/plan_exports_controller.rb | 2 +- app/jobs/pdf_publisher_job.rb | 11 +++++++-- app/models/template.rb | 2 -- .../template_exports/template_export.pdf.erb | 23 +++++++++++-------- lib/tasks/v5.rake | 2 +- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/controllers/org_admin/templates_controller.rb b/app/controllers/org_admin/templates_controller.rb index 421a0a908e..0fc94f0111 100644 --- a/app/controllers/org_admin/templates_controller.rb +++ b/app/controllers/org_admin/templates_controller.rb @@ -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)}.") diff --git a/app/controllers/plan_exports_controller.rb b/app/controllers/plan_exports_controller.rb index f6d9cbba4d..0859f26b26 100644 --- a/app/controllers/plan_exports_controller.rb +++ b/app/controllers/plan_exports_controller.rb @@ -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 @@ -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? diff --git a/app/jobs/pdf_publisher_job.rb b/app/jobs/pdf_publisher_job.rb index 50b7957dc2..a1927f3153 100644 --- a/app/jobs/pdf_publisher_job.rb +++ b/app/jobs/pdf_publisher_job.rb @@ -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: { @@ -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? diff --git a/app/models/template.rb b/app/models/template.rb index c81ccde873..86286b1a63 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -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 diff --git a/app/views/template_exports/template_export.pdf.erb b/app/views/template_exports/template_export.pdf.erb index 55fcbe0763..3bb9bdc44b 100644 --- a/app/views/template_exports/template_export.pdf.erb +++ b/app/views/template_exports/template_export.pdf.erb @@ -1,29 +1,32 @@ +<% +formatting = formatting || @formatting +template = template || @template +margin = formatting.fetch(:margin, {}) %> - <%= @template.title %> + <%= template.title %> - <% @template.phases.each do |phase| %> + <% template.phases.each do |phase| %>
-

<%= "#{@template.org.name}: #{@template.title}" %><%= @template.phases.length > 1 ? " - #{phase.title}" : "" %>

+

<%= "#{template.org.name}: #{template.title}" %><%= template.phases.length > 1 ? " - #{phase.title}" : "" %>

<% phase.sections.order(:number).each do |section| %>

<%= section.title %>

<% section.questions.each do |question| %> diff --git a/lib/tasks/v5.rake b/lib/tasks/v5.rake index 4d47fac998..40b5a76284 100644 --- a/lib/tasks/v5.rake +++ b/lib/tasks/v5.rake @@ -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|