Skip to content

Commit ec5c898

Browse files
authored
Merge pull request rails#40712 from ghiculescu/patch-1
Mention `representation`/`representable?` in ActiveStorage guide [ci skip]
2 parents ea0cce4 + d2b6fc6 commit ec5c898

File tree

1 file changed

+49
-19
lines changed

1 file changed

+49
-19
lines changed

guides/source/active_storage_overview.md

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -533,68 +533,98 @@ It's important to know that the file is not yet available in the `after_create`
533533
Analyzing Files
534534
---------------
535535

536-
Active Storage [analyzes](https://api.rubyonrails.org/classes/ActiveStorage/Blob/Analyzable.html#method-i-analyze) files once they've been uploaded by queuing a job in Active Job. Analyzed files will store additional information in the metadata hash, including `analyzed: true`. You can check whether a blob has been analyzed by calling [`analyzed?`][] on it.
536+
Active Storage analyzes files once they've been uploaded by queuing a job in Active Job. Analyzed files will store additional information in the metadata hash, including `analyzed: true`. You can check whether a blob has been analyzed by calling [`analyzed?`][] on it.
537537

538538
Image analysis provides `width` and `height` attributes. Video analysis provides these, as well as `duration`, `angle`, and `display_aspect_ratio`.
539539

540540
Analysis requires the `mini_magick` gem. Video analysis also requires the [FFmpeg](https://www.ffmpeg.org/) library, which you must include separately.
541541

542542
[`analyzed?`]: https://api.rubyonrails.org/classes/ActiveStorage/Blob/Analyzable.html#method-i-analyzed-3F
543543

544-
Transforming Images
545-
-------------------
544+
Displaying Images, Videos, and PDFs
545+
---------------
546+
547+
Active Storage supports representing a variety of files. You can call
548+
[`representation`][] on an attachment to display an image variant, or a
549+
preview of a video or PDF. Before calling `representation`, check if the
550+
attachment can be represented by calling [`representable?`]. Some file formats
551+
can't be previewed by ActiveStorage out of the box (eg. Word documents); if
552+
`representable?` returns false you may want to [link to](#linking-to-files)
553+
the file instead.
554+
555+
```erb
556+
<ul>
557+
<% @message.files.each do |file| %>
558+
<li>
559+
<% if file.representable? %>
560+
<%= image_tag file.representation(resize_to_limit: [100, 100]) %>
561+
<% else %>
562+
<%= link_to rails_blob_path(file, disposition: "attachment") do %>
563+
<%= image_tag "placeholder.png", alt: "Download file" %>
564+
<% end %>
565+
<% end %>
566+
</li>
567+
<% end %>
568+
</ul>
569+
```
570+
571+
Internally, `representation` calls `variant` for images, and `preview` for
572+
previewable files. You can also call these methods directly.
546573

574+
[`representable?`]: https://api.rubyonrails.org/classes/ActiveStorage/Blob/Representable.html#method-i-representable-3F
575+
[`representation`]: https://api.rubyonrails.org/classes/ActiveStorage/Blob/Representable.html#method-i-representation
576+
577+
### Transforming Images
578+
579+
Transforming images allows you to display the image at your choice of dimensions.
547580
To enable variants, add the `image_processing` gem to your `Gemfile`:
548581

549582
```ruby
550583
gem 'image_processing'
551584
```
552585

553-
To create a variation of an image, call [`variant`][] on the attachment. You can pass any transformation to the method supported by the processor. The default processor for Active Storage is MiniMagick, but you can also use [Vips](https://www.rubydoc.info/gems/ruby-vips/Vips/Image).
554-
555-
When the browser hits the variant URL, Active Storage will lazily transform the
556-
original blob into the specified format and redirect to its new service
586+
To create a variation of an image, call [`variant`][] on the attachment. You
587+
can pass any transformation supported by the variant processor to the method.
588+
When the browser hits the variant URL, Active Storage will lazily transform
589+
the original blob into the specified format and redirect to its new service
557590
location.
558591

559592
```erb
560593
<%= image_tag user.avatar.variant(resize_to_limit: [100, 100]) %>
561594
```
562595

563-
To switch to the Vips processor, you would add the following to
564-
`config/application.rb`:
596+
The default processor for Active Storage is MiniMagick, but you can also use
597+
[Vips][]. To switch to Vips, add the following to `config/application.rb`:
565598

566599
```ruby
567-
# Use Vips for processing variants.
568600
config.active_storage.variant_processor = :vips
569601
```
570602

571603
[`variant`]: https://api.rubyonrails.org/classes/ActiveStorage/Blob/Representable.html#method-i-variant
604+
[Vips]: https://www.rubydoc.info/gems/ruby-vips/Vips/Image
572605

573-
Previewing Files
574-
----------------
606+
### Previewing Files
575607

576608
Some non-image files can be previewed: that is, they can be presented as images.
577609
For example, a video file can be previewed by extracting its first frame. Out of
578610
the box, Active Storage supports previewing videos and PDF documents. To create
579611
a link to a lazily-generated preview, use the attachment's [`preview`][] method:
580612

581613
```erb
582-
<ul>
583-
<% @message.files.each do |file| %>
584-
<li>
585-
<%= image_tag file.preview(resize_to_limit: [100, 100]) %>
586-
</li>
587-
<% end %>
588-
</ul>
614+
<%= image_tag message.video.preview(resize_to_limit: [100, 100]) %>
589615
```
590616

617+
To add support for another format, add your own previewer. See the
618+
[`ActiveStorage::Preview`][] documentation for more information.
619+
591620
WARNING: Extracting previews requires third-party applications: FFmpeg v3.4+ for
592621
video and muPDF for PDFs, and on macOS also XQuartz and Poppler.
593622
These libraries are not provided by Rails. You must install them yourself to
594623
use the built-in previewers. Before you install and use third-party software,
595624
make sure you understand the licensing implications of doing so.
596625

597626
[`preview`]: https://api.rubyonrails.org/classes/ActiveStorage/Blob/Representable.html#method-i-preview
627+
[`ActiveStorage::Preview`]: https://api.rubyonrails.org/classes/ActiveStorage/Preview.html
598628

599629
Direct Uploads
600630
--------------

0 commit comments

Comments
 (0)