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_box_dimensions(variant) ⇒ Object



24
25
26
27
28
29
# File 'lib/active_storage/async_variants/helper.rb', line 24

def async_variant_box_dimensions(variant)
  resize = variant.variation.transformations
    .values_at(:resize_to_limit, :resize_to_fit, :resize_to_fill)
    .compact.first
  resize.is_a?(Array) ? { width: resize[0], height: resize[1] } : {}
end

#async_variant_direct_url(variant) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/active_storage/async_variants/helper.rb', line 63

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



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

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



53
54
55
56
57
58
59
60
61
# File 'lib/active_storage/async_variants/helper.rb', line 53

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_placeholder_tag(variant, html_options = {}, kind: :image) ⇒ Object

Invisible box reserving layout for the progress bar. The image src is a tiny gem GIF whose URL ends in the media filename (no original fetched); the video placeholder carries no <source>.



13
14
15
16
17
18
19
20
21
22
# File 'lib/active_storage/async_variants/helper.rb', line 13

def async_variant_placeholder_tag(variant, html_options = {}, kind: :image)
  opts = async_variant_box_dimensions(variant)
    .merge(html_options.symbolize_keys.except(:src, :controls, :autoplay, :preload, :poster))
  if kind == :video
    (:video, "", opts)
  else
    src = Rails.application.routes.url_helpers.async_variant_placeholder_path(variant.blob.filename.to_s)
    image_tag(src, opts)
  end
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)


35
36
37
# File 'lib/active_storage/async_variants/helper.rb', line 35

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

#async_variant_representation_path(variant) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/active_storage/async_variants/helper.rb', line 71

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



39
40
41
42
43
44
45
46
# File 'lib/active_storage/async_variants/helper.rb', line 39

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