Module: Lockbox::ActiveStorageExtensions::AttachedMany

Defined in:
lib/lockbox/active_storage_extensions.rb

Instance Method Summary collapse

Instance Method Details

#attach(*attachables) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/lockbox/active_storage_extensions.rb', line 69

def attach(*attachables)
  if encrypted?
    attachables =
      attachables.flatten.collect do |attachable|
        encrypt_attachable(attachable)
      end
  end

  super(attachables)
end

#rotate_encryption!Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lockbox/active_storage_extensions.rb', line 80

def rotate_encryption!
  raise "Not encrypted" unless encrypted?

  # must call to_a - do not change
  previous_attachments = attachments.to_a

  attachables =
    previous_attachments.map do |attachment|
      rebuild_attachable(attachment)
    end

  ActiveStorage::Attachment.transaction do
    attach(attachables)
    previous_attachments.each(&:purge)
  end

  attachments.reload

  true
end