Class: Plutonium::Attachments::Resolved
- Inherits:
-
Object
- Object
- Plutonium::Attachments::Resolved
- 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
- #content_type ⇒ Object
-
#download ⇒ Object
The bytes, as a String.
- #extension ⇒ Object
- #filename ⇒ Object
-
#initialize(source, token) ⇒ Resolved
constructor
A new instance of Resolved.
-
#open {|an| ... } ⇒ Object
Yields the attachment as an open file, cleaned up afterwards.
- #present? ⇒ Boolean
- #representable? ⇒ Boolean
-
#signed_id ⇒ Object
The re-postable token, surfaced under the name the uppy input reads.
-
#url(*args) ⇒ Object
urlis lazy (called at render, inside a request, where AS url options exist) — never eager at resolve time.
Constructor Details
#initialize(source, token) ⇒ Resolved
Returns a new instance of Resolved.
208 209 210 211 |
# File 'lib/plutonium/attachments.rb', line 208 def initialize(source, token) @source = source @token = token end |
Instance Method Details
#content_type ⇒ Object
222 |
# File 'lib/plutonium/attachments.rb', line 222 def content_type = @source.try(:content_type) || @source.try(:mime_type) |
#download ⇒ Object
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) |
#extension ⇒ Object
226 |
# File 'lib/plutonium/attachments.rb', line 226 def extension = @source.try(:extension).presence || File.extname(filename).delete(".").presence |
#filename ⇒ Object
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.
237 |
# File 'lib/plutonium/attachments.rb', line 237 def open(&) = @source.open(&) |
#present? ⇒ Boolean
251 |
# File 'lib/plutonium/attachments.rb', line 251 def present? = true |
#representable? ⇒ Boolean
224 |
# File 'lib/plutonium/attachments.rb', line 224 def representable? = @source.try(:representable?) || content_type.to_s.start_with?("image/") |
#signed_id ⇒ Object
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) |