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

Add index_preview_only option #156

Merged
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
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
Loading