module Shrine::Plugins::Backgrounding::AttacherClassMethods

  1. lib/shrine/plugins/backgrounding.rb

Methods

Public Instance

  1. delete
  2. dump
  3. load
  4. load_record
  5. promote

Public Instance methods

delete (data = nil, &block)

If block is passed in, stores it to be called on deletion. Otherwise resolves data into objects and calls Shrine#delete.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 159
def delete(data = nil, &block)
  if block
    shrine_class.opts[:backgrounding_delete] = block
  else
    attacher = load(data)
    uploaded_file = attacher.uploaded_file(data["attachment"])
    action = data["action"].to_sym if data["action"]

    attacher.delete!(uploaded_file, action: action)

    attacher
  end
end
dump (attacher)

Delegates to Attacher#dump.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 174
def dump(attacher)
  attacher.dump
end
load (data)

Loads the data created by dump, resolving the record and returning the attacher.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 180
def load(data)
  record = load_record(data)
  name = data["name"].to_sym

  if record.respond_to?(:"#{name}_attacher")
    attacher = record.send(:"#{name}_attacher")
  elsif data["shrine_class"]
    shrine_class = Object.const_get(data["shrine_class"])
    attacher = shrine_class::Attacher.new(record, name)
  else
    fail Error, "cannot load anonymous uploader class"
  end

  attacher
end
load_record (data)

Resolves the record from backgrounding data. If the record was found, returns it. If the record wasn't found, returns an instance of the model with ID assigned for logging. If find_record isn't defined, then it is a PORO model and should be instantiated with the cached attachment.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 201
def load_record(data)
  record_class, record_id = data["record"]
  record_class = Object.const_get(record_class)

  if respond_to?(:find_record)
    record   = find_record(record_class, record_id)
    record ||= record_class.new.tap do |instance|
      # so that the id is always included in file deletion logs
      instance.singleton_class.send(:define_method, :id) { record_id }
    end
  else
    record = record_class.new
    record.send(:"#{data["name"]}_data=", data["attachment"])
  end

  record
end
promote (data = nil, &block)

If block is passed in, stores it to be called on promotion. Otherwise resolves data into objects and calls Attacher#promote.

[show source]
# File lib/shrine/plugins/backgrounding.rb, line 142
def promote(data = nil, &block)
  if block
    shrine_class.opts[:backgrounding_promote] = block
  else
    attacher = load(data)
    cached_file = attacher.uploaded_file(data["attachment"])
    action = data["action"].to_sym if data["action"]

    return if cached_file != attacher.get
    attacher.promote(cached_file, action: action) or return

    attacher
  end
end