Last active
September 30, 2022 06:22
-
-
Save simran-sawhney/109f6bc99d0c9fdd6fa4b3d04e8aaf51 to your computer and use it in GitHub Desktop.
ActiveStorage-Nested-Directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Originally ActiveStorage stores everything on root folder in s3, but if you want to have directories under nested behaviour, following MONKET PATCH should help. Once this is placed, now you can achieved nested folders using filename itself, so if filename have "/" should automatically help you creating nested folder. | |
# frozen_string_literal: true | |
require "action_dispatch" | |
require "action_dispatch/http/upload" | |
require "active_support/core_ext/module/delegation" | |
module ActiveStorage | |
# Abstract base class for the concrete ActiveStorage::Attached::One and ActiveStorage::Attached::Many | |
# classes that both provide proxy access to the blob association for a record. | |
class Attached | |
attr_reader :name, :record, :dependent | |
def initialize(name, record, dependent:) | |
@name, @record, @dependent = name, record, dependent | |
end | |
private | |
def create_blob_from(attachable) | |
case attachable | |
when ActiveStorage::Blob | |
attachable | |
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile | |
ActiveStorage::Blob.create_after_upload!(io: attachable.open, | |
filename: attachable.original_filename, | |
content_type: attachable.content_type, | |
record: record) | |
when Hash | |
ActiveStorage::Blob.create_after_upload!(attachable.merge(record: record)) | |
when String | |
ActiveStorage::Blob.find_signed(attachable) | |
else | |
nil | |
end | |
end | |
end | |
end | |
require "active_storage/attached/one" | |
require "active_storage/attached/many" | |
require "active_storage/attached/macros" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment