shimmer 0.0.42 → 0.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 557b926b9a2cf8f02c044f15d4a5cd174ffd79c7882fa338f81facf5ca29626d
4
- data.tar.gz: dcf50f8c696654a586c0c9bfce088fe54beb7d1ae571e6202946fb7395edaf2d
3
+ metadata.gz: fa3d1482f1b06e46680603bff5b30b0bb5b65fa2cc1e614dc7dd770b32854180
4
+ data.tar.gz: fb9824638cb575b87d6f904dd7ae4ad46d73a5fab3307c913863e59a7fbfef63
5
5
  SHA512:
6
- metadata.gz: 5b09d05c6dc817fe747796540fb4c3cf625e42b84313555e8d3a48f73969ac67ee5b3fc528af056d6cd1f08a718c9dcddab65147a606649ebd1a46c81ba7ea8d
7
- data.tar.gz: 11b0a1a52dbb32986d8a760ff52efea2ae7b3c734678bc672828fbbd911264c5bf553d973a129fc1d923fb04719a691e2b385e0eb6da77490e2bd08f478e1e26
6
+ metadata.gz: 9f2d78f02e3bbd3815ec7f72311026a9e08b3aa635eaaa372c315e50112299bb2c3b58f506d980473a18f34041165eff8233f5325add59c41a4097411901f18d
7
+ data.tar.gz: ed6698ede442254af839174bb6d530cbaecc59b810772c68ece8a729f4dd48565abae3ffbb5d7570e32d6d8748780600a7b839f6bab48da26ca05b662f4c3438
data/README.md CHANGED
@@ -140,7 +140,7 @@ Then, if there are specific cops you want to use in the specific project you are
140
140
 
141
141
  `ActiveStorage` is great, but serving of files, especially behind a CDN, can be complicated to get right. Shimmer has your back.
142
142
 
143
- It overrides `image_tag` and automatically resizes your image and creates a static, cacheable URL.
143
+ It overrides `image_tag` and automatically resizes your image, converts it to WebP, and creates a static, cacheable URL.
144
144
 
145
145
  ```ruby
146
146
  # use an image tag
@@ -5,11 +5,14 @@ module Shimmer
5
5
  def show
6
6
  expires_in 1.year, public: true
7
7
  request.session_options[:skip] = true # prevents a session cookie from being set (would prevent caching on CDNs)
8
+
8
9
  proxy = FileProxy.restore(params.require(:id))
9
- send_data proxy.file,
10
+ send_data(
11
+ proxy.file,
10
12
  filename: proxy.filename.to_s,
11
13
  type: proxy.content_type,
12
14
  disposition: "inline"
15
+ )
13
16
  rescue ActiveRecord::RecordNotFound, ActiveStorage::FileNotFoundError
14
17
  head :not_found
15
18
  end
@@ -15,23 +15,31 @@ module Shimmer
15
15
  def image_tag(source, **options)
16
16
  return nil if source.blank?
17
17
 
18
- if source.is_a?(ActiveStorage::Variant) || source.is_a?(ActiveStorage::Attached) || source.is_a?(ActiveStorage::Attachment) || source.is_a?(ActionText::Attachment)
18
+ if source.is_a?(ActiveStorage::Variant) ||
19
+ source.is_a?(ActiveStorage::VariantWithRecord) ||
20
+ source.is_a?(ActiveStorage::Attached) ||
21
+ source.is_a?(ActiveStorage::Attachment) ||
22
+ (Object.const_defined?("ActionText::Attachment") && source.is_a?(ActionText::Attachment))
19
23
  attachment = source
20
24
  width = options[:width]
21
25
  height = options[:height]
22
26
  source = image_file_path(source, width: width, height: height)
23
27
  options[:loading] ||= :lazy
24
- options[:srcset] = "#{source} 1x, #{image_file_path(attachment, width: width.to_i * 2, height: height ? height.to_i * 2 : nil)} 2x" if options[:width].present?
28
+ if options[:width].present?
29
+ source_2x = image_file_path(attachment, width: width.to_i * 2, height: height ? height.to_i * 2 : nil)
30
+ options[:srcset] = "#{source} 1x, #{source_2x} 2x"
31
+ end
25
32
  end
33
+
26
34
  super(source, options)
27
35
  end
28
36
 
29
- def image_file_path(source, width: nil, height: nil)
30
- image_file_proxy(source, width: width, height: height, return_type: :path)
37
+ def image_file_path(source, **)
38
+ image_file_proxy(source, **, return_type: :path)
31
39
  end
32
40
 
33
- def image_file_url(source, width: nil, height: nil)
34
- image_file_proxy(source, width: width, height: height, return_type: :url)
41
+ def image_file_url(source, **)
42
+ image_file_proxy(source, **, return_type: :url)
35
43
  end
36
44
 
37
45
  def image_file_proxy(source, width: nil, height: nil, return_type: nil)
@@ -5,25 +5,21 @@ module Shimmer
5
5
  attr_reader :blob_id
6
6
 
7
7
  delegate :message_verifier, to: :class
8
- delegate :content_type, :filename, to: :blob
8
+ delegate :content_type, :filename, to: :variant
9
9
 
10
10
  class << self
11
11
  def restore(id)
12
12
  blob_id, resize = message_verifier.verified(id)
13
-
14
- if resize.is_a?(String)
15
- width, height = legacy_resize_string_to_tuple(resize)
16
- elsif resize.is_a?(Array)
17
- width, height = resize
18
- end
13
+ width, height = parse_resize(resize)
19
14
 
20
15
  new(blob_id: blob_id, width: width, height: height)
21
16
  end
22
17
 
23
- # In the past, we generated the IDs with ImageMagick style "200x200>" strings. We don't do that anymore, but to prevent all old URLs breaking and caches invalidating at once, we grandfather these URLs in.
24
- def legacy_resize_string_to_tuple(resize)
18
+ def parse_resize(resize)
25
19
  return if resize.blank?
20
+ return resize if resize.is_a?(Array)
26
21
 
22
+ # In the past, we generated the IDs with ImageMagick style "200x200>" strings. We don't do that anymore, but to prevent all old URLs breaking and caches invalidating at once, we grandfather these URLs in.
27
23
  matches = resize.match(/(?<width>\d*)x(?<height>\d*)/)
28
24
 
29
25
  [
@@ -54,12 +50,13 @@ module Shimmer
54
50
  @blob ||= ActiveStorage::Blob.find(blob_id)
55
51
  end
56
52
 
57
- def resizeable?
58
- @resize.present? && blob.content_type.exclude?("svg")
59
- end
60
-
61
53
  def variant
62
- @variant ||= resizeable? ? blob.representation(resize_to_limit: @resize).processed : blob
54
+ @variant ||= if blob.representable?
55
+ options = {resize_to_limit: @resize, format: "webp"}
56
+ blob.representation(options.compact).processed
57
+ else
58
+ blob
59
+ end
63
60
  end
64
61
 
65
62
  def file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Shimmer
4
- VERSION = "0.0.42"
4
+ VERSION = "0.0.43"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shimmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.42
4
+ version: 0.0.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Ravens
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-04 00:00:00.000000000 Z
11
+ date: 2025-04-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: