Skip to content

Commit 4417efc

Browse files
authored
Show a thumbnail of all representable Active Storage files + images (#3656)
* Show a thumbnail of all representable Active Storage files not only images * Fix RuboCop offense * Show representable and all images (incl. SVGs) * Fix RuboCop offense
1 parent 35c8702 commit 4417efc

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

lib/rails_admin/config/fields/types/active_storage.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
1818
end
1919

2020
register_instance_option :image? do
21-
if value
22-
mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
23-
mime_type.to_s.match?(/^image/)
24-
end
21+
value && (value.representable? || mime_type(value.filename).to_s.match?(/^image/))
2522
end
2623

2724
register_instance_option :eager_load do
@@ -51,11 +48,11 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
5148
def resource_url(thumb = false)
5249
return nil unless value
5350

54-
if thumb && value.variable?
51+
if thumb && value.representable?
5552
thumb = thumb_method if thumb == true
56-
variant = value.variant(thumb)
53+
repr = value.representation(thumb)
5754
Rails.application.routes.url_helpers.rails_blob_representation_path(
58-
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true
55+
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
5956
)
6057
else
6158
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
@@ -66,6 +63,10 @@ def value
6663
attachment = super
6764
attachment if attachment&.attached?
6865
end
66+
67+
def mime_type(filename_obj)
68+
Mime::Type.lookup_by_extension(filename_obj.extension_without_delimiter)
69+
end
6970
end
7071
end
7172
end

lib/rails_admin/config/fields/types/multiple_active_storage.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,25 @@ class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileU
2323
end
2424

2525
register_instance_option :image? do
26-
if value
27-
mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
28-
mime_type.to_s.match?(/^image/)
29-
end
26+
value && (value.representable? || mime_type(value.filename).to_s.match?(/^image/))
3027
end
3128

3229
def resource_url(thumb = false)
3330
return nil unless value
3431

35-
if thumb && value.variable?
36-
variant = value.variant(thumb_method)
32+
if thumb && value.representable?
33+
repr = value.representation(thumb_method)
3734
Rails.application.routes.url_helpers.rails_blob_representation_path(
38-
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true
35+
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
3936
)
4037
else
4138
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
4239
end
4340
end
41+
42+
def mime_type(filename_obj)
43+
Mime::Type.lookup_by_extension(filename_obj.extension_without_delimiter)
44+
end
4445
end
4546

4647
register_instance_option :attachment_class do

0 commit comments

Comments
 (0)