Module: Fileboost::Helpers

Defined in:
lib/fileboost/helpers.rb

Instance Method Summary collapse

Instance Method Details

#fileboost_image_tag(asset, **options) ⇒ String

Generate an optimized image tag using Fileboost

Examples:

fileboost_image_tag(user.avatar, resize: { w: 300, h: 200 }, alt: "Avatar")
fileboost_image_tag(post.featured_image.blob, resize: { width: 1200, quality: 90 }, class: "hero-image")

Parameters:

  • asset (ActiveStorage::Blob, ActiveStorage::Attached, ActiveStorage::VariantWithRecord)

    The ActiveStorage image asset

  • options (Hash)

    Accepts a ‘:resize` hash for transformations plus standard HTML options

Returns:

  • (String)

    HTML image tag



14
15
16
17
18
19
20
21
22
23
# File 'lib/fileboost/helpers.rb', line 14

def fileboost_image_tag(asset, **options)
  # Extract resize options for transformation
  resize_options = options.delete(:resize) || {}

  # Generate the optimized URL
  optimized_url = fileboost_url_for(asset, resize: resize_options)

  # Use the optimized URL with Rails image_tag for consistency
  image_tag(optimized_url, **options)
end

#fileboost_responsive_urls(asset, sizes, **base_options) ⇒ Hash

Generate multiple image URLs for responsive images

Example:

fileboost_responsive_urls(hero.image, [
  { width: 400, suffix: "sm" },
  { width: 800, suffix: "md" },
  { width: 1200, suffix: "lg" }
], resize: { quality: 85, format: :webp })
# Returns: { "sm" => "url1", "md" => "url2", "lg" => "url3" }

Parameters:

  • asset (ActiveStorage::Blob, ActiveStorage::Attached, ActiveStorage::VariantWithRecord)

    The ActiveStorage image asset

  • sizes (Array<Hash>)

    Array of size configurations

  • base_options (Hash)

    Base transformation options applied to all sizes

Returns:

  • (Hash)

    Hash with size keys and URL values



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fileboost/helpers.rb', line 59

def fileboost_responsive_urls(asset, sizes, **base_options)
  urls = {}

  sizes.each do |size_config|
    suffix = size_config[:suffix] || size_config["suffix"]
    size_options = size_config.except(:suffix, "suffix")

    # Merge size options into base resize options
    merged_resize_options = (base_options[:resize] || {}).merge(size_options)
    combined_options = base_options.merge(resize: merged_resize_options)

    url = fileboost_url_for(asset, **combined_options)
    urls[suffix] = url if !url.nil? && !url.empty?
  end

  urls
end

#fileboost_url_for(asset, **options) ⇒ String?

Generate an optimized URL using Fileboost

Examples:

fileboost_url_for(post.image, resize: { width: 500, format: :webp })
fileboost_url_for(user.avatar.blob, resize: { w: 1200, h: 400, q: 85 })

Parameters:

  • asset (ActiveStorage::Blob, ActiveStorage::Attached, ActiveStorage::VariantWithRecord)

    The ActiveStorage image asset

  • options (Hash)

    Supports a ‘:resize` hash for image transformations and an optional `:disposition`

Returns:

  • (String, nil)

    The optimized URL or nil if generation failed

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
# File 'lib/fileboost/helpers.rb', line 34

def fileboost_url_for(asset, **options)
  # Validate that asset is an ActiveStorage object
  raise ArgumentError, "Invalid asset type #{asset.class}. Only ActiveStorage objects are supported." unless valid_activestorage_asset?(asset)

  # Validate configuration
  raise Fileboost::ConfigurationError, "Invalid Fileboost configuration" unless Fileboost.config.valid?

  # Build the optimized URL
  Fileboost::UrlBuilder.build_url(asset, **options)
end