2017-07-06 05:33:29 -04:00
|
|
|
require "active_storage/blob"
|
|
|
|
require "active_storage/attachment"
|
2017-07-05 10:09:41 -04:00
|
|
|
|
|
|
|
require "action_dispatch/http/upload"
|
|
|
|
require "active_support/core_ext/module/delegation"
|
|
|
|
|
2017-07-06 05:33:29 -04:00
|
|
|
class ActiveStorage::Attached
|
2017-07-05 10:09:41 -04:00
|
|
|
attr_reader :name, :record
|
|
|
|
|
|
|
|
def initialize(name, record)
|
|
|
|
@name, @record = name, record
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def create_blob_from(attachable)
|
|
|
|
case attachable
|
2017-07-06 05:33:29 -04:00
|
|
|
when ActiveStorage::Blob
|
2017-07-05 10:09:41 -04:00
|
|
|
attachable
|
|
|
|
when ActionDispatch::Http::UploadedFile
|
2017-07-06 05:33:29 -04:00
|
|
|
ActiveStorage::Blob.create_after_upload! \
|
2017-07-05 10:09:41 -04:00
|
|
|
io: attachable.open,
|
|
|
|
filename: attachable.original_filename,
|
|
|
|
content_type: attachable.content_type
|
|
|
|
when Hash
|
2017-07-06 05:33:29 -04:00
|
|
|
ActiveStorage::Blob.create_after_upload!(attachable)
|
2017-07-05 10:09:41 -04:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-06 05:33:29 -04:00
|
|
|
require "active_storage/attached/one"
|
|
|
|
require "active_storage/attached/many"
|
|
|
|
require "active_storage/attached/macros"
|