lib/echo_uploads/tmp_file_writing.rb
def echo_uploads_maybe_write_tmp_file(attr, options)
      if (file = send(attr)).present? and errors[attr].empty?
        # A file has been uploaded. Validation failed, but the file itself was valid.
        # Thus, we must persist a temporary file.
        # 
        # It's possible at this point that the record already has a permanent file.
        # That's fine. We'll now have a permanent and a temporary one. The temporary
        # one will replace the permanent one if and when the user resubmits with
        # valid data.
        
        # Construct an array of EchoUploads::File instances. The array might have only
        # one element.
        if options[:multiple]
          mapped_files = send("mapped_#{attr}") ||
            raise('echo_uploads called with :multiple, but :map option was missing')
          metas = mapped_files.map do |mapped_file|
            ::EchoUploads::File.new(
              owner: nil, temporary: true, expires_at: options[:expires].from_now,
              file: mapped_file
            )
          end
        else
          metas = [::EchoUploads::File.new(
            owner: nil, temporary: true, expires_at: options[:expires].from_now,
            file: send(attr)
          )]
        end
        
        # Persist each file. (There might only be one, though.)
        metas.each do |meta|
          meta.persist! attr, options
        end
        
        # Set the attr_tmp_metadata attribute so the form can remember our records.
        send("#{attr}_tmp_metadata=", metas)
      end
    end
def echo_uploads_maybe_write_tmp_file(attr, options)
      if (file = send(attr)).present? and errors[attr].empty?
        # A file has been uploaded. Validation failed, but the file itself was valid.
        # Thus, we must persist a temporary file.
        # 
        # It's possible at this point that the record already has a permanent file.
        # That's fine. We'll now have a permanent and a temporary one. The temporary
        # one will replace the permanent one if and when the user resubmits with
        # valid data.
        
        # Construct an array of EchoUploads::File instances. The array might have only
        # one element.
        if options[:multiple]
          mapped_files = send("mapped_#{attr}") ||
            raise('echo_uploads called with :multiple, but :map option was missing')
          metas = mapped_files.map do |mapped_file|
            ::EchoUploads::File.new(
              owner: nil, temporary: true, expires_at: options[:expires].from_now,
              file: mapped_file
            )
          end
        else
          metas = [::EchoUploads::File.new(
            owner: nil, temporary: true, expires_at: options[:expires].from_now,
            file: send(attr)
          )]
        end
        
        # Persist each file. (There might only be one, though.)
        metas.each do |meta|
          meta.persist! attr, options
        end
        
        # Set the attr_tmp_metadata attribute so the form can remember our records.
        send("#{attr}_tmp_metadata=", metas)
      end
    end
def echo_uploads_maybe_write_tmp_file ( attr , options ) if ( file = send ( attr ) ) . present? and errors [ attr ] . empty? # A file has been uploaded. Validation failed, but the file itself was valid. # Thus, we must persist a temporary file. #  # It's possible at this point that the record already has a permanent file. # That's fine. We'll now have a permanent and a temporary one. The temporary # one will replace the permanent one if and when the user resubmits with # valid data. # Construct an array of EchoUploads::File instances. The array might have only # one element. if options [ :multiple ] mapped_files = send ( "mapped_#{attr}" ) || raise ( 'echo_uploads called with :multiple, but :map option was missing' ) metas = mapped_files . map do | mapped_file | :: EchoUploads :: File . new ( owner : nil , temporary : true , expires_at : options [ :expires ] . from_now , file : mapped_file ) end else metas = [ :: EchoUploads :: File . new ( owner : nil , temporary : true , expires_at : options [ :expires ] . from_now , file : send ( attr ) ) ] end # Persist each file. (There might only be one, though.) metas . each do | meta | meta . persist! attr , options end # Set the attr_tmp_metadata attribute so the form can remember our records. send ( "#{attr}_tmp_metadata=" , metas ) end end
On a failed attempt to save (typically due to validation errors), save the file
 and metadata. Metadata record will be given the temporary flag.
On a failed attempt to save ( typically due to validation errors ) save the file and metadata . Metadata record will be given the temporary flag .