Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a thumbnail of all representable Active Storage files + images #3656

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -43,11 +40,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 @@ -58,6 +55,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