ack-paperclip-meta 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +92 -0
- data/Rakefile +10 -0
- data/lib/ack-paperclip-meta.rb +1 -0
- data/lib/paperclip-meta.rb +1 -0
- data/lib/paperclip/meta.rb +3 -0
- data/lib/paperclip/meta/attachment.rb +111 -0
- data/lib/paperclip/meta/process_meta_service.rb +72 -0
- data/lib/paperclip/meta/railtie.rb +31 -0
- data/lib/paperclip/meta/version.rb +5 -0
- data/paperclip-meta.gemspec +32 -0
- data/release.sh +7 -0
- data/spec/attachment_spec.rb +144 -0
- data/spec/fixtures/big.jpg +0 -0
- data/spec/fixtures/big.zip +0 -0
- data/spec/fixtures/small.png +0 -0
- data/spec/gemfiles/Gemfile.paperclip-3 +17 -0
- data/spec/gemfiles/Gemfile.paperclip-4 +17 -0
- data/spec/schema.rb +15 -0
- data/spec/spec_helper.rb +53 -0
- data/tasks/reprocess_meta.rake +9 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7fe8ad4e4db2f3ec4b0e6b348a1d4fb748618cfe
|
4
|
+
data.tar.gz: 478098e9bbc9c0ddca769fff31bac6b682cf8ee4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3b6e9693b9ed8862c042ad4e163af51d40ffb8b37babfb35f53848cf081485dfa7eeb3fa5d5f242ce2bcaf7eb5029ed945fc9190fe62cd5267eaddd27255c9f
|
7
|
+
data.tar.gz: 25e0ce466a9340b6da949114817bbf2303af89c3e44b8742b3d6fd19120b39c761c628c3501dd1ebfcccfb94700598fa705307ebf6fcea575b5722b8a1f54e4a
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ack_paperclip-meta
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 ack
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Paperclip Meta
|
2
|
+
|
3
|
+
[](http://rubygems.org/gems/paperclip-meta)
|
4
|
+
[](https://travis-ci.org/teeparham/paperclip-meta)
|
5
|
+
|
6
|
+
Add width, height, and size to paperclip images.
|
7
|
+
|
8
|
+
Paperclip Meta gets image dimensions after `post_process_styles` using paperclip's `Geometry.from_file`.
|
9
|
+
|
10
|
+
Paperclip Meta works with paperclip versions 3.x and 4.x.
|
11
|
+
|
12
|
+
### Setup
|
13
|
+
|
14
|
+
Add paperclip-meta to Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'ack-paperclip-meta'
|
18
|
+
```
|
19
|
+
|
20
|
+
Create migration to add a *_meta column:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class AddAvatarMetaToUsers < ActiveRecord::Migration
|
24
|
+
def change
|
25
|
+
add_column :users, :avatar_meta, :text
|
26
|
+
end
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Rebuild all thumbnails to populate the meta column if you already have some attachments.
|
31
|
+
|
32
|
+
Now you can grab the size from the paperclip attachment:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
image_tag user.avatar.url, size: user.avatar.image_size
|
36
|
+
image_tag user.avatar.url(:medium), size: user.avatar.image_size(:medium)
|
37
|
+
image_tag user.avatar.url(:thumb), size: user.avatar.image_size(:thumb)
|
38
|
+
```
|
39
|
+
|
40
|
+
### Internals
|
41
|
+
|
42
|
+
The meta column is simple hash:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
style: {
|
46
|
+
width: 100,
|
47
|
+
height: 100,
|
48
|
+
size: 42000
|
49
|
+
}
|
50
|
+
```
|
51
|
+
|
52
|
+
This hash will be marshaled and base64 encoded before writing to model attribute.
|
53
|
+
|
54
|
+
`height`, `width`, and `image_size` methods are provided:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
user.avatar.width(:thumb)
|
58
|
+
=> 100
|
59
|
+
user.avatar.height(:medium)
|
60
|
+
=> 200
|
61
|
+
user.avatar.image_size
|
62
|
+
=> '60x70'
|
63
|
+
```
|
64
|
+
|
65
|
+
You can pass the image style to these methods. If a style is not passed, the default style will be used.
|
66
|
+
|
67
|
+
### Alternatives
|
68
|
+
|
69
|
+
https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions
|
70
|
+
|
71
|
+
### Development
|
72
|
+
|
73
|
+
Test:
|
74
|
+
|
75
|
+
```sh
|
76
|
+
bundle
|
77
|
+
rake
|
78
|
+
```
|
79
|
+
|
80
|
+
Test paperclip 3.x:
|
81
|
+
|
82
|
+
```sh
|
83
|
+
BUNDLE_GEMFILE=./spec/gemfiles/Gemfile.paperclip-3 bundle
|
84
|
+
BUNDLE_GEMFILE=./spec/gemfiles/Gemfile.paperclip-3 rake
|
85
|
+
```
|
86
|
+
|
87
|
+
Test paperclip 4.x:
|
88
|
+
|
89
|
+
```sh
|
90
|
+
BUNDLE_GEMFILE=./spec/gemfiles/Gemfile.paperclip-4 bundle
|
91
|
+
BUNDLE_GEMFILE=./spec/gemfiles/Gemfile.paperclip-4 rake
|
92
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'paperclip-meta'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'paperclip/meta'
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Meta
|
3
|
+
module Attachment
|
4
|
+
def self.included(base)
|
5
|
+
base.send :include, InstanceMethods
|
6
|
+
base.alias_method_chain :save, :meta_data
|
7
|
+
base.alias_method_chain :post_process_styles, :meta_data
|
8
|
+
base.alias_method_chain :size, :meta_data
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def save_with_meta_data
|
13
|
+
if @queued_for_delete and @queued_for_delete.any? and (@queued_for_write.nil? or @queued_for_write.empty?)
|
14
|
+
instance_write(:meta, meta_encode({}))
|
15
|
+
elsif !@queued_for_write.nil? and !@queued_for_write.empty?
|
16
|
+
post_process_styles_with_meta_data(@queued_for_write.keys.flatten)
|
17
|
+
end
|
18
|
+
save_without_meta_data
|
19
|
+
end
|
20
|
+
|
21
|
+
def post_process_styles_with_meta_data(*styles)
|
22
|
+
post_process_styles_without_meta_data(*styles)
|
23
|
+
return unless instance.respond_to?(:"#{name}_meta=")
|
24
|
+
|
25
|
+
process_meta_for_styles(@queued_for_write)
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_meta_for_styles(queue)
|
29
|
+
meta = populate_meta(queue)
|
30
|
+
return if meta == {}
|
31
|
+
|
32
|
+
write_meta(meta)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Use meta info for style if required
|
36
|
+
def size_with_meta_data(style = nil)
|
37
|
+
style ? read_meta(style, :size) : size_without_meta_data
|
38
|
+
end
|
39
|
+
|
40
|
+
def height(style = default_style)
|
41
|
+
read_meta style, :height
|
42
|
+
end
|
43
|
+
|
44
|
+
def width(style = default_style)
|
45
|
+
read_meta style, :width
|
46
|
+
end
|
47
|
+
|
48
|
+
# Return image dimesions ("WxH") for given style name. If style name not given,
|
49
|
+
# return dimesions for default_style.
|
50
|
+
def image_size(style = default_style)
|
51
|
+
w = width(style)
|
52
|
+
h = height(style)
|
53
|
+
"#{w}#{h && "x#{h}"}" if w || h
|
54
|
+
end
|
55
|
+
|
56
|
+
def meta_data
|
57
|
+
if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
|
58
|
+
meta_decode(instance_read(:meta)).freeze
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def populate_meta(queue)
|
65
|
+
meta = {}
|
66
|
+
queue.each do |style, file|
|
67
|
+
begin
|
68
|
+
geo = Geometry.from_file file
|
69
|
+
geo.auto_orient
|
70
|
+
meta[style] = { width: geo.width.to_i, height: geo.height.to_i, size: file.size }
|
71
|
+
rescue Paperclip::Errors::NotIdentifiedByImageMagickError
|
72
|
+
meta[style] = {}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
meta
|
76
|
+
end
|
77
|
+
|
78
|
+
def write_meta(meta)
|
79
|
+
merge_existing_meta_hash meta
|
80
|
+
instance.send("#{name}_meta=", meta_encode(meta))
|
81
|
+
end
|
82
|
+
|
83
|
+
# Return meta data for given style
|
84
|
+
def read_meta(style, item)
|
85
|
+
if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
|
86
|
+
if (meta = meta_decode(instance_read(:meta)))
|
87
|
+
meta[style] && meta[style][item]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Return encoded metadata as String
|
93
|
+
def meta_encode(meta)
|
94
|
+
Base64.encode64(Marshal.dump(meta))
|
95
|
+
end
|
96
|
+
|
97
|
+
# Return decoded metadata as Object
|
98
|
+
def meta_decode(meta)
|
99
|
+
Marshal.load(Base64.decode64(meta))
|
100
|
+
end
|
101
|
+
|
102
|
+
# Retain existing meta values that will not be recalculated when
|
103
|
+
# reprocessing a subset of styles
|
104
|
+
def merge_existing_meta_hash(meta)
|
105
|
+
return unless (original_meta = instance.send("#{name}_meta"))
|
106
|
+
meta.reverse_merge! meta_decode(original_meta)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Meta
|
3
|
+
|
4
|
+
class ProcessMetaService
|
5
|
+
|
6
|
+
def self.process!(logger = nil)
|
7
|
+
class_names = ENV['CLASSES'] || ENV['classes'] || ""
|
8
|
+
classes = class_names.split(',').map { |class_name| Paperclip.class_for(class_name) }
|
9
|
+
self.new(logger).process!(*classes)
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_accessor :logger
|
13
|
+
|
14
|
+
def initialize(logger = nil)
|
15
|
+
@logger = logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def process!(*classes)
|
19
|
+
classes.each do |klass|
|
20
|
+
process_class(klass)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def process_class(klass)
|
27
|
+
attachments = Paperclip::AttachmentRegistry.names_for(klass)
|
28
|
+
raise "Class #{klass.name} has no attachments specified" if attachments.empty?
|
29
|
+
|
30
|
+
klass.unscoped.each do |instance|
|
31
|
+
log("Processing image meta data for #{instance.class}.#{instance.id}")
|
32
|
+
attachments.each do |attachment_name|
|
33
|
+
next if instance.send(attachment_name).blank?
|
34
|
+
process_attachment(instance, attachment_name)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def process_attachment(instance, attachment_name)
|
40
|
+
meta_attribute_name = "#{attachment_name}_meta"
|
41
|
+
return unless instance.respond_to?(meta_attribute_name)
|
42
|
+
|
43
|
+
attachment = instance.send(attachment_name)
|
44
|
+
io_hash = attachment_styles_to_io_adapters(attachment)
|
45
|
+
attachment.process_meta_for_styles(io_hash)
|
46
|
+
if instance.respond_to?(:update_column)
|
47
|
+
instance.update_column(meta_attribute_name, instance.send(meta_attribute_name))
|
48
|
+
elsif instance.respond_to?(:update_attribute)
|
49
|
+
instance.update_attribute(meta_attribute_name, instance.send(meta_attribute_name))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def attachment_styles_to_io_adapters(attachment)
|
54
|
+
io_hash = attachment.styles.reduce({}) do |hash, (style_name, style)|
|
55
|
+
hash[style_name] = Paperclip.io_adapters.for(style)
|
56
|
+
hash
|
57
|
+
end
|
58
|
+
|
59
|
+
unless io_hash.has_key?(:original)
|
60
|
+
io_hash[:original] = Paperclip.io_adapters.for(attachment)
|
61
|
+
end
|
62
|
+
|
63
|
+
io_hash
|
64
|
+
end
|
65
|
+
|
66
|
+
def log(*args)
|
67
|
+
logger.info(*args) if logger
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'paperclip/meta'
|
2
|
+
|
3
|
+
module Paperclip
|
4
|
+
module Meta
|
5
|
+
|
6
|
+
if defined? ::Rails::Railtie
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
initializer :paperclip_meta do
|
9
|
+
ActiveSupport.on_load :active_record do
|
10
|
+
Paperclip::Meta::Railtie.insert
|
11
|
+
end
|
12
|
+
ActiveSupport.on_load :mongoid do
|
13
|
+
Paperclip::Meta::Railtie.insert
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
rake_tasks do
|
18
|
+
dir = File.join(File.dirname(__FILE__),'../../../tasks/*.rake')
|
19
|
+
Dir[dir].each { |f| load f }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Railtie
|
25
|
+
def self.insert
|
26
|
+
Paperclip::Attachment.send(:include, Paperclip::Meta::Attachment)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "paperclip/meta/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ack-paperclip-meta"
|
7
|
+
s.version = Paperclip::Meta::VERSION
|
8
|
+
s.authors = ["ack", "Alexey Bondar", "Tee Parham"]
|
9
|
+
s.email = ["i43ack@gmail.com", "y8@ya.ru", "tee@neighborland.com"]
|
10
|
+
s.homepage = "http://github.com/ack43/paperclip-meta"
|
11
|
+
s.summary = %q{Add width, height, and size to paperclip images}
|
12
|
+
s.description = %q{Add width, height and size methods to paperclip images}
|
13
|
+
s.license = "MIT"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
|
17
|
+
s.executables = []
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.required_ruby_version = ">= 1.9.3"
|
21
|
+
|
22
|
+
s.add_dependency "paperclip", ">= 3.0.2", "< 5.0"
|
23
|
+
|
24
|
+
s.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
s.add_development_dependency "rake", "~> 10.1"
|
26
|
+
s.add_development_dependency "mocha", "~> 1.0"
|
27
|
+
# s.add_development_dependency "activerecord", ">= 4.0"
|
28
|
+
|
29
|
+
# sqlite3 1.3.9 does not work with rubinius 2.2.5:
|
30
|
+
# https://github.com/sparklemotion/sqlite3-ruby/issues/122
|
31
|
+
# s.add_development_dependency "sqlite3", "1.3.8"
|
32
|
+
end
|
data/release.sh
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Attachment" do
|
4
|
+
it "saves image geometry for original image" do
|
5
|
+
img = Image.create(small_image: small_image)
|
6
|
+
img.reload
|
7
|
+
geometry = geometry_for(small_path)
|
8
|
+
assert_equal geometry.width, img.small_image.width
|
9
|
+
assert_equal geometry.height, img.small_image.height
|
10
|
+
assert_equal "50x64", img.small_image.image_size
|
11
|
+
end
|
12
|
+
|
13
|
+
it "saves geometry for styles" do
|
14
|
+
img = Image.create(small_image: small_image, big_image: big_image)
|
15
|
+
assert_equal 100, img.big_image.width(:thumb)
|
16
|
+
assert_equal 100, img.big_image.height(:thumb)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "saves original style geometry" do
|
20
|
+
img = Image.create(small_image: small_image)
|
21
|
+
assert_equal 50, img.small_image.width(:original)
|
22
|
+
assert_equal 64, img.small_image.height(:original)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "sets geometry on update" do
|
26
|
+
img = Image.create!
|
27
|
+
img.small_image = small_image
|
28
|
+
img.save
|
29
|
+
geometry = geometry_for(small_path)
|
30
|
+
assert_equal geometry.width, img.small_image.width
|
31
|
+
assert_equal geometry.height, img.small_image.height
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#size' do
|
35
|
+
before do
|
36
|
+
@image = Image.create(big_image: big_image)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should save file size with meta data ' do
|
40
|
+
path = File.join(File.dirname(__FILE__), "tmp/fixtures/tmp/thumb/#{@image.id}.jpg")
|
41
|
+
size = File.stat(path).size
|
42
|
+
assert_equal size, @image.big_image.size(:thumb)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should access normal paperclip method when no style passed' do
|
46
|
+
@image.big_image.expects size_without_meta_data: 1234
|
47
|
+
assert_equal 1234, @image.big_image.size
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should have access to original file size' do
|
51
|
+
assert_equal 37042, @image.big_image.size
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "clears geometry fields when image is destroyed" do
|
56
|
+
img = Image.create(small_image: small_image, big_image: big_image)
|
57
|
+
assert_equal 100, img.big_image.width(:thumb)
|
58
|
+
img.big_image = nil
|
59
|
+
img.save!
|
60
|
+
assert_nil img.big_image.width(:thumb)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "does not save when file is not an image" do
|
64
|
+
img = Image.new
|
65
|
+
img.small_image = not_image
|
66
|
+
refute img.save
|
67
|
+
assert_nil img.small_image.width
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns nil attributes when file is not an image" do
|
71
|
+
img = ImageWithNoValidation.new
|
72
|
+
img.small_image = not_image
|
73
|
+
img.save!
|
74
|
+
assert_nil img.small_image.width
|
75
|
+
assert_nil img.small_image.height
|
76
|
+
end
|
77
|
+
|
78
|
+
it "preserves metadata when reprocessing a specific style" do
|
79
|
+
img = Image.new
|
80
|
+
img.big_image = big_image
|
81
|
+
img.save!
|
82
|
+
assert_equal 500, img.big_image.width(:large)
|
83
|
+
img.big_image.reprocess!(:thumb)
|
84
|
+
assert_equal 500, img.big_image.width(:large)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "preserves metadata for unprocessed styles" do
|
88
|
+
img = Image.new
|
89
|
+
img.big_image = big_image
|
90
|
+
img.save!
|
91
|
+
|
92
|
+
# set big image meta to fake values for :large & missing :thumb
|
93
|
+
hash = { large: { height: 1, width: 2, size: 3 } }
|
94
|
+
img.update_column(:big_image_meta, img.big_image.send(:meta_encode, hash))
|
95
|
+
|
96
|
+
assert_equal 1, img.big_image.height(:large)
|
97
|
+
assert_equal 2, img.big_image.width(:large)
|
98
|
+
assert_equal 3, img.big_image.size(:large)
|
99
|
+
assert_nil img.big_image.height(:thumb)
|
100
|
+
assert_nil img.big_image.height(:original)
|
101
|
+
img.big_image.reprocess!(:thumb)
|
102
|
+
assert_equal 1, img.big_image.height(:large)
|
103
|
+
assert_equal 2, img.big_image.width(:large)
|
104
|
+
assert_equal 3, img.big_image.size(:large)
|
105
|
+
assert_equal 100, img.big_image.height(:thumb)
|
106
|
+
assert_equal 100, img.big_image.width(:thumb)
|
107
|
+
assert_equal 277, img.big_image.height(:original) # original is always reprocessed
|
108
|
+
end
|
109
|
+
|
110
|
+
it "replaces metadata when attachment changes" do
|
111
|
+
img = Image.new
|
112
|
+
img.big_image = big_image
|
113
|
+
img.save!
|
114
|
+
img.big_image = small_image
|
115
|
+
img.save!
|
116
|
+
assert_equal "50x64", img.big_image.image_size
|
117
|
+
assert_equal "100x100", img.big_image.image_size(:thumb)
|
118
|
+
assert_equal "500x500", img.big_image.image_size(:large)
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def small_path
|
124
|
+
File.join(File.dirname(__FILE__), 'fixtures', 'small.png')
|
125
|
+
end
|
126
|
+
|
127
|
+
# 50x64
|
128
|
+
def small_image
|
129
|
+
File.open(small_path)
|
130
|
+
end
|
131
|
+
|
132
|
+
def geometry_for(path)
|
133
|
+
Paperclip::Geometry.from_file(path)
|
134
|
+
end
|
135
|
+
|
136
|
+
# 600x277
|
137
|
+
def big_image
|
138
|
+
File.open(File.join(File.dirname(__FILE__), 'fixtures', 'big.jpg'))
|
139
|
+
end
|
140
|
+
|
141
|
+
def not_image
|
142
|
+
File.open(File.join(File.dirname(__FILE__), 'fixtures', 'big.zip'))
|
143
|
+
end
|
144
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
platforms :jruby do
|
4
|
+
gem 'activerecord-jdbcsqlite3-adapter'
|
5
|
+
end
|
6
|
+
|
7
|
+
platforms :rbx do
|
8
|
+
gem 'json'
|
9
|
+
gem 'rubysl', '~> 2.0'
|
10
|
+
end
|
11
|
+
|
12
|
+
# See paperclip-meta.gemspec
|
13
|
+
gemspec path: '../..'
|
14
|
+
|
15
|
+
gem 'activerecord', '~> 4.0.0'
|
16
|
+
gem 'minitest', '~> 4.7'
|
17
|
+
gem 'paperclip', '~> 3.5'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
platforms :jruby do
|
4
|
+
gem 'activerecord-jdbcsqlite3-adapter'
|
5
|
+
end
|
6
|
+
|
7
|
+
platforms :rbx do
|
8
|
+
gem 'json'
|
9
|
+
gem 'rubysl', '~> 2.0'
|
10
|
+
end
|
11
|
+
|
12
|
+
# See paperclip-meta.gemspec
|
13
|
+
gemspec path: '../..'
|
14
|
+
|
15
|
+
gem 'activerecord', '~> 4.1'
|
16
|
+
gem 'minitest', '~> 5.3'
|
17
|
+
gem 'paperclip', '~> 4.0'
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
create_table :images do |t|
|
3
|
+
t.string :small_image_file_name
|
4
|
+
t.string :small_image_content_type
|
5
|
+
t.integer :small_image_updated_at
|
6
|
+
t.integer :small_image_file_size
|
7
|
+
t.string :small_image_meta
|
8
|
+
|
9
|
+
t.string :big_image_file_name
|
10
|
+
t.string :big_image_content_type
|
11
|
+
t.integer :big_image_updated_at
|
12
|
+
t.integer :big_image_file_size
|
13
|
+
t.string :big_image_meta
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.require(:default)
|
3
|
+
require 'active_record'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'mocha/setup'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
adapter: "sqlite3",
|
9
|
+
database: ":memory:"
|
10
|
+
)
|
11
|
+
|
12
|
+
if ENV["VERBOSE"]
|
13
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
14
|
+
else
|
15
|
+
Paperclip.options[:log] = false
|
16
|
+
end
|
17
|
+
|
18
|
+
load(File.join(File.dirname(__FILE__), 'schema.rb'))
|
19
|
+
|
20
|
+
ActiveRecord::Base.send(:include, Paperclip::Glue)
|
21
|
+
Paperclip::Meta::Railtie.insert
|
22
|
+
|
23
|
+
I18n.enforce_available_locales = true
|
24
|
+
|
25
|
+
class Image < ActiveRecord::Base
|
26
|
+
has_attached_file :small_image,
|
27
|
+
storage: :filesystem,
|
28
|
+
path: "./spec/tmp/:style/:id.:extension",
|
29
|
+
url: "./spec/tmp/:style/:id.extension"
|
30
|
+
|
31
|
+
has_attached_file :big_image,
|
32
|
+
storage: :filesystem,
|
33
|
+
path: "./spec/tmp/fixtures/tmp/:style/:id.:extension",
|
34
|
+
url: "./spec/tmp/fixtures/tmp/:style/:id.extension",
|
35
|
+
styles: { thumb: "100x100#", large: "500x500#" }
|
36
|
+
|
37
|
+
# paperclip 4.0 requires a validator
|
38
|
+
validates_attachment_content_type :small_image, content_type: /\Aimage/
|
39
|
+
validates_attachment_content_type :big_image, content_type: /\Aimage/
|
40
|
+
end
|
41
|
+
|
42
|
+
class ImageWithNoValidation < ActiveRecord::Base
|
43
|
+
self.table_name = :images
|
44
|
+
|
45
|
+
has_attached_file :small_image,
|
46
|
+
storage: :filesystem,
|
47
|
+
path: "./spec/tmp/:style/:id.:extension",
|
48
|
+
url: "./spec/tmp/:style/:id.extension"
|
49
|
+
|
50
|
+
if Paperclip::VERSION >= "4.0"
|
51
|
+
do_not_validate_attachment_file_type :small_image
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'paperclip/meta/process_meta_service'
|
2
|
+
|
3
|
+
namespace :paperclip_meta do
|
4
|
+
|
5
|
+
desc 'Regenerate only the paperclip-meta without reprocessing the images themselves'
|
6
|
+
task :reprocess => :environment do
|
7
|
+
Paperclip::Meta::ProcessMetaService.process!(Rails.logger)
|
8
|
+
end
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ack-paperclip-meta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ack
|
8
|
+
- Alexey Bondar
|
9
|
+
- Tee Parham
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2015-04-25 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: paperclip
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">="
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.2
|
22
|
+
- - "<"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '5.0'
|
25
|
+
type: :runtime
|
26
|
+
prerelease: false
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 3.0.2
|
32
|
+
- - "<"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '5.0'
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
requirement: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.5'
|
42
|
+
type: :development
|
43
|
+
prerelease: false
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.5'
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rake
|
51
|
+
requirement: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '10.1'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '10.1'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: mocha
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '1.0'
|
77
|
+
description: Add width, height and size methods to paperclip images
|
78
|
+
email:
|
79
|
+
- i43ack@gmail.com
|
80
|
+
- y8@ya.ru
|
81
|
+
- tee@neighborland.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- ".gitignore"
|
87
|
+
- ".ruby-gemset"
|
88
|
+
- ".ruby-version"
|
89
|
+
- ".travis.yml"
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/ack-paperclip-meta.rb
|
95
|
+
- lib/paperclip-meta.rb
|
96
|
+
- lib/paperclip/meta.rb
|
97
|
+
- lib/paperclip/meta/attachment.rb
|
98
|
+
- lib/paperclip/meta/process_meta_service.rb
|
99
|
+
- lib/paperclip/meta/railtie.rb
|
100
|
+
- lib/paperclip/meta/version.rb
|
101
|
+
- paperclip-meta.gemspec
|
102
|
+
- release.sh
|
103
|
+
- spec/attachment_spec.rb
|
104
|
+
- spec/fixtures/big.jpg
|
105
|
+
- spec/fixtures/big.zip
|
106
|
+
- spec/fixtures/small.png
|
107
|
+
- spec/gemfiles/Gemfile.paperclip-3
|
108
|
+
- spec/gemfiles/Gemfile.paperclip-4
|
109
|
+
- spec/schema.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- tasks/reprocess_meta.rake
|
112
|
+
homepage: http://github.com/ack43/paperclip-meta
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.9.3
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.4.6
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Add width, height, and size to paperclip images
|
136
|
+
test_files: []
|