Module: ActiveStorage::AsyncVariants::PreviewExtension

Defined in:
lib/active_storage/async_variants/preview_extension.rb

Instance Method Summary collapse

Instance Method Details

#async_stateObject



50
51
52
53
# File 'lib/active_storage/async_variants/preview_extension.rb', line 50

def async_state
  return nil unless async_preview?
  find_preview_variant_record&.state || "pending"
end

#enqueue!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_storage/async_variants/preview_extension.rb', line 13

def enqueue!
  if result = find_named_async_variant
    attachment, variant_name, _ = result

    blob.variant_records.create!(
      variation_digest: variation.digest,
      state: "pending",
    )
    ActiveStorage::AsyncVariants::ProcessJob.perform_later(
      attachment.record, attachment.name, variant_name.to_s,
    )
  end
rescue ActiveRecord::RecordNotUnique
  # another caller (or a leftover record) wins; their job handles it
end

#keyObject



41
42
43
44
45
46
47
48
# File 'lib/active_storage/async_variants/preview_extension.rb', line 41

def key
  if async_preview?
    raise ActiveStorage::Preview::UnprocessedError unless preview_variant_processed?
    find_preview_variant_record.image.blob.key
  else
    super
  end
end

#processedObject

Block vanilla ActiveStorage’s synchronous preview transform on async-backed services; rely on the auto-enqueue (AttachmentExtension) path – and the public #enqueue! – to dispatch ProcessJob.



9
10
11
# File 'lib/active_storage/async_variants/preview_extension.rb', line 9

def processed
  async_preview? ? self : super
end

#processed?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_storage/async_variants/preview_extension.rb', line 29

def processed?
  async_preview? ? preview_variant_processed? : super
end

#urlObject



33
34
35
36
37
38
39
# File 'lib/active_storage/async_variants/preview_extension.rb', line 33

def url(...)
  if async_preview?
    preview_variant_processed? ? find_preview_variant_record.image.url(...) : fallback_preview_url(...)
  else
    super
  end
end