module Shrine::Attacher::InstanceMethods
Direct including types
Defined in:
shrine/attacher.crConstructors
-
.new(file : Shrine::UploadedFile | Nil = nil, cache_key : String = "cache", store_key : String = "store")
Initializes the attached file, temporary and permanent storage.
Instance Method Summary
-
#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).
-
#attach(io : IO | Shrine::UploadedFile | Nil, storage = store_key, **options) : UploadedFile | Nil
Uploads given IO object and changes the uploaded file.
-
#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.
- #attach_cached(value : String | Hash(String, String | UploadedFile::MetadataType), **options)
-
#attached?
Returns whether a file is attached.
- #cache_key
-
#cached?(file = self.file)
Returns whether the file is uploaded to temporary storage.
-
#change(file : Shrine::UploadedFile | Nil) : Shrine::UploadedFile | Nil
Sets the uploaded file with dirty tracking, and runs validations.
-
#changed?
Returns whether the attachment has changed.
-
#context
Returns options that are automatically forwarded to the uploader.
-
#data
Generates serializable data for the attachment.
-
#destroy
Destroys the attachment.
-
#destroy_attached
Destroys the attached file if it exists and is uploaded to permanent storage.
-
#destroy_previous
If a new file was attached, deletes previously attached file if any.
-
#file : Shrine::UploadedFile | Nil
Returns the attached uploaded file.
-
#file!
Returns attached file or raises an exception if no file is attached.
-
#file=(file : Shrine::UploadedFile | Nil)
Returns the attached uploaded file.
-
#finalize
Deletes any previous file and promotes newly attached cached file.
-
#get
Returns the attached file.
-
#load_data(data : Hash(String, String | UploadedFile::MetadataType))
Loads the uploaded file from data generated by
Attacher#data
. - #load_data(data : Nil)
- #load_data(**data)
-
#promote(storage = store_key, **options) : Shrine::UploadedFile | Nil
Uploads current file to permanent storage and sets the stored file.
-
#promote_cached(**options)
If a new cached file has been attached, uploads it to permanent storage.
-
#set(file : Shrine::UploadedFile | Nil) : Shrine::UploadedFile | Nil
Sets the uploaded file.
-
#shrine_class
Returns the Shrine class that this attacher's class is namespaced under.
- #store_key
-
#stored?(file = self.file)
Returns whether the file is uploaded to permanent storage.
-
#upload(io : IO | Shrine::UploadedFile, storage = store_key, **options) : Shrine::UploadedFile
Delegates to
Shrine.upload
, passing the #context. -
#uploaded_file(value)
Converts JSON or Hash data into a Shrine::UploadedFile object.
-
#url(**options)
If a file is attached, returns the uploaded file URL, otherwise returns nil.
Constructor Detail
Initializes the attached file, temporary and permanent storage.
Instance Method Detail
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("")
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)
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" => {} })
Returns whether a file is attached.
attacher.attach(io)
attacher.attached? #=> true
attacher.attach(nil)
attacher.attached? #=> false
Returns whether the file is uploaded to temporary storage.
attacher.cached? # checks current file
attacher.cached?(file) # checks given file
Sets the uploaded file with dirty tracking, and runs validations.
attacher.change(uploaded_file)
attacher.file #=> #<Shrine::UploadedFile>
attacher.changed? #=> true
Returns whether the attachment has changed.
attacher.changed? #=> false
attacher.attach(file)
attacher.changed? #=> true
TODO This will work incorrect if @previous
is nil
Returns options that are automatically forwarded to the uploader. Can be modified with additional data.
Generates serializable data for the attachment.
attacher.data #=> { "id" => "...", "storage" => "...", "metadata": { ... } }
Destroys the attachment.
attacher.file.exists? #=> true
attacher.destroy
attacher.file.exists? #=> false
Destroys the attached file if it exists and is uploaded to permanent storage.
attacher.file.exists? #=> true
attacher.destroy_attached
attacher.file.exists? #=> false
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
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
Returns the attached file.
# when a file is attached
attacher.get #=> #<Shrine::UploadedFile>
# when no file is attached
attacher.get #=> nil
Loads the uploaded file from data generated by Attacher#data
.
attacher.file #=> nil
attacher.load_data({ "id" => "...", "storage" => "...", "metadata" => { ... } })
attacher.file #=> #<Shrine::UploadedFile>
Uploads current file to permanent storage and sets the stored file.
attacher.cached? #=> true
attacher.promote
attacher.stored? #=> true
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
Sets the uploaded file.
attacher.set(uploaded_file)
attacher.file #=> #<Shrine::UploadedFile>
attacher.changed? #=> false
Returns whether the file is uploaded to permanent storage.
attacher.stored? # checks current file
attacher.stored?(file) # checks given file
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" })
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 ...>
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