Module: ActiveStorage::AsyncVariants::Helper

Included in:
AssetTagHelperExtension
Defined in:
lib/active_storage/async_variants/helper.rb

Overview

View/controller helper methods. Included in the AssetTagHelperExtension (so image_tag/video_tag can use them) and added to StatesController via ‘helper ActiveStorage::AsyncVariants::Helper` (so the state partials can use them).

Instance Method Summary collapse

Instance Method Details

#async_variant_direct_url(variant) ⇒ Object



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

def async_variant_direct_url(variant)
  if cdn = ActiveStorage::AsyncVariants.cdn_host
    "#{cdn}/#{variant.key}"
  else
    variant.image.url
  end
end

#async_variant_frame_id(variant) ⇒ Object



27
28
29
30
# File 'lib/active_storage/async_variants/helper.rb', line 27

def async_variant_frame_id(variant)
  digest = variant.variation.digest.gsub(/[^a-zA-Z0-9_-]/, "")
  "async-variant-#{variant.blob.id}-#{digest}"
end

#async_variant_frame_src(variant, kind:, direct:, html_options: {}) ⇒ Object



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

def async_variant_frame_src(variant, kind:, direct:, html_options: {})
  async_variant_state_path(
    signed_blob_id: variant.blob.signed_id,
    variation_key: variant.variation.key,
    kind:,
    direct:,
    opts: html_options.slice(*PASS_THROUGH_HTML_OPTIONS),
  )
end

#async_variant_processed_inline?(variant) ⇒ Boolean

In test with non-bucket-backed services, the gem defers to vanilla ActiveStorage (synchronous vips transform) – inline rendering keeps those environments simple. Otherwise, only inline a normal <img> when the variant has reached the processed terminal state.

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_storage/async_variants/helper.rb', line 14

def async_variant_processed_inline?(variant)
  !variant.blob.bucket_backed? || variant.async_state == "processed"
end

#async_variant_representation_path(variant) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/active_storage/async_variants/helper.rb', line 50

def async_variant_representation_path(variant)
  Rails.application.routes.url_helpers.rails_blob_representation_path(
    variant.blob.signed_id,
    variant.variation.key,
    variant.blob.filename.to_s,
  )
end

#async_variant_resolved_src(variant, direct:) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/active_storage/async_variants/helper.rb', line 18

def async_variant_resolved_src(variant, direct:)
  if direct && variant.async_state == "processed"
    async_variant_direct_url(variant)
  else
    variant.processed if variant.blob.bucket_backed?
    async_variant_representation_path(variant)
  end
end