Skip to content

Commit

Permalink
add identifier and file name to reports table
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-mj committed Nov 20, 2024
1 parent b66ba3a commit 4078ebc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
27 changes: 22 additions & 5 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# frozen_string_literal: true

class ReportsController < ApplicationController
authorize_resource
before_action :set_report, only: [:download, :download_by_identifier]
before_action :check_downloadable, only: [:download, :download_by_identifier]

skip_authorization_check only: :download_by_identifier
skip_authorize_resource only: :download_by_identifier
skip_before_action :authenticate_user!, only: :download_by_identifier

active_scaffold :report do |config|
config.list.columns = [:user, :created_at, :expires_at]
config.show.columns = [:user, :created_at, :expires_at]
config.list.columns = [:user, :file_name, :identifier, :created_at, :expires_at]
config.show.columns = [:user, :file_name, :identifier, :created_at, :expires_at]
config.update.columns = [:expires_at]
config.columns[:user].clear_link
config.actions.exclude :create, :delete
Expand All @@ -23,11 +28,23 @@ class ReportsController < ApplicationController
end

def download
report = Report.find(params[:id])
redirect_to download_path(medium_hash: report.carrierwave_file.medium_hash)
redirect_to download_path(medium_hash: @report.carrierwave_file.medium_hash)
end

def download_by_identifier
redirect_to download_path(medium_hash: @report.carrierwave_file.medium_hash)
end

private
def set_report
@report = params[:id] ? Report.find(params[:id]) : Report.find_by_identifier(params[:identifier])
raise ActionController::RoutingError.new("Este documento não foi encontrado.") if @report.nil?
end

def check_downloadable
raise ActionController::RoutingError.new("Este documento expirou.") if cant_download?(@report)
end

def cant_download?(record)
record.carrierwave_file.blank?
end
Expand Down
18 changes: 9 additions & 9 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def signature_footer(pdf, options = {})
pdf.stroke_bounds

if options[:qr_code_signature]
qrcode_url = "#{request.protocol}#{request.host_with_port}/files/#{@qrcode_identifier}"
qrcode_url = "#{request.protocol}#{request.host_with_port}/reports/#{@qrcode_identifier}.pdf"
qrcode_signature_warning = I18n.t("pdf_content.enrollment.footer.qrcode_signature_warning")
signed_at = "#{I18n.t("pdf_content.enrollment.footer.signed_by_qrcode")} #{I18n.l(Time.now, format: :defaultdatetime)} (Horário de Brasília)"
you_can_also_access = I18n.t("pdf_content.enrollment.footer.you_can_also_access")
Expand Down Expand Up @@ -361,13 +361,14 @@ def new_document(name, title, options = {}, &block)

if pdf_config.qr_code_signature && !pdf_config.preview
uploader = PdfUploader.new
uploader.store!({ base64_contents: Base64.encode64(document), filename: "academic_transcript.pdf" })
uploader.file&.file&.update!(medium_hash: @qrcode_identifier)
uploader.store!({ base64_contents: Base64.encode64(document), filename: name })

Report.create!(
expires_at: pdf_config.expiration_in_months.present? ? Date.today + pdf_config.expiration_in_months.months : nil,
user: current_user,
carrierwave_file: uploader.file&.file
carrierwave_file: uploader.file&.file,
file_name: name,
identifier: @qrcode_identifier
)
end

Expand Down Expand Up @@ -497,14 +498,13 @@ def simple_pdf_table(pdf, widths, header, data, options = {}, join_header_and_ta
end

end

def qrcode_signature(pdf, options = {})
@qrcode_identifier ||= generate_qr_code_key + ".pdf"
while CarrierWave::Storage::ActiveRecord::ActiveRecordFile.where(medium_hash: @qrcode_identifier).exists?
@qrcode_identifier = generate_qr_code_key + ".pdf"
@qrcode_identifier ||= generate_qr_code_key
while Report.where(identifier: @qrcode_identifier).exists?
@qrcode_identifier = generate_qr_code_key
end

data = "#{request.protocol}#{request.host_with_port}/files/#{@qrcode_identifier}"
data = "#{request.protocol}#{request.host_with_port}/reports/#{@qrcode_identifier}.pdf"

pdf.print_qr_code(data, extent: options[:size] || 80, align: :center)
end
Expand Down
2 changes: 2 additions & 0 deletions config/locales/reports.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pt-BR:
attributes:
report:
user: "Gerado por"
file_name: "Nome do Arquivo"
identifier: "Identificador"
created_at: "Data de Criação"
expires_at: "Data de Expiração"

Expand Down
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,10 @@
resources :reports do
concerns :active_scaffold
member do
get "download"
get :download
end
collection do
get ":identifier.pdf", to: "reports#download_by_identifier", as: :download_by_identifier
end
end

Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20241119221140_add_identifier_to_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddIdentifierToReports < ActiveRecord::Migration[7.0]
def change
add_column :reports, :identifier, :string
end
end
7 changes: 7 additions & 0 deletions db/migrate/20241119221152_add_file_name_to_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddFileNameToReports < ActiveRecord::Migration[7.0]
def change
add_column :reports, :file_name, :string
end
end
15 changes: 14 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4078ebc

Please sign in to comment.