| ❗ IN DEVELOPMENT |
|---|
| This is still in development and should not be used in any production environment! |
This is an idea after ActiveStorage from rails. We wanted to use separate models and tables for saving attachments.
How to use my plugin.
An attachment table is owned by the application, so it may carry columns of its own beyond the ones StorageTables needs. The attachment class declares which of them may be written this way; nothing is writable until it does:
class StorageTables::InvoiceDocument < StorageTables::Attachment
self.permitted_attachment_attributes = %i[template_name]
endThen set them by passing an :attachment_attributes hash alongside the attachable:
invoice.document = { io:, filename: "1234.pdf", attachment_attributes: { template_name: "gds801" } }
invoice.save!
invoice.document_storage_attachment.template_name # => "gds801"Anything the class has not permitted is dropped rather than rejected. String and symbol keys both work.
The columns StorageTables derives itself — record, record_id, blob, blob_key, checksum
and filename — can never be set this way. They are stripped from the allow list on assignment,
so listing one has no effect:
self.permitted_attachment_attributes = [:template_name, :filename]
permitted_attachment_attributes # => [:template_name]It works through attach as well, and applies to an attachment that is already persisted, so
re-assigning the same blob with a changed value updates the column. The key is stripped before the
rest of the hash is handed to Blob.create_and_upload!, and only a Hash attachable can carry it —
the setter generated by has_one_stored is reached through assign_attributes, which passes a
single argument.
Add this line to your application's Gemfile:
gem "storage_tables"And then execute:
$ bundleOr install it yourself as:
$ gem install storage_tablesContribution directions go here.
The gem is available as open source under the terms of the MIT License.