Class: Plutonium::Attachments::Resolved

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/attachments.rb

Overview

A uniform view over a resolved attachment so the review display + the uppy preview don't care whether the source is an ActiveStorage Blob (filename/content_type/representable?) or a Shrine UploadedFile (original_filename/mime_type, none of the AS-only methods). Exposes exactly what those components call.

Instance Method Summary collapse

Constructor Details

#initialize(source, token) ⇒ Resolved

Returns a new instance of Resolved.

Parameters:

  • source

    the backend object (AS Blob or Shrine UploadedFile).

  • token (String)

    the ORIGINAL staged token — what the hidden preview field re-posts to preserve the upload across a Back/re-submit, and what execute assigns to the model attachment.



208
209
210
211
# File 'lib/plutonium/attachments.rb', line 208

def initialize(source, token)
  @source = source
  @token = token
end

Instance Method Details

#content_typeObject



222
# File 'lib/plutonium/attachments.rb', line 222

def content_type = @source.try(:content_type) || @source.try(:mime_type)

#downloadObject

The bytes, as a String.

Routed through #open rather than the backends' own download, because that is one of the names they disagree on: ActiveStorage's returns the bytes, Shrine's returns a Tempfile. Papering over exactly that kind of divergence is what this class is for, so a caller can read a staged file without knowing which backend minted its token. Explicit receiver: a bare open here is indistinguishable from Kernel#open to a reader (and to the linter), which is a very different and much more dangerous method.



249
# File 'lib/plutonium/attachments.rb', line 249

def download = self.open(&:read)

#extensionObject



226
# File 'lib/plutonium/attachments.rb', line 226

def extension = @source.try(:extension).presence || File.extname(filename).delete(".").presence

#filenameObject



220
# File 'lib/plutonium/attachments.rb', line 220

def filename = (@source.try(:filename) || @source.try(:original_filename)).to_s

#open {|an| ... } ⇒ Object

Yields the attachment as an open file, cleaned up afterwards.

#url is enough for a wizard, which only ever renders a staged attachment. Work that RUNS on one — an import parsing the rows, a job promoting the file onto a model — has to read it, and in a job there is no request to build a URL against anyway.

Yield Parameters:

  • an (File, Tempfile)

    IO positioned at the start

Returns:

  • the block's value



237
# File 'lib/plutonium/attachments.rb', line 237

def open(&) = @source.open(&)

#present?Boolean

Returns:

  • (Boolean)


251
# File 'lib/plutonium/attachments.rb', line 251

def present? = true

#representable?Boolean

Returns:

  • (Boolean)


224
# File 'lib/plutonium/attachments.rb', line 224

def representable? = @source.try(:representable?) || content_type.to_s.start_with?("image/")

#signed_idObject

The re-postable token, surfaced under the name the uppy input reads.



218
# File 'lib/plutonium/attachments.rb', line 218

def signed_id = @token

#url(*args) ⇒ Object

url is lazy (called at render, inside a request, where AS url options exist) — never eager at resolve time.



215
# File 'lib/plutonium/attachments.rb', line 215

def url(*args) = @source.url(*args)