Migrate, e.g. organisation.logo (Carrierwave "LogoUploader") to organisation.logo2 (ActiveStorage)
require 'migrate_attachment'
migrate_attachment!(
klass: Organisation,
attachment_attribute: :logo,
carrierwave_uploader: LogoUploader,
active_storage_column: :logo2
)
Checklist:
- Use with caution! only tested with file adapters, not Cloud storage yet.
- In this example we migrate from logo to logo2 so we can replace all occurences in the codebase. If you are confident, you could also migrate to the same column name (logo -> logo). But make sure to replace all logo.url(...) stuff etc.
- Only works if you not made any funky things with Carrierwave path (e.g. base path on parts of model methods). Add the missing methods to the meta programming part, as well as a vanilla table with "normal" id column.
- Does not migrate any versions. Try to mimick your Carrierwave versions like:
mount_uploader :logo, LogoUploader
...
version :medium do
process resize_to_fit: [200, 1500]
end
# becomes:
has_one_attached :logo2
def logo_medium
logo2.variant(
combine_options: {
gravity: "center",
resize: "200x1500>",
crop: "200x1500+0+0"
})
end
- programatically setting blobs + nil does not work. Use:
organisation.logo2.attach(io: tempfile, filename: filename)
, instead oforganisation.logo = tempfile
organisation.logo2.detach
instead oforganisation.logo2 = nil
- referencing:
image_tag organisation.logo2
- if need url e.g. for serializer or inside pdf scripts etc, one need to use the Rails url helper:
- blob:
Rails.application.routes.url_helpers.rails_blob_path(organisation.logo2, only_path: true)
- variant:
Rails.application.routes.url_helpers.rails_representation_path(organisation.logo_medium, only_path: true)
- blob:
- AS does not have a
remote_ATTACHMENT
that downloads automatically from remote server - grep the codebase for
organisation.logo.url
andorganisation.logo.path
,organisation.logo.versions
- If you need to access the blob for local processing, use
AS::Downloader
config in accompanied file (organisation.logo2.open { |tempfile| .... }
), if on Rails 5.2.0.
Has it been tested with AWS at this point?