AttestationTemplate: store logo and signature with uuid

This commit is contained in:
Simon Lehericey 2017-10-19 15:07:18 +02:00
parent 475675f75d
commit 3dce5b9b5e
7 changed files with 68 additions and 6 deletions

View file

@ -3,8 +3,8 @@ class AttestationTemplate < ApplicationRecord
belongs_to :procedure
mount_uploader :logo, AttestationTemplateImageUploader
mount_uploader :signature, AttestationTemplateImageUploader
mount_uploader :logo, AttestationTemplateLogoUploader
mount_uploader :signature, AttestationTemplateSignatureUploader
validate :logo_signature_file_size
validates :footer, length: { maximum: 190 }

View file

@ -1,4 +1,4 @@
class AttestationTemplateImageUploader < BaseUploader
class AttestationTemplateLogoUploader < BaseUploader
def root
File.join(Rails.root, 'public')
end
@ -23,4 +23,16 @@ class AttestationTemplateImageUploader < BaseUploader
def extension_white_list
%w(jpg jpeg png)
end
def filename
if file.present?
"attestation-template-logo-#{secure_token}.#{file.extension.downcase}"
end
end
private
def secure_token
model.logo_secure_token ||= SecureRandom.uuid
end
end

View file

@ -0,0 +1,38 @@
class AttestationTemplateSignatureUploader < BaseUploader
def root
File.join(Rails.root, 'public')
end
# Choose what kind of storage to use for this uploader:
if Features.remote_storage
storage :fog
else
storage :file
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
unless Features.remote_storage
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg png)
end
def filename
if file.present?
"attestation-template-signature-#{secure_token}.#{file.extension.downcase}"
end
end
private
def secure_token
model.signature_secure_token ||= SecureRandom.uuid
end
end

View file

@ -0,0 +1,5 @@
class AddLogoSecureTokenColumnToAttestationTemplate < ActiveRecord::Migration[5.0]
def change
add_column :attestation_templates, :logo_secure_token, :string
end
end

View file

@ -0,0 +1,5 @@
class AddSignatureSecureTokenColumnToAttestationTemplate < ActiveRecord::Migration[5.0]
def change
add_column :attestation_templates, :signature_secure_token, :string
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171019085515) do
ActiveRecord::Schema.define(version: 20171019113610) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -81,6 +81,8 @@ ActiveRecord::Schema.define(version: 20171019085515) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "procedure_id"
t.string "logo_secure_token"
t.string "signature_secure_token"
t.index ["procedure_id"], name: "index_attestation_templates_on_procedure_id", unique: true, using: :btree
end

View file

@ -2,8 +2,8 @@ describe AttestationTemplate, type: :model do
describe 'validate' do
let(:logo_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte }
let(:signature_size) { AttestationTemplate::FILE_MAX_SIZE_IN_MB.megabyte }
let(:fake_logo) { double(AttestationTemplateImageUploader, file: double(size: logo_size)) }
let(:fake_signature) { double(AttestationTemplateImageUploader, file: double(size: signature_size)) }
let(:fake_logo) { double(AttestationTemplateLogoUploader, file: double(size: logo_size)) }
let(:fake_signature) { double(AttestationTemplateSignatureUploader, file: double(size: signature_size)) }
let(:attestation_template) { AttestationTemplate.new }
before do