123456789_123456789_123456789_123456789_123456789_

Class: ActiveStorage::Attached::Changes::CreateOne

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activestorage/lib/active_storage/attached/changes/create_one.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, record, attachable) ⇒ CreateOne

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 10

def initialize(name, record, attachable)
  @name, @record, @attachable = name, record, attachable
  blob.identify_without_saving
end

Instance Attribute Details

#analyze_immediately?Boolean (readonly, private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 123

def analyze_immediately?
  case analyze_option
  when :immediately then true
  when :later, :lazily then false
  when nil then has_immediate_variants? || ActiveStorage.analyze == :immediately
  else
    raise ArgumentError, "Unknown analyze option: #{analyze_option.inspect}. Valid options are :immediately, :later, :lazily."
  end
end

#attachable (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 8

attr_reader :name, :record, :attachable

#has_immediate_variants?Boolean (readonly, private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 141

def has_immediate_variants?
  named_variants.any? { |_name, named_variant| named_variant.process(record) == :immediately }
end

#name (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 8

attr_reader :name, :record, :attachable

#pending_upload?Boolean (readonly, private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 55

def pending_upload?
  case attachable
  when ActiveStorage::Blob, String then false
  else true
  end
end

#record (readonly)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 8

attr_reader :name, :record, :attachable

Instance Method Details

#analyze

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 15

def analyze
  with_local_io { blob.analyze_without_saving unless blob.analyzed? } if analyze_immediately?
end

#analyze_option (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 133

def analyze_option
  reflection&.options&.fetch(:analyze, nil)
end

#attachment

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 19

def attachment
  @attachment ||= find_or_build_attachment
end

#attachment_service_name (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 114

def attachment_service_name
  service_name = record.attachment_reflections[name].options[:service_name]
  if service_name.is_a?(Proc)
    service_name = service_name.call(record)
    Attached::Model.validate_service_configuration(service_name, record.class, name)
  end
  service_name
end

#blob

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 23

def blob
  @blob ||= find_or_build_blob
end

#build_attachment (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 49

def build_attachment
  ActiveStorage::Attachment.new(record: record, name: name, blob: blob).tap do |attachment|
    attachment.pending_upload = pending_upload?
  end
end

#find_attachment (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 43

def find_attachment
  if record.public_send("#{name}_blob") == blob
    record.public_send("#{name}_attachment")
  end
end

#find_or_build_attachment (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 39

def find_or_build_attachment
  find_attachment || build_attachment
end

#find_or_build_blob (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 62

def find_or_build_blob
  case attachable
  when ActiveStorage::Blob
    attachable
  when ActionDispatch::Http::UploadedFile
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable.open,
      filename: attachable.original_filename,
      content_type: attachable.content_type,
      record: record,
      service_name: attachment_service_name
    )
  when Rack::Test::UploadedFile
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable.respond_to?(:open) ? attachable.open : attachable,
      filename: attachable.original_filename,
      content_type: attachable.content_type,
      record: record,
      service_name: attachment_service_name
    )
  when Hash
    ActiveStorage::Blob.build_after_unfurling(
      **attachable.reverse_merge(
        record: record,
        service_name: attachment_service_name
      ).symbolize_keys
    )
  when String
    ActiveStorage::Blob.find_signed!(attachable, record: record)
  when File
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable,
      filename: File.basename(attachable),
      record: record,
      service_name: attachment_service_name
    )
  when Pathname
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable.open,
      filename: File.basename(attachable),
      record: record,
      service_name: attachment_service_name
    )
  else
    raise(
      ArgumentError,
      "Could not find or build blob: expected attachable, " \
        "got #{attachable.inspect}"
    )
  end
end

#named_variants (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 145

def named_variants
  reflection&.named_variants || {}
end

#open_attachable_io (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 149

def open_attachable_io
  case attachable
  when ActionDispatch::Http::UploadedFile
    attachable.open
  when Rack::Test::UploadedFile
    attachable.respond_to?(:open) ? attachable.open : attachable
  when Hash
    attachable.fetch(:io)
  when File
    attachable
  when Pathname
    attachable.open
  when ActiveStorage::Blob, String
    nil
  else
    raise ArgumentError, "Could not upload: expected attachable, got #{attachable.inspect}"
  end
end

#reflection (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 137

def reflection
  record.attachment_reflections[name]
end

#save

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 33

def save
  record.public_send("#{name}_attachment=", attachment)
  record.public_send("#{name}_blob=", blob)
end

#upload

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 27

def upload
  if io = open_attachable_io
    attachment.uploaded(io: io)
  end
end

#with_local_io (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 168

def with_local_io
  io = open_attachable_io if pending_upload? && !blob.local_io

  if io
    blob.local_io = io
    io.rewind if io.respond_to?(:rewind)
  end

  yield if io || blob.local_io
ensure
  blob.local_io = nil if io
end