Skip to content

Commit

Permalink
Add index_preview_only option
Browse files Browse the repository at this point in the history
  • Loading branch information
pedantic-git authored and Dreamersoul committed Apr 30, 2024
1 parent 583fcfe commit 200b641
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions app/views/fields/active_storage/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ 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',
locals: {
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 %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/fields/active_storage/_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
<div>
<%= render partial: 'fields/active_storage/preview', locals: local_assigns %>
</div>
<% end %>

<% unless preview_only %>
<% if attachment.persisted? %>
<div>
<%= link_to attachment.filename, field.blob_url(attachment), title: attachment.filename %>
Expand All @@ -40,3 +44,4 @@ controlled via a boolean local variable.
</div>
<hr>
<% end %>
<% end %>
4 changes: 3 additions & 1 deletion app/views/fields/active_storage/_items.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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| %>
Expand All @@ -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
} %>
</div>
<% end %>
4 changes: 4 additions & 0 deletions lib/administrate/field/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 200b641

Please sign in to comment.