module Shrine::Attacher::InstanceMethods

Direct including types

Defined in:

shrine/attacher.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(file : Shrine::UploadedFile | Nil = nil, cache_key : String = "cache", store_key : String = "store") #

Initializes the attached file, temporary and permanent storage.


[View source]

Instance Method Detail

def assign(value : IO | Nil, **options) #

Calls #attach_cached, but skips if value is an empty string (this is useful when the uploaded file comes from form fields). Forwards any additional options to #attach_cached.

attacher.assign(File.open(...))
attacher.assign(File.open(...), metadata: { "foo" => "bar" })

# ignores the assignment when a blank string is given
attacher.assign("")

[View source]
def attach(io : IO | Shrine::UploadedFile | Nil, storage = store_key, **options) : UploadedFile | Nil #

Uploads given IO object and changes the uploaded file.

# uploads the file to permanent storage
attacher.attach(io)

# uploads the file to specified storage
attacher.attach(io, storage: :other_store)

# forwards additional options to the uploader
attacher.attach(io, upload_options: { "x-amz-acl": "public-read" }, metadata: { "foo" => "bar" })

# removes the attachment
attacher.attach(nil)

[View source]
def attach_cached(value : IO | UploadedFile | Nil, **options) #

Sets an existing cached file, or uploads an IO object to temporary storage and sets it via #attach. Forwards any additional options to #attach.

# upload file to temporary storage and set the uploaded file.
attacher.attach_cached(File.open(...))

# foward additional options to the uploader
attacher.attach_cached(File.open(...), metadata: { "foo" => "bar" })

# sets an existing cached file from JSON data
attacher.attach_cached("{\"id\":\"...\",\"storage\":\"cache\",\"metadata\ ":{...}}")

# sets an existing cached file from Hash data
attacher.attach_cached({ "id" => "...", "storage" => "cache", "metadata" => {} })

[View source]
def attach_cached(value : String | Hash(String, String | UploadedFile::MetadataType), **options) #

[View source]
def attached? #

Returns whether a file is attached.

attacher.attach(io)
attacher.attached? #=> true

attacher.attach(nil)
attacher.attached? #=> false

[View source]
def cache_key #

[View source]
def cached?(file = self.file) #

Returns whether the file is uploaded to temporary storage.

attacher.cached?       # checks current file
attacher.cached?(file) # checks given file

[View source]
def change(file : Shrine::UploadedFile | Nil) : Shrine::UploadedFile | Nil #

Sets the uploaded file with dirty tracking, and runs validations.

attacher.change(uploaded_file)
attacher.file #=> #<Shrine::UploadedFile>
attacher.changed? #=> true

[View source]
def changed? #

Returns whether the attachment has changed.

attacher.changed? #=> false
attacher.attach(file)
attacher.changed? #=> true

TODO This will work incorrect if @previous is nil


[View source]
def context #

Returns options that are automatically forwarded to the uploader. Can be modified with additional data.


[View source]
def data #

Generates serializable data for the attachment.

attacher.data #=> { "id" => "...", "storage" => "...", "metadata": { ... } }

[View source]
def destroy #

Destroys the attachment.

attacher.file.exists? #=> true
attacher.destroy
attacher.file.exists? #=> false

[View source]
def destroy_attached #

Destroys the attached file if it exists and is uploaded to permanent storage.

attacher.file.exists? #=> true
attacher.destroy_attached
attacher.file.exists? #=> false

[View source]
def destroy_previous #

If a new file was attached, deletes previously attached file if any.

previous_file = attacher.file
attacher.attach(file)
attacher.destroy_previous
previous_file.exists? #=> false

[View source]
def file : Shrine::UploadedFile | Nil #

Returns the attached uploaded file.


[View source]
def file! #

Returns attached file or raises an exception if no file is attached.


[View source]
def file=(file : Shrine::UploadedFile | Nil) #

Returns the attached uploaded file.


[View source]
def finalize #

Deletes any previous file and promotes newly attached cached file. It also clears any dirty tracking.

# promoting cached file
attacher.assign(io)
attacher.cached? #=> true
attacher.finalize
attacher.stored?

# deleting previous file
previous_file = attacher.file
previous_file.exists? #=> true
attacher.assign(io)
attacher.finalize
previous_file.exists? #=> false

# clearing dirty tracking
attacher.assign(io)
attacher.changed? #=> true
attacher.finalize
attacher.changed? #=> false

[View source]
def get #

Returns the attached file.

# when a file is attached
attacher.get #=> #<Shrine::UploadedFile>

# when no file is attached
attacher.get #=> nil

[View source]
def load_data(data : Hash(String, String | UploadedFile::MetadataType)) #

Loads the uploaded file from data generated by Attacher#data.

attacher.file #=> nil
attacher.load_data({ "id" => "...", "storage" => "...", "metadata" => { ... } })
attacher.file #=> #<Shrine::UploadedFile>

[View source]
def load_data(data : Nil) #

[View source]
def load_data(**data) #

[View source]
def promote(storage = store_key, **options) : Shrine::UploadedFile | Nil #

Uploads current file to permanent storage and sets the stored file.

attacher.cached? #=> true
attacher.promote
attacher.stored? #=> true

[View source]
def promote_cached(**options) #

If a new cached file has been attached, uploads it to permanent storage. Any additional options are forwarded to #promote.

attacher.assign(io)
attacher.cached? #=> true
attacher.promote_cached
attacher.stored? #=> true

[View source]
def set(file : Shrine::UploadedFile | Nil) : Shrine::UploadedFile | Nil #

Sets the uploaded file.

attacher.set(uploaded_file)
attacher.file #=> #<Shrine::UploadedFile>
attacher.changed? #=> false

[View source]
def shrine_class #

Returns the Shrine class that this attacher's class is namespaced under.


[View source]
def store_key #

[View source]
def stored?(file = self.file) #

Returns whether the file is uploaded to permanent storage.

attacher.stored?       # checks current file
attacher.stored?(file) # checks given file

[View source]
def upload(io : IO | Shrine::UploadedFile, storage = store_key, **options) : Shrine::UploadedFile #

Delegates to Shrine.upload, passing the #context.

# upload file to specified storage
attacher.upload(io, "store") #=> #<Shrine::UploadedFile>

# pass additional options for the uploader
attacher.upload(io, "store", metadata: { "foo" => "bar" })

[View source]
def uploaded_file(value) #

Converts JSON or Hash data into a Shrine::UploadedFile object.

attacher.uploaded_file("{\"id\":\"...\",\"storage\":\"...\",\"metadata\":{...}}")
#=> #<Shrine::UploadedFile ...>

attacher.uploaded_file({ "id" => "...", "storage" => "...", "metadata" => {} })
#=> #<Shrine::UploadedFile ...>

[View source]
def url(**options) #

If a file is attached, returns the uploaded file URL, otherwise returns nil. Any options are forwarded to the storage.

attacher.file = file
attacher.url #=> "https://..."

attacher.file = nil
attacher.url #=> nil

[View source]