Module: Fileboost::Helpers
- Defined in:
- lib/fileboost/helpers.rb
Instance Method Summary collapse
-
#fileboost_image_tag(asset, **options) ⇒ String
Generate an optimized image tag using Fileboost.
-
#fileboost_responsive_urls(asset, sizes, **base_options) ⇒ Hash
Generate multiple image URLs for responsive images.
-
#fileboost_url_for(asset, **options) ⇒ String?
Generate an optimized URL using Fileboost.
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")
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fileboost/helpers.rb', line 14 def fileboost_image_tag(asset, **) # Extract resize options for transformation = .delete(:resize) || {} # Generate the optimized URL optimized_url = fileboost_url_for(asset, resize: ) # Use the optimized URL with Rails image_tag for consistency image_tag(optimized_url, **) 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" }
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, **) urls = {} sizes.each do |size_config| suffix = size_config[:suffix] || size_config["suffix"] = size_config.except(:suffix, "suffix") # Merge size options into base resize options = ([:resize] || {}).merge() = .merge(resize: ) url = fileboost_url_for(asset, **) 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 })
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fileboost/helpers.rb', line 34 def fileboost_url_for(asset, **) # 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, **) end |