Skip to content

Commit db992b3

Browse files
authored
Merge pull request #396 from platanus/shrine-images
Shrine images
2 parents 7508859 + 98c8c00 commit db992b3

File tree

15 files changed

+277
-68
lines changed

15 files changed

+277
-68
lines changed

.circleci/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ commands:
3232
keys:
3333
- bundler-dependencies-{{ checksum "Gemfile.lock" }}
3434
- bundler-dependencies-
35+
- run:
36+
name: Install apt and vips buildpack dependencies
37+
command: |
38+
cd spec/dummy/
39+
xargs -a Aptfile sudo apt-get install
40+
sudo apt-get install libvips
3541
- run:
3642
name: Install bundle dependencies
3743
command: |

Gemfile.lock

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ GEM
9696
climate_control (0.2.0)
9797
coderay (1.1.2)
9898
concurrent-ruby (1.1.8)
99+
content_disposition (1.0.0)
99100
crass (1.0.6)
100101
database_cleaner (1.8.2)
101102
diff-lcs (1.3)
103+
down (5.2.4)
104+
addressable (~> 2.8)
102105
enumerize (2.3.1)
103106
activesupport (>= 3.2)
104107
erubi (1.10.0)
@@ -107,7 +110,7 @@ GEM
107110
factory_bot_rails (5.1.1)
108111
factory_bot (~> 5.1.0)
109112
railties (>= 4.2.0)
110-
ffi (1.12.2)
113+
ffi (1.15.5)
111114
formatador (0.2.5)
112115
formtastic (3.1.5)
113116
actionpack (>= 3.2.13)
@@ -133,6 +136,9 @@ GEM
133136
activesupport (>= 4.1)
134137
i18n (1.8.10)
135138
concurrent-ruby (~> 1.0)
139+
image_processing (1.12.1)
140+
mini_magick (>= 4.9.5, < 5)
141+
ruby-vips (>= 2.0.17, < 3)
136142
inherited_resources (1.11.0)
137143
actionpack (>= 5.0, < 6.1)
138144
has_scope (~> 0.6)
@@ -170,6 +176,7 @@ GEM
170176
mime-types (3.3.1)
171177
mime-types-data (~> 3.2015)
172178
mime-types-data (3.2019.1009)
179+
mini_magick (4.11.0)
173180
mini_mime (1.0.2)
174181
mini_portile2 (2.6.1)
175182
minitest (5.14.4)
@@ -280,6 +287,8 @@ GEM
280287
ruby-progressbar (~> 1.7)
281288
unicode-display_width (~> 1.4.0)
282289
ruby-progressbar (1.10.1)
290+
ruby-vips (2.1.4)
291+
ffi (~> 1.12)
283292
rubyzip (2.2.0)
284293
sassc (2.2.1)
285294
ffi (~> 1.9)
@@ -296,6 +305,9 @@ GEM
296305
shellany (0.0.1)
297306
shoulda-matchers (4.2.0)
298307
activesupport (>= 4.2.0)
308+
shrine (3.4.0)
309+
content_disposition (~> 1.0)
310+
down (~> 5.1)
299311
sprockets (4.0.0)
300312
concurrent-ruby (~> 1.0)
301313
rack (> 1, < 3)
@@ -343,6 +355,7 @@ DEPENDENCIES
343355
factory_bot_rails
344356
guard
345357
guard-rspec
358+
image_processing
346359
mimemagic!
347360
paperclip
348361
pry-rails
@@ -352,6 +365,7 @@ DEPENDENCIES
352365
rspec_junit_formatter
353366
rubocop (~> 0.65.0)
354367
shoulda-matchers
368+
shrine (~> 3.0)
355369
sqlite3
356370
webdrivers
357371
webpacker (~> 5.0)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ Displays a paperclip link with attachment related icon into index and show views
7878

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

81-
#### Paperclip Image
81+
#### Images
8282

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

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

87-
[Read more!](docs/paperclip_images.md)
87+
[Read more!](docs/images.md)
8888

8989
#### AASM Integration
9090

activeadmin_addons.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
3232
s.add_development_dependency "factory_bot_rails"
3333
s.add_development_dependency "guard"
3434
s.add_development_dependency "guard-rspec"
35+
s.add_development_dependency "image_processing"
3536
s.add_development_dependency "paperclip"
3637
s.add_development_dependency "pry-rails"
3738
s.add_development_dependency "puma"
@@ -40,6 +41,7 @@ Gem::Specification.new do |s|
4041
s.add_development_dependency "rspec_junit_formatter"
4142
s.add_development_dependency "rubocop", "~> 0.65.0"
4243
s.add_development_dependency "shoulda-matchers"
44+
s.add_development_dependency "shrine", "~> 3.0"
4345
s.add_development_dependency "sqlite3"
4446
s.add_development_dependency "webdrivers"
4547
end

