Module: ActiveStorage::AsyncVariants::VariantWithRecordExtension

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

Instance Method Summary collapse

Instance Method Details

#async_stateObject



61
62
63
64
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 61

def async_state
  return nil unless blob.bucket_backed?
  async_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/variant_with_record_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

#errorObject



57
58
59
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 57

def error
  async_record&.error
end

#failed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 53

def failed?
  async_record&.state == "failed"
end

#last_heartbeat_atObject



74
75
76
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 74

def last_heartbeat_at
  async_record&.last_heartbeat_at
end

#pending?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 49

def pending?
  async_record.nil? || async_record.state == "pending"
end

#processedObject

Block vanilla ActiveStorage’s synchronous transform on bucket-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/variant_with_record_extension.rb', line 9

def processed
  blob.bucket_backed? ? self : super
end

#processed?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 37

def processed?
  if blob.bucket_backed?
    async_record&.state == "processed"
  else
    super
  end
end

#processing?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 45

def processing?
  async_record&.state == "processing"
end

#progressObject



66
67
68
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 66

def progress
  async_record&.progress
end

#progress_known?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 70

def progress_known?
  !async_record&.progress.nil?
end

#urlObject



29
30
31
32
33
34
35
# File 'lib/active_storage/async_variants/variant_with_record_extension.rb', line 29

def url(...)
  if blob.bucket_backed? && async_variant? && !processed?
    blob.url(...)
  else
    super
  end
end