-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #396 from platanus/shrine-images
Shrine images
- Loading branch information
Showing
15 changed files
with
277 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Images Integration | ||
|
||
## Paperclip | ||
|
||
### Image Row | ||
|
||
```ruby | ||
show do | ||
attributes_table do | ||
image_row :photo | ||
end | ||
end | ||
``` | ||
|
||
<img src="./images/paperclip-image-row.png" height="400" /> | ||
|
||
### Image Column | ||
|
||
```ruby | ||
index do | ||
image_column :photo, style: :thumb | ||
end | ||
``` | ||
|
||
<img src="./images/paperclip-image-column.png" height="400" /> | ||
|
||
> You can pass `style` attribute matching paperclip's style definition | ||
## Shrine | ||
|
||
### Image Row | ||
|
||
The Shrine implementation receives an optional `image_options` which is then used as the options for the [`image_tag`](https://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag) method. | ||
```ruby | ||
show do | ||
attributes_table do | ||
image_row :photo, image_options: { width: 400 } | ||
end | ||
end | ||
``` | ||
|
||
<img src="./images/paperclip-image-row.png" height="400" /> | ||
|
||
### Image Column | ||
|
||
To use the Shrine [derivatives](https://shrinerb.com/docs/plugins/derivatives) you can use the `style` option with the name of the derivative like the example below. In this case you would need to have a derivative created with the `'jpg_small'` name. | ||
```ruby | ||
index do | ||
image_column :photo, style: :jpg_small | ||
end | ||
``` | ||
|
||
<img src="./images/paperclip-image-column.png" height="400" /> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
libglib2.0-0 | ||
libglib2.0-dev | ||
libpoppler-glib8 | ||
libwebp-dev | ||
webp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'image_processing/vips' | ||
|
||
class ImageUploader < Shrine | ||
DERIVATIVE_SIZES = { | ||
jpg_small: { size: [100, 100], type: 'jpg' } | ||
} | ||
|
||
Attacher.derivatives do |original| | ||
vips = ImageProcessing::Vips.source(original) | ||
|
||
DERIVATIVE_SIZES.reduce({}) do |derivatives_hash, (name, derivative_info)| | ||
derivatives_hash[name] = vips.convert(derivative_info[:type]).resize_to_limit!( | ||
*derivative_info[:size] | ||
) | ||
derivatives_hash | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'shrine' | ||
|
||
if Rails.env.development? | ||
require 'shrine/storage/file_system' | ||
|
||
Shrine.storages = { | ||
cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'), | ||
store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads') | ||
} | ||
else | ||
require 'shrine/storage/memory' | ||
|
||
Shrine.storages = { | ||
cache: Shrine::Storage::Memory.new, | ||
store: Shrine::Storage::Memory.new | ||
} | ||
end | ||
|
||
Shrine.plugin :activerecord | ||
Shrine.plugin :cached_attachment_data | ||
Shrine.plugin :restore_cached_data | ||
Shrine.plugin :derivatives |
5 changes: 5 additions & 0 deletions
5
spec/dummy/db/migrate/20220401181023_add_picture_data_to_invoice.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddPictureDataToInvoice < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :invoices, :picture_data, :text | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.