aboutsummaryrefslogblamecommitdiffstats
path: root/lib/active_storage/attached.rb
blob: 7475c38999b51fb46ab6308010e583a7d30195a4 (plain) (tree)
1
2
3
4
5
6
7

                                   



                                                   
                             








                                    
                              

                                             
                                                  



                                                 
                                                            





           


                                        
require "active_storage/blob"
require "active_storage/attachment"

require "action_dispatch/http/upload"
require "active_support/core_ext/module/delegation"

class ActiveStorage::Attached
  attr_reader :name, :record

  def initialize(name, record)
    @name, @record = name, record
  end

  private
    def create_blob_from(attachable)
      case attachable
      when ActiveStorage::Blob
        attachable
      when ActionDispatch::Http::UploadedFile
        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)
      else
        nil
      end
    end
end

require "active_storage/attached/one"
require "active_storage/attached/many"
require "active_storage/attached/macros"