docs/images.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Images Integration
2+
3+
## Paperclip
4+
5+
### Image Row
6+
7+
```ruby
8+
show do
9+
attributes_table do
10+
image_row :photo
11+
end
12+
end
13+
```
14+
15+
<img src="./images/paperclip-image-row.png" height="400" />
16+
17+
### Image Column
18+
19+
```ruby
20+
index do
21+
image_column :photo, style: :thumb
22+
end
23+
```
24+
25+
<img src="./images/paperclip-image-column.png" height="400" />
26+
27+
> You can pass `style` attribute matching paperclip's style definition
28+
29+
## Shrine
30+
31+
### Image Row
32+
33+
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.
34+
```ruby
35+
show do
36+
attributes_table do
37+
image_row :photo, image_options: { width: 400 }
38+
end
39+
end
40+
```
41+
42+
<img src="./images/paperclip-image-row.png" height="400" />
43+
44+
### Image Column
45+
46+
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.
47+
```ruby
48+
index do
49+
image_column :photo, style: :jpg_small
50+
end
51+
```
52+
53+
<img src="./images/paperclip-image-column.png" height="400" />

docs/paperclip_images.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/activeadmin_addons/addons/image_builder.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
require 'pry'
2+
13
module ActiveAdminAddons
24
class ImageBuilder < CustomBuilder
35
def render
46
return nil if data.nil?
5-
raise "you need to pass a paperclip image attribute" unless data.respond_to?(:url)
7+
8+
if Object.const_defined?('Paperclip::Attachment') && data.is_a?(Paperclip::Attachment)
9+
paperclip_data
10+
elsif Object.const_defined?('Shrine::UploadedFile') && data.is_a?(Shrine::UploadedFile)
11+
shrine_data
12+
else
13+
raise "you need to pass a paperclip or shrine image attribute"
14+
end
15+
end
16+
17+
private
18+
19+
def paperclip_data
620
style = options.fetch(:style, :original)
721
context.image_tag(data.url(style)) if data.file?
822
end
23+
24+
def shrine_data
25+
image_options = options[:image_options].presence || {}
26+
if options[:style] && derivatives.include?(options[:style])
27+
context.image_tag(model.send("#{attribute}_url", options[:style]), image_options)
28+
else
29+
context.image_tag(data.url, image_options)
30+
end
31+
end
32+
33+
def derivatives
34+
model.send("#{attribute}_derivatives")
35+
end
936
end
1037
end
1138

spec/dummy/Aptfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
libglib2.0-0
2+
libglib2.0-dev
3+
libpoppler-glib8
4+
libwebp-dev
5+
webp

spec/dummy/app/admin/invoices.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ActiveAdmin.register Invoice do
2-
permit_params :legal_date, :number, :paid, :state, :attachment, :photo, :category_id,
3-
:city_id, :amount, :color, :updated_at, :active, item_ids: [], other_item_ids: []
2+
permit_params :legal_date, :number, :paid, :state, :attachment, :photo, :category_id, :city_id,
3+
:amount, :color, :updated_at, :picture, :active, item_ids: [], other_item_ids: []
44

55
filter :id, as: :numeric_range_filter
66

@@ -17,6 +17,7 @@
1717
tag_column :state, interactive: true
1818
bool_column :paid
1919
image_column :photo, style: :thumb
20+
image_column :picture, style: :jpg_small
2021
attachment_column :attachment
2122
number_column :amount, as: :currency, unit: "$", separator: ","
2223
toggle_bool_column :active
@@ -35,6 +36,7 @@
3536
list_row :details, localize: true
3637
image_row("Mi foto", :photo, style: :big, &:photo)
3738
attachment_row("My doc", :attachment, label: 'Download file', truncate: false, &:attachment)
39+
image_row("Mi picture", :picture, image_options: { width: 100 }, &:picture)
3840
row :legal_date
3941
number_row("Monto", :amount, as: :human, &:amount)
4042
row :city
@@ -87,6 +89,8 @@
8789

8890
f.input :photo
8991

92+
f.input :picture, as: :file
93+
9094
f.input :color, as: :color_picker,
9195
palette: Invoice.colors
9296

spec/dummy/app/models/invoice.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Invoice < ActiveRecord::Base
66
extend ::Enumerize
77
include Paperclip::Glue
88
include AASM
9+
include ImageUploader::Attachment(:picture)
910

1011
belongs_to :category
1112
belongs_to :city

0 commit comments

Comments
 (0)