Skip to content

Commit 5096a4e

Browse files
committed
Show a thumbnail of all representable Active Storage files
not only images
1 parent 13c90fd commit 5096a4e

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

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

Lines changed: 4 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.representable? if value
2522
end
2623

2724
register_instance_option :eager_load do
@@ -43,11 +40,11 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
4340
def resource_url(thumb = false)
4441
return nil unless value
4542

46-
if thumb && value.variable?
43+
if thumb && value.representable?
4744
thumb = thumb_method if thumb == true
48-
variant = value.variant(thumb)
45+
repr = value.representation(thumb)
4946
Rails.application.routes.url_helpers.rails_blob_representation_path(
50-
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true
47+
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
5148
)
5249
else
5350
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@ 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.representable? if value
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)

0 commit comments

Comments
 (0)