From 318dec4f528be7a2a625168693ad418d60ec5d6b Mon Sep 17 00:00:00 2001 From: Thiago Youssef Date: Tue, 28 Jun 2022 10:24:35 -0300 Subject: [PATCH 1/6] Download pdf with domain data --- app/controllers/admin/domains_controller.rb | 7 +- app/models/domain.rb | 6 + app/views/admin/domains/show.html.erb | 5 +- app/views/domain/pdf.haml | 315 ++++++++++++++++++++ config/routes.rb | 1 + 5 files changed, 331 insertions(+), 3 deletions(-) create mode 100644 app/views/domain/pdf.haml diff --git a/app/controllers/admin/domains_controller.rb b/app/controllers/admin/domains_controller.rb index 707b40654..0b3a8b785 100644 --- a/app/controllers/admin/domains_controller.rb +++ b/app/controllers/admin/domains_controller.rb @@ -2,7 +2,7 @@ module Admin class DomainsController < BaseController DEFAULT_VERSIONS_PER_PAGE = 10 - before_action :set_domain, only: %i[show edit update keep] + before_action :set_domain, only: %i[show edit update download keep] authorize_resource # rubocop:disable Metrics/MethodLength @@ -65,6 +65,11 @@ module Admin .per(DEFAULT_VERSIONS_PER_PAGE) end + def download + filename = "#{@domain.name}.pdf" + send_data @domain.as_pdf, filename: filename + end + def keep @domain.keep redirect_to edit_admin_domain_url(@domain), notice: t('.kept') diff --git a/app/models/domain.rb b/app/models/domain.rb index 68c5bc611..193fd96fd 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -740,6 +740,12 @@ class Domain < ApplicationRecord ] end + def as_pdf + domain_html = ApplicationController.render(template: 'domain/pdf', assigns: { domain: self }) + generator = PDFKit.new(domain_html) + generator.to_pdf + end + def registrant_name return registrant.name if registrant diff --git a/app/views/admin/domains/show.html.erb b/app/views/admin/domains/show.html.erb index 2b2835429..7fde9f7b1 100644 --- a/app/views/admin/domains/show.html.erb +++ b/app/views/admin/domains/show.html.erb @@ -6,11 +6,12 @@