Skip to content

Commit

Permalink
Add an ability to display original images
Browse files Browse the repository at this point in the history
The current implementation always displays a variant if an image is
variable regardless of setting `show_preview_variant` to `nil`.

```
Field::ActiveStorage.with_options(show_preview_variant: nil)
```

This change allows us to show the original image by passing `false`.

```
Field::ActiveStorage.with_options(show_preview_variant: false)
```

Though the behavioral difference between `nil` and `false` is subtle, it
somewhat aligns with the Action View's `layout` method.

> https://api.rubyonrails.org/classes/ActionView/Layouts/ClassMethods.html#method-i-layout
> - false: There is no layout
> - nil: Force default layout behavior with inheritance
  • Loading branch information
shouichi committed Dec 28, 2023
1 parent 694d3fb commit f32050e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ It might be necessary to add to app/assets/config/manifest.js:
```
When set, this takes precedence over `index_preview_size` and `show_preview_size`.

Setting this to `false` displays original images instead of variants.

Defaults to `nil`.

### index_display_count

Displays the number of attachments in the `index` action.
Expand Down
2 changes: 2 additions & 0 deletions app/views/fields/active_storage/_preview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<% if attachment.variable? %>
<% if variant.nil? %>
<%= image_tag(field.variant(attachment, resize_to_limit: size)) %>
<% elsif variant == false %>
<%= image_tag(field.url(attachment)) %>
<% else %>
<%= image_tag(attachment.variant(variant)) %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
- David Ma [@taikon](https://github.com/taikon)
- Dmitry Davydov [@haukot](https://github.com/haukot)
- Jeanine Soterwood [@littleforest](https://github.com/littleforest)
- Shouichi Kamiya [@shouichi](https://github.com/shouichi)

0 comments on commit f32050e

Please sign in to comment.