module Shrine::Plugins::Backgrounding::AttacherMethods

  1. lib/shrine/plugins/backgrounding.rb

Methods

Public Instance

  1. _delete
  2. _promote
  3. dump
  4. swap

Public Instance methods

_delete (uploaded_file, phase: nil, action: phase)

Calls the deleting block (if registered) with a serializable data hash.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 232
def _delete(uploaded_file, phase: nil, action: phase)
  if background_delete = shrine_class.opts[:backgrounding_delete]
    data = self.class.dump(self).merge(
      "attachment" => uploaded_file.to_json,
      "action"     => (action.to_s if action),
      "phase"      => (action.to_s if action), # legacy
    )
    instance_exec(data, &background_delete)
    uploaded_file
  else
    super
  end
end
_promote (uploaded_file = get, phase: nil, action: phase)

Calls the promoting block (if registered) with a serializable data hash.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 217
def _promote(uploaded_file = get, phase: nil, action: phase)
  if background_promote = shrine_class.opts[:backgrounding_promote]
    data = self.class.dump(self).merge(
      "attachment" => uploaded_file.to_json,
      "action"     => (action.to_s if action),
      "phase"      => (action.to_s if action), # legacy
    )
    instance_exec(data, &background_promote)
  else
    super
  end
end
dump ()

Dumps all the information about the attacher in a serializable hash suitable for passing as an argument to background jobs.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 248
def dump
  {
    "attachment"   => (get && get.to_json),
    "record"       => [record.class.to_s, record.id.to_s],
    "name"         => name.to_s,
    "shrine_class" => shrine_class.name,
  }
end
swap (new_file)

Updates with the new file only if the attachment hasn't changed.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 258
def swap(new_file)
  if self.class.respond_to?(:find_record)
    reloaded = self.class.find_record(record.class, record.id)
    return if reloaded.nil? || self.class.new(reloaded, name).read != read
  end
  super
end