Skip to content

Commit adbf494

Browse files
committed
render optimized background image variant for block background image url
1 parent ab6e88a commit adbf494

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

app/models/concerns/better_together/content/block_attributes.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ module BlockAttributes
1919
include BetterTogether::Translatable
2020
include BetterTogether::Visible
2121

22-
has_one_attached :background_image_file
22+
has_one_attached :background_image_file do |attachable|
23+
attachable.variant :optimized, resize_to_limit: [1920, 1080], saver: { strip: true, quality: 75, interlace: true, optimize_coding: true, trellis_quant: true, quant_table: 3 }, format: 'jpg'
24+
end
2325

2426
validates :background_image_file,
2527
content_type: CONTENT_TYPES,
@@ -128,7 +130,9 @@ def block_styles
128130

129131
if background_image_file.attached?
130132
ActiveStorage::Current.url_options = { host: BetterTogether.base_url }
131-
bg_image_style = ["url(#{background_image_file.url})", background_image.presence].reject(&:blank?).join(', ')
133+
bg_variant = background_image_file.variant(:optimized)
134+
135+
bg_image_style = ["url(#{Rails.application.routes.url_helpers.rails_representation_url(bg_variant)})", background_image.presence].reject(&:blank?).join(', ')
132136
styles = styles.merge({
133137
background_image: bg_image_style,
134138
background_size: (background_size.present? ? background_size : 'cover')

0 commit comments

Comments
 (0)