module Shrine::Attacher::InstanceMethods

  1. lib/shrine/attacher.rb

Public Instance Aliases

attached? -> changed?

Attributes

cache [R]

Returns the uploader that is used for the temporary storage.

context [R]

Returns the context that will be sent to the uploader when uploading and deleting. Can be modified with additional data to be sent to the uploader.

errors [R]

Returns an array of validation errors created on file assignment in the Attacher.validate block.

store [R]

Returns the uploader that is used for the permanent storage.

Public Class methods

new (record, name, cache: :cache, store: :store)

Initializes the necessary attributes.

[show source]
   # File lib/shrine/attacher.rb
52 def initialize(record, name, cache: :cache, store: :store)
53   @cache   = shrine_class.new(cache)
54   @store   = shrine_class.new(store)
55   @context = {record: record, name: name}
56   @errors  = []
57 end

Public Instance methods

_delete (uploaded_file, **options)

Delegates to delete!, overriden for backgrounding.

[show source]
    # File lib/shrine/attacher.rb
149 def _delete(uploaded_file, **options)
150   delete!(uploaded_file, **options)
151 end
_promote (uploaded_file = get, **options)

Delegates to promote, overriden for backgrounding.

[show source]
    # File lib/shrine/attacher.rb
116 def _promote(uploaded_file = get, **options)
117   promote(uploaded_file, **options)
118 end
assign (value, **options)

Receives the attachment value from the form. It can receive an already cached file as a JSON string, otherwise it assumes that it's an IO object and uploads it to the temporary storage. The cached file is then written to the attachment attribute in the JSON format.

[show source]
   # File lib/shrine/attacher.rb
69 def assign(value, **options)
70   if value.is_a?(String)
71     return if value == "" || !cached?(uploaded_file(value))
72     assign_cached(uploaded_file(value))
73   else
74     uploaded_file = cache!(value, action: :cache, **options) if value
75     set(uploaded_file)
76   end
77 end
cache! (io, **options)

Uploads the file using the cache uploader, passing the context.

[show source]
    # File lib/shrine/attacher.rb
183 def cache!(io, **options)
184   Shrine.deprecation("Sending :phase to Attacher#cache! is deprecated and will not be supported in Shrine 3. Use :action instead.") if options[:phase]
185   cache.upload(io, context.merge(_equalize_phase_and_action(options)))
186 end
cached? (file = get)

Returns true if attachment is present and cached.

[show source]
    # File lib/shrine/attacher.rb
160 def cached?(file = get)
161   file && cache.uploaded?(file)
162 end
changed? ()

Returns true if a new file has been attached.

[show source]
   # File lib/shrine/attacher.rb
96 def changed?
97   instance_variable_defined?(:@old)
98 end
data_attribute ()

The name of the attribute on the model instance that is used to store the attachment data. Defaults to <attachment>_data.

[show source]
    # File lib/shrine/attacher.rb
208 def data_attribute
209   :"#{name}_data"
210 end
delete! (uploaded_file, **options)

Deletes the file using the uploader, passing the context.

[show source]
    # File lib/shrine/attacher.rb
195 def delete!(uploaded_file, **options)
196   Shrine.deprecation("Sending :phase to Attacher#delete! is deprecated and will not be supported in Shrine 3. Use :action instead.") if options[:phase]
197   store.delete(uploaded_file, context.merge(_equalize_phase_and_action(options)))
198 end
destroy ()

Deletes the current attachment, typically called after destroying the record.

[show source]
    # File lib/shrine/attacher.rb
143 def destroy
144   file = get
145   _delete(file, action: :destroy) if file && !cached?(file)
146 end
finalize ()

Deletes the old file and promotes the new one. Typically this should be called after saving the model instance.

[show source]
    # File lib/shrine/attacher.rb
108 def finalize
109   return if !instance_variable_defined?(:@old)
110   replace
111   remove_instance_variable(:@old)
112   _promote(action: :store) if cached?
113 end
get ()

Returns a Shrine::UploadedFile instantiated from the data written to the attachment attribute.

[show source]
    # File lib/shrine/attacher.rb
171 def get
172   uploaded_file(read) if read
173 end
name ()

Returns the attachment name associated with the attacher.

[show source]
   # File lib/shrine/attacher.rb
63 def name;   context[:name];   end
promote (uploaded_file = get, **options)

Uploads the cached file to store, and writes the stored file to the attachment attribute.

[show source]
    # File lib/shrine/attacher.rb
122 def promote(uploaded_file = get, **options)
123   stored_file = store!(uploaded_file, **options)
124   result = swap(stored_file) or _delete(stored_file, action: :abort)
125   result
126 end
read ()

Reads from the <attachment>_data attribute on the model instance. It returns nil if the value is blank.

[show source]
    # File lib/shrine/attacher.rb
177 def read
178   value = record.send(data_attribute)
179   convert_after_read(value) unless value.nil? || value.empty?
180 end
record ()

Returns the model instance associated with the attacher.

[show source]
   # File lib/shrine/attacher.rb
60 def record; context[:record]; end
replace ()

Deletes the previous attachment that was replaced, typically called after the model instance is saved with the new attachment.

[show source]
    # File lib/shrine/attacher.rb
137 def replace
138   _delete(@old, action: :replace) if @old && !cached?(@old)
139 end
save ()

Plugins can override this if they want something to be done before save.

[show source]
    # File lib/shrine/attacher.rb
103 def save
104 end
set (uploaded_file)

Accepts a Shrine::UploadedFile object and writes it to the attachment attribute. It then runs file validations, and records that the attachment has changed.

[show source]
   # File lib/shrine/attacher.rb
82 def set(uploaded_file)
83   file = get
84   @old = file unless uploaded_file == file
85   _set(uploaded_file)
86   validate
87 end
shrine_class ()

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

[show source]
    # File lib/shrine/attacher.rb
214 def shrine_class
215   self.class.shrine_class
216 end
store! (io, **options)

Uploads the file using the store uploader, passing the context.

[show source]
    # File lib/shrine/attacher.rb
189 def store!(io, **options)
190   Shrine.deprecation("Sending :phase to Attacher#store! is deprecated and will not be supported in Shrine 3. Use :action instead.") if options[:phase]
191   store.upload(io, context.merge(_equalize_phase_and_action(options)))
192 end
stored? (file = get)

Returns true if attachment is present and stored.

[show source]
    # File lib/shrine/attacher.rb
165 def stored?(file = get)
166   file && store.uploaded?(file)
167 end
swap (uploaded_file)

Calls update, overriden in ORM plugins, and returns true if the attachment was successfully updated.

[show source]
    # File lib/shrine/attacher.rb
130 def swap(uploaded_file)
131   update(uploaded_file)
132   uploaded_file if uploaded_file == get
133 end
uploaded_file (object, &block)

Enhances Shrine.uploaded_file with the ability to recognize uploaded files as JSON strings.

[show source]
    # File lib/shrine/attacher.rb
202 def uploaded_file(object, &block)
203   shrine_class.uploaded_file(object, &block)
204 end
url (**options)

Returns the URL to the attached file if it's present. It forwards any given URL options to the storage.

[show source]
    # File lib/shrine/attacher.rb
155 def url(**options)
156   get.url(**options) if read
157 end
validate ()

Runs the validations defined by Attacher.validate.

[show source]
   # File lib/shrine/attacher.rb
90 def validate
91   errors.clear
92   validate_block if get
93 end