Module: Shrine::Plugins::Backgrounding::AttacherClassMethods

Defined in:
lib/shrine/plugins/backgrounding.rb

Instance Method Summary collapse

Instance Method Details

#delete(data = nil, &block) ⇒ Object

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shrine/plugins/backgrounding.rb', line 29

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) ⇒ Object

Delegates to ‘Attacher#dump`.



44
45
46
# File 'lib/shrine/plugins/backgrounding.rb', line 44

def dump(attacher)
  attacher.dump
end

#load(data) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/shrine/plugins/backgrounding.rb', line 50

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) ⇒ Object

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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shrine/plugins/backgrounding.rb', line 71

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) ⇒ Object

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/shrine/plugins/backgrounding.rb', line 12

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