lib/lockbox/active_storage_extensions.rb in lockbox-0.2.3 vs lib/lockbox/active_storage_extensions.rb in lockbox-0.2.4

- old
+ new

@@ -8,39 +8,15 @@ protected def encrypted? # could use record_type directly # but record should already be loaded most of the time - !Utils.encrypted_options(record, name).nil? + Utils.encrypted?(record, name) end def encrypt_attachable(attachable) - options = Utils.encrypted_options(record, name) - box = Utils.build_box(record, options, record.class.table_name, name) - - case attachable - when ActiveStorage::Blob - raise NotImplementedError, "Not supported" - when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile - attachable = { - io: StringIO.new(box.encrypt(attachable.read)), - filename: attachable.original_filename, - content_type: attachable.content_type - } - when Hash - attachable = { - io: StringIO.new(box.encrypt(attachable[:io].read)), - filename: attachable[:filename], - content_type: attachable[:content_type] - } - when String - raise NotImplementedError, "Not supported" - else - nil - end - - attachable + Utils.encrypt_attachable(record, name, attachable) end def rebuild_attachable(attachment) { io: StringIO.new(attachment.download), @@ -49,13 +25,15 @@ } end end module AttachedOne - def attach(attachable) - attachable = encrypt_attachable(attachable) if encrypted? - super(attachable) + if ActiveStorage::VERSION::MAJOR < 6 + def attach(attachable) + attachable = encrypt_attachable(attachable) if encrypted? + super(attachable) + end end def rotate_encryption! raise "Not encrypted" unless encrypted? @@ -64,19 +42,21 @@ true end end module AttachedMany - def attach(*attachables) - if encrypted? - attachables = - attachables.flatten.collect do |attachable| - encrypt_attachable(attachable) - end - end + if ActiveStorage::VERSION::MAJOR < 6 + def attach(*attachables) + if encrypted? + attachables = + attachables.flatten.collect do |attachable| + encrypt_attachable(attachable) + end + end - super(attachables) + super(attachables) + end end def rotate_encryption! raise "Not encrypted" unless encrypted? @@ -94,9 +74,17 @@ end attachments.reload true + end + end + + module CreateOne + def initialize(name, record, attachable) + # this won't encrypt existing blobs + attachable = Lockbox::Utils.encrypt_attachable(record, name, attachable) if Lockbox::Utils.encrypted?(record, name) && !attachable.is_a?(ActiveStorage::Blob) + super(name, record, attachable) end end module Attachment extend ActiveSupport::Concern