Skip to content

Commit

Permalink
Show a thumbnail of all representable Active Storage files + images (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
wvengen authored Aug 25, 2024
1 parent 35c8702 commit 4417efc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
15 changes: 8 additions & 7 deletions lib/rails_admin/config/fields/types/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
end

register_instance_option :image? do
if value
mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
mime_type.to_s.match?(/^image/)
end
value && (value.representable? || mime_type(value.filename).to_s.match?(/^image/))
end

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

if thumb && value.variable?
if thumb && value.representable?
thumb = thumb_method if thumb == true
variant = value.variant(thumb)
repr = value.representation(thumb)
Rails.application.routes.url_helpers.rails_blob_representation_path(
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
)
else
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
Expand All @@ -66,6 +63,10 @@ def value
attachment = super
attachment if attachment&.attached?
end

def mime_type(filename_obj)
Mime::Type.lookup_by_extension(filename_obj.extension_without_delimiter)
end
end
end
end
Expand Down
15 changes: 8 additions & 7 deletions lib/rails_admin/config/fields/types/multiple_active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileU
end

register_instance_option :image? do
if value
mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
mime_type.to_s.match?(/^image/)
end
value && (value.representable? || mime_type(value.filename).to_s.match?(/^image/))
end

def resource_url(thumb = false)
return nil unless value

if thumb && value.variable?
variant = value.variant(thumb_method)
if thumb && value.representable?
repr = value.representation(thumb_method)
Rails.application.routes.url_helpers.rails_blob_representation_path(
variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
)
else
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
end
end

def mime_type(filename_obj)
Mime::Type.lookup_by_extension(filename_obj.extension_without_delimiter)
end
end

register_instance_option :attachment_class do
Expand Down

0 comments on commit 4417efc

Please sign in to comment.