Skip to content

Commit

Permalink
Merge pull request #396 from platanus/shrine-images
Browse files Browse the repository at this point in the history
Shrine images
  • Loading branch information
GabrielLyonB authored Apr 8, 2022
2 parents 7508859 + 98c8c00 commit db992b3
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 68 deletions.
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ commands:
keys:
- bundler-dependencies-{{ checksum "Gemfile.lock" }}
- bundler-dependencies-
- run:
name: Install apt and vips buildpack dependencies
command: |
cd spec/dummy/
xargs -a Aptfile sudo apt-get install
sudo apt-get install libvips
- run:
name: Install bundle dependencies
command: |
Expand Down
16 changes: 15 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ GEM
climate_control (0.2.0)
coderay (1.1.2)
concurrent-ruby (1.1.8)
content_disposition (1.0.0)
crass (1.0.6)
database_cleaner (1.8.2)
diff-lcs (1.3)
down (5.2.4)
addressable (~> 2.8)
enumerize (2.3.1)
activesupport (>= 3.2)
erubi (1.10.0)
Expand All @@ -107,7 +110,7 @@ GEM
factory_bot_rails (5.1.1)
factory_bot (~> 5.1.0)
railties (>= 4.2.0)
ffi (1.12.2)
ffi (1.15.5)
formatador (0.2.5)
formtastic (3.1.5)
actionpack (>= 3.2.13)
Expand All @@ -133,6 +136,9 @@ GEM
activesupport (>= 4.1)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
image_processing (1.12.1)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
inherited_resources (1.11.0)
actionpack (>= 5.0, < 6.1)
has_scope (~> 0.6)
Expand Down Expand Up @@ -170,6 +176,7 @@ GEM
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2019.1009)
mini_magick (4.11.0)
mini_mime (1.0.2)
mini_portile2 (2.6.1)
minitest (5.14.4)
Expand Down Expand Up @@ -280,6 +287,8 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.4.0)
ruby-progressbar (1.10.1)
ruby-vips (2.1.4)
ffi (~> 1.12)
rubyzip (2.2.0)
sassc (2.2.1)
ffi (~> 1.9)
Expand All @@ -296,6 +305,9 @@ GEM
shellany (0.0.1)
shoulda-matchers (4.2.0)
activesupport (>= 4.2.0)
shrine (3.4.0)
content_disposition (~> 1.0)
down (~> 5.1)
sprockets (4.0.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -343,6 +355,7 @@ DEPENDENCIES
factory_bot_rails
guard
guard-rspec
image_processing
mimemagic!
paperclip
pry-rails
Expand All @@ -352,6 +365,7 @@ DEPENDENCIES
rspec_junit_formatter
rubocop (~> 0.65.0)
shoulda-matchers
shrine (~> 3.0)
sqlite3
webdrivers
webpacker (~> 5.0)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ Displays a paperclip link with attachment related icon into index and show views

[Read more!](docs/paperclip_attachment.md)

#### Paperclip Image
#### Images

Displays a paperclip image into index and show views
Display images in the index and show views. This implementation supports [Shrine](https://github.com/shrinerb/shrine) and [Paperclip](https://github.com/thoughtbot/paperclip).

<img src="./docs/images/paperclip-image-column.png" height="380" />

[Read more!](docs/paperclip_images.md)
[Read more!](docs/images.md)

#### AASM Integration

Expand Down
2 changes: 2 additions & 0 deletions activeadmin_addons.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "factory_bot_rails"
s.add_development_dependency "guard"
s.add_development_dependency "guard-rspec"
s.add_development_dependency "image_processing"
s.add_development_dependency "paperclip"
s.add_development_dependency "pry-rails"
s.add_development_dependency "puma"
Expand All @@ -40,6 +41,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec_junit_formatter"
s.add_development_dependency "rubocop", "~> 0.65.0"
s.add_development_dependency "shoulda-matchers"
s.add_development_dependency "shrine", "~> 3.0"
s.add_development_dependency "sqlite3"
s.add_development_dependency "webdrivers"
end
53 changes: 53 additions & 0 deletions docs/images.md
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" />
25 changes: 0 additions & 25 deletions docs/paperclip_images.md

This file was deleted.

29 changes: 28 additions & 1 deletion lib/activeadmin_addons/addons/image_builder.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
require 'pry'

module ActiveAdminAddons
class ImageBuilder < CustomBuilder
def render
return nil if data.nil?
raise "you need to pass a paperclip image attribute" unless data.respond_to?(:url)

if Object.const_defined?('Paperclip::Attachment') && data.is_a?(Paperclip::Attachment)
paperclip_data
elsif Object.const_defined?('Shrine::UploadedFile') && data.is_a?(Shrine::UploadedFile)
shrine_data
else
raise "you need to pass a paperclip or shrine image attribute"
end
end

private

def paperclip_data
style = options.fetch(:style, :original)
context.image_tag(data.url(style)) if data.file?
end

def shrine_data
image_options = options[:image_options].presence || {}
if options[:style] && derivatives.include?(options[:style])
context.image_tag(model.send("#{attribute}_url", options[:style]), image_options)
else
context.image_tag(data.url, image_options)
end
end

def derivatives
model.send("#{attribute}_derivatives")
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/Aptfile
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
8 changes: 6 additions & 2 deletions spec/dummy/app/admin/invoices.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ActiveAdmin.register Invoice do
permit_params :legal_date, :number, :paid, :state, :attachment, :photo, :category_id,
:city_id, :amount, :color, :updated_at, :active, item_ids: [], other_item_ids: []
permit_params :legal_date, :number, :paid, :state, :attachment, :photo, :category_id, :city_id,
:amount, :color, :updated_at, :picture, :active, item_ids: [], other_item_ids: []

filter :id, as: :numeric_range_filter

Expand All @@ -17,6 +17,7 @@
tag_column :state, interactive: true
bool_column :paid
image_column :photo, style: :thumb
image_column :picture, style: :jpg_small
attachment_column :attachment
number_column :amount, as: :currency, unit: "$", separator: ","
toggle_bool_column :active
Expand All @@ -35,6 +36,7 @@
list_row :details, localize: true
image_row("Mi foto", :photo, style: :big, &:photo)
attachment_row("My doc", :attachment, label: 'Download file', truncate: false, &:attachment)
image_row("Mi picture", :picture, image_options: { width: 100 }, &:picture)
row :legal_date
number_row("Monto", :amount, as: :human, &:amount)
row :city
Expand Down Expand Up @@ -87,6 +89,8 @@

f.input :photo

f.input :picture, as: :file

f.input :color, as: :color_picker,
palette: Invoice.colors

Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Invoice < ActiveRecord::Base
extend ::Enumerize
include Paperclip::Glue
include AASM
include ImageUploader::Attachment(:picture)

belongs_to :category
belongs_to :city
Expand Down
18 changes: 18 additions & 0 deletions spec/dummy/app/uploaders/image_uploader.rb
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
22 changes: 22 additions & 0 deletions spec/dummy/config/initializers/shrine.rb
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
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
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_02_28_122115) do
ActiveRecord::Schema.define(version: 2022_04_01_181023) do

create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
Expand Down Expand Up @@ -93,6 +93,7 @@
t.string "aasm_state"
t.boolean "active", default: true
t.string "shipping_status"
t.text "picture_data"
t.index ["category_id"], name: "index_invoices_on_category_id"
t.index ["city_id"], name: "index_invoices_on_city_id"
end
Expand Down
Loading

0 comments on commit db992b3

Please sign in to comment.