Module: ActiveStorageDedup::ChangesExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_storage_dedup/changes_extension.rb

Instance Method Summary collapse

Instance Method Details

#find_or_build_blobObject

Patch Changes::CreateOne#find_or_build_blob to pass context



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_storage_dedup/changes_extension.rb', line 8

def find_or_build_blob
  Rails.logger.debug "[ActiveStorageDedup] ChangesExtension#find_or_build_blob called for #{name} attachment"
  Rails.logger.debug "[ActiveStorageDedup] Attachable type: #{attachable.class.name}"

  case attachable
  when ActiveStorage::Blob
    attachable
  when ActionDispatch::Http::UploadedFile
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable.open,
      filename: attachable.original_filename,
      content_type: attachable.content_type,
      __dedup_record: record,
      __dedup_attachment_name: name,
      service_name: attachment_service_name
    )
  when Rack::Test::UploadedFile
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable.respond_to?(:open) ? attachable.open : attachable,
      filename: attachable.original_filename,
      content_type: attachable.content_type,
      __dedup_record: record,
      __dedup_attachment_name: name,
      service_name: attachment_service_name
    )
  when Hash
    ActiveStorage::Blob.build_after_unfurling(
      **attachable.reverse_merge(
        record: record,
        service_name: attachment_service_name
      ).symbolize_keys.merge(
        __dedup_record: record,
        __dedup_attachment_name: name
      )
    )
  when String
    ActiveStorage::Blob.find_signed!(attachable, record: record)
  when File
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable,
      filename: File.basename(attachable),
      __dedup_record: record,
      __dedup_attachment_name: name,
      service_name: attachment_service_name
    )
  when Pathname
    ActiveStorage::Blob.build_after_unfurling(
      io: attachable.open,
      filename: File.basename(attachable),
      __dedup_record: record,
      __dedup_attachment_name: name,
      service_name: attachment_service_name
    )
  else
    raise(
      ArgumentError,
      "Could not find or build blob: expected attachable, " \
        "got #{attachable.inspect}"
    )
  end
end