2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-03 17:02:43 -04:00
|
|
|
require "action_dispatch"
|
2017-07-05 10:09:41 -04:00
|
|
|
require "action_dispatch/http/upload"
|
|
|
|
require "active_support/core_ext/module/delegation"
|
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
module ActiveStorage
|
2017-08-15 12:48:19 -04:00
|
|
|
# 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.
|
2017-08-04 19:33:40 -04:00
|
|
|
class Attached
|
2017-08-29 15:34:50 -04:00
|
|
|
attr_reader :name, :record, :dependent
|
2017-07-05 10:09:41 -04:00
|
|
|
|
2017-08-29 15:34:50 -04:00
|
|
|
def initialize(name, record, dependent:)
|
|
|
|
@name, @record, @dependent = name, record, dependent
|
2017-08-04 19:33:40 -04:00
|
|
|
end
|
2017-07-05 10:09:41 -04:00
|
|
|
|
2017-08-04 19:33:40 -04:00
|
|
|
private
|
|
|
|
def create_blob_from(attachable)
|
|
|
|
case attachable
|
|
|
|
when ActiveStorage::Blob
|
|
|
|
attachable
|
2017-08-11 10:30:41 -04:00
|
|
|
when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
|
2017-08-04 19:33:40 -04:00
|
|
|
ActiveStorage::Blob.create_after_upload! \
|
|
|
|
io: attachable.open,
|
|
|
|
filename: attachable.original_filename,
|
|
|
|
content_type: attachable.content_type
|
|
|
|
when Hash
|
|
|
|
ActiveStorage::Blob.create_after_upload!(attachable)
|
|
|
|
when String
|
|
|
|
ActiveStorage::Blob.find_signed(attachable)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2017-07-05 10:09:41 -04:00
|
|
|
end
|
2017-08-04 19:33:40 -04:00
|
|
|
end
|
2017-07-05 10:09:41 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 05:33:29 -04:00
|
|
|
require "active_storage/attached/one"
|
|
|
|
require "active_storage/attached/many"
|
|
|
|
require "active_storage/attached/macros"
|