From e430834b3bea478db65ed6f9d6c6c02e9ea3425c Mon Sep 17 00:00:00 2001 From: Quinn Daley Date: Mon, 29 Apr 2024 15:42:23 +0100 Subject: [PATCH] Add index_preview_only option --- README.md | 4 ++++ app/views/fields/active_storage/_index.html.erb | 6 ++++-- app/views/fields/active_storage/_item.html.erb | 5 +++++ app/views/fields/active_storage/_items.html.erb | 4 +++- lib/administrate/field/active_storage.rb | 4 ++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e642bb2..a07c549 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,10 @@ in the `index` action. Defaults to `true`. +### index_preview_only + +If true, will show only the preview in the `index` action, and not the filename or destroy link (if set). + ### index_preview_size and show_preview_size Indicate the size of the image preview for the `index` and `show` actions, respectively. diff --git a/app/views/fields/active_storage/_index.html.erb b/app/views/fields/active_storage/_index.html.erb index 4cd1612..304ad9b 100644 --- a/app/views/fields/active_storage/_index.html.erb +++ b/app/views/fields/active_storage/_index.html.erb @@ -22,7 +22,8 @@ By default, the attribute is rendered as an image tag. locals: { field: field, variant: field.index_preview_variant, - size: field.index_preview_size + size: field.index_preview_size, + preview_only: field.index_preview_only? } %> <% else %> <%= render partial: 'fields/active_storage/item', @@ -30,7 +31,8 @@ By default, the attribute is rendered as an image tag. field: field, attachment: field.data, variant: field.index_preview_variant, - size: field.index_preview_size + size: field.index_preview_size, + preview_only: field.index_preview_only? } %> <% end %> <% end %> diff --git a/app/views/fields/active_storage/_item.html.erb b/app/views/fields/active_storage/_item.html.erb index 2f1f40e..5019fc6 100644 --- a/app/views/fields/active_storage/_item.html.erb +++ b/app/views/fields/active_storage/_item.html.erb @@ -19,13 +19,17 @@ controlled via a boolean local variable. - `size`: [x, y] Maximum size of the ActiveStorage preview. +- `preview_only`: + If true, show only the previous and not the name or destroy link %> +<% preview_only = local_assigns.fetch(:preview_only, false) %> <% if field.show_display_preview? && attachment.persisted? %>
<%= render partial: 'fields/active_storage/preview', locals: local_assigns %>
<% end %> +<% unless preview_only %> <% if attachment.persisted? %>
<%= link_to attachment.filename, field.blob_url(attachment), title: attachment.filename %> @@ -40,3 +44,4 @@ controlled via a boolean local variable.

<% end %> +<% end %> \ No newline at end of file diff --git a/app/views/fields/active_storage/_items.html.erb b/app/views/fields/active_storage/_items.html.erb index 08b2f25..47088b6 100644 --- a/app/views/fields/active_storage/_items.html.erb +++ b/app/views/fields/active_storage/_items.html.erb @@ -17,6 +17,7 @@ This partial renders one or more attachments <% variant = local_assigns.fetch(:variant, field.show_preview_variant) size = local_assigns.fetch(:size, field.show_preview_size) + preview_only = local_assigns.fetch(:preview_only, false) %> <% field.attachments.each do |attachment| %> @@ -26,7 +27,8 @@ This partial renders one or more attachments field: field, attachment: attachment, variant: variant, - size: size + size: size, + preview_only: preview_only } %> <% end %> diff --git a/lib/administrate/field/active_storage.rb b/lib/administrate/field/active_storage.rb index 7244946..3ea937e 100644 --- a/lib/administrate/field/active_storage.rb +++ b/lib/administrate/field/active_storage.rb @@ -20,6 +20,10 @@ def index_preview_variant options.fetch(:index_preview_variant, nil) end + def index_preview_only? + options.fetch(:index_preview_only, false) + end + def index_display_count? options.fetch(:index_display_count) { attached? && attachments.count != 1 } end