paperclip-meta 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -22
- data/Rakefile +3 -4
- data/lib/paperclip-meta/attachment.rb +8 -11
- data/lib/paperclip-meta/version.rb +1 -1
- data/paperclip-meta.gemspec +2 -2
- data/spec/paperclip-meta/paperclip-meta_spec.rb +48 -43
- data/spec/spec_helper.rb +6 -2
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aabd2e01ad47d7f22e378b79e409a9798a4778c9
|
4
|
+
data.tar.gz: 9ea6f76f9f3fcd8aec42555ece5570363d4f7f78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85a9971fd625ad0efc89959e6e04663dd1fabe2f079bfdaaa867913cac834a60a3609713da04d06ed464fecc5fe61528a6d00b4039a14232f522a4e8409e9d03
|
7
|
+
data.tar.gz: 880f048a350568ec742879385ea8c1559648c78cc74909a2124d510b2e1568edfd6475a9b59e75257649843af392330adc3f0cd201a39fa27ff07a32aa3d19c9
|
data/README.md
CHANGED
@@ -5,8 +5,7 @@
|
|
5
5
|
|
6
6
|
Add width, height, and size to paperclip images.
|
7
7
|
|
8
|
-
Paperclip Meta gets image dimensions after post_process_styles using
|
9
|
-
This should make paperclip-meta storage independent.
|
8
|
+
Paperclip Meta gets image dimensions after `post_process_styles` using paperclip's `Geometry.from_file`.
|
10
9
|
|
11
10
|
### Setup
|
12
11
|
|
@@ -19,25 +18,21 @@ gem 'paperclip-meta'
|
|
19
18
|
Create migration to add a *_meta column:
|
20
19
|
|
21
20
|
```ruby
|
22
|
-
class
|
23
|
-
def
|
21
|
+
class AddAvatarMetaToUsers < ActiveRecord::Migration
|
22
|
+
def change
|
24
23
|
add_column :users, :avatar_meta, :text
|
25
24
|
end
|
26
|
-
|
27
|
-
def self.down
|
28
|
-
remove_column :users, :avatar_meta
|
29
|
-
end
|
30
25
|
end
|
31
26
|
```
|
32
27
|
|
33
|
-
Rebuild all thumbnails to
|
28
|
+
Rebuild all thumbnails to populate the meta column if you already have some attachments.
|
34
29
|
|
35
|
-
Now you can
|
30
|
+
Now you can grab the size from the paperclip attachment:
|
36
31
|
|
37
32
|
```
|
38
|
-
|
39
|
-
|
40
|
-
|
33
|
+
<%= image_tag user.avatar.url, size: user.avatar.image_size %>
|
34
|
+
<%= image_tag user.avatar.url(:medium), size: user.avatar.image_size(:medium) %>
|
35
|
+
<%= image_tag user.avatar.url(:thumb), size: user.avatar.image_size(:thumb) %>
|
41
36
|
```
|
42
37
|
|
43
38
|
### Internals
|
@@ -45,18 +40,28 @@ Now you can use meta-magic:
|
|
45
40
|
The meta column is simple hash:
|
46
41
|
|
47
42
|
```ruby
|
48
|
-
:
|
49
|
-
:
|
50
|
-
:
|
51
|
-
:
|
43
|
+
style: {
|
44
|
+
width: 100,
|
45
|
+
height: 100,
|
46
|
+
size: 42000
|
52
47
|
}
|
53
48
|
```
|
54
49
|
|
55
50
|
This hash will be marshaled and base64 encoded before writing to model attribute.
|
56
51
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
52
|
+
`height`, `width`, and `size` methods are provided:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
user.avatar.width(:thumb)
|
56
|
+
=> 100
|
57
|
+
user.avatar.height(:medium)
|
58
|
+
=> 200
|
59
|
+
user.avatar.size
|
60
|
+
=> '60x70'
|
61
|
+
```
|
62
|
+
|
63
|
+
You can pass the image style to these methods. If a style is not passed, the default style will be used.
|
64
|
+
|
65
|
+
### Alternatives
|
61
66
|
|
62
|
-
|
67
|
+
https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions
|
data/Rakefile
CHANGED
@@ -10,7 +10,7 @@ module Paperclip
|
|
10
10
|
|
11
11
|
module InstanceMethods
|
12
12
|
def save_with_meta_data
|
13
|
-
if @queued_for_delete.any? && @queued_for_write.empty?
|
13
|
+
if @queued_for_delete.any? && @queued_for_write.empty?
|
14
14
|
instance_write(:meta, meta_encode({}))
|
15
15
|
end
|
16
16
|
save_without_meta_data
|
@@ -24,7 +24,7 @@ module Paperclip
|
|
24
24
|
@queued_for_write.each do |style, file|
|
25
25
|
begin
|
26
26
|
geo = Geometry.from_file file
|
27
|
-
meta[style] = {:
|
27
|
+
meta[style] = { width: geo.width.to_i, height: geo.height.to_i, size: file.size }
|
28
28
|
rescue Paperclip::Errors::NotIdentifiedByImageMagickError
|
29
29
|
meta[style] = {}
|
30
30
|
end
|
@@ -33,22 +33,19 @@ module Paperclip
|
|
33
33
|
return if meta == {}
|
34
34
|
|
35
35
|
instance.send("#{name}_meta=", meta_encode(meta))
|
36
|
-
instance.class
|
37
|
-
.where(instance.class.primary_key => instance.id)
|
38
|
-
.update_all("#{name}_meta" => meta_encode(meta))
|
39
36
|
end
|
40
37
|
|
41
38
|
# Use meta info for style if required
|
42
|
-
def size_with_meta_data(
|
43
|
-
|
39
|
+
def size_with_meta_data(style = nil)
|
40
|
+
style ? meta_read(style, :size) : size_without_meta_data
|
44
41
|
end
|
45
42
|
|
46
|
-
def height(
|
47
|
-
meta_read
|
43
|
+
def height(style = default_style)
|
44
|
+
meta_read style, :height
|
48
45
|
end
|
49
46
|
|
50
|
-
def width(
|
51
|
-
meta_read
|
47
|
+
def width(style = default_style)
|
48
|
+
meta_read style, :width
|
52
49
|
end
|
53
50
|
|
54
51
|
# Return image dimesions ("WxH") for given style name. If style name not given,
|
data/paperclip-meta.gemspec
CHANGED
@@ -21,8 +21,8 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.add_dependency "paperclip", "~> 3.0"
|
22
22
|
|
23
23
|
s.add_development_dependency "bundler", "~> 1.5"
|
24
|
-
s.add_development_dependency "rake"
|
25
|
-
s.add_development_dependency "rspec"
|
24
|
+
s.add_development_dependency "rake", "~> 10.1"
|
25
|
+
s.add_development_dependency "rspec", "~> 2.14"
|
26
26
|
s.add_development_dependency "activerecord", "~> 4.0"
|
27
27
|
s.add_development_dependency "sqlite3", "~> 1.3"
|
28
28
|
end
|
@@ -1,49 +1,38 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
4
|
-
before(:each) do
|
5
|
-
small_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'small.png')
|
6
|
-
big_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.jpg')
|
7
|
-
not_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.zip')
|
8
|
-
@big_image = File.open(big_path)
|
9
|
-
@small_image = File.open(small_path)
|
10
|
-
@big_size = Paperclip::Geometry.from_file(big_path)
|
11
|
-
@small_size = Paperclip::Geometry.from_file(small_path)
|
12
|
-
@not_image = File.open(not_path)
|
13
|
-
end
|
14
|
-
|
3
|
+
describe "Attachment" do
|
15
4
|
it "saves image geometry for original image" do
|
16
|
-
img = Image.
|
17
|
-
img.
|
18
|
-
|
19
|
-
|
20
|
-
img.
|
21
|
-
|
22
|
-
img.small_image.width eq(@small_size.width)
|
23
|
-
img.small_image.height eq(@small_size.height)
|
5
|
+
img = Image.create(small_image: small_image)
|
6
|
+
img.reload
|
7
|
+
geometry = geometry_for(small_path)
|
8
|
+
img.small_image.width.should == geometry.width
|
9
|
+
img.small_image.height.should == geometry.height
|
24
10
|
end
|
25
11
|
|
26
12
|
it "saves geometry for styles" do
|
27
|
-
img = Image.
|
28
|
-
img.
|
29
|
-
img.big_image
|
30
|
-
|
13
|
+
img = Image.create(small_image: small_image, big_image: big_image)
|
14
|
+
img.big_image.width(:thumb).should == 100
|
15
|
+
img.big_image.height(:thumb).should == 100
|
16
|
+
end
|
31
17
|
|
32
|
-
|
33
|
-
img
|
18
|
+
it "sets geometry on update" do
|
19
|
+
img = Image.create!
|
20
|
+
img.small_image = small_image
|
21
|
+
img.save
|
22
|
+
geometry = geometry_for(small_path)
|
23
|
+
img.small_image.width.should == geometry.width
|
24
|
+
img.small_image.height.should == geometry.height
|
34
25
|
end
|
35
26
|
|
36
27
|
describe 'file size' do
|
37
28
|
before do
|
38
|
-
@image = Image.
|
39
|
-
@image.big_image = @big_image
|
40
|
-
@image.save!
|
29
|
+
@image = Image.create(big_image: big_image)
|
41
30
|
end
|
42
31
|
|
43
32
|
it 'should save file size with meta data ' do
|
44
|
-
path = File.join(File.dirname(__FILE__), "../tmp/fixtures/tmp/
|
33
|
+
path = File.join(File.dirname(__FILE__), "../tmp/fixtures/tmp/thumb/#{@image.id}.jpg")
|
45
34
|
size = File.stat(path).size
|
46
|
-
@image.big_image.size(:
|
35
|
+
@image.big_image.size(:thumb).should == size
|
47
36
|
end
|
48
37
|
|
49
38
|
it 'should access normal paperclip method when no style passed' do
|
@@ -54,28 +43,44 @@ describe "Geometry saver plugin" do
|
|
54
43
|
it 'should have access to original file size' do
|
55
44
|
@image.big_image.size.should == 37042
|
56
45
|
end
|
57
|
-
|
58
46
|
end
|
59
47
|
|
60
48
|
it "clears geometry fields when image is destroyed" do
|
61
|
-
img = Image.
|
62
|
-
img.
|
63
|
-
img.big_image = @big_image
|
64
|
-
img.save!
|
65
|
-
|
66
|
-
img.big_image.width(:small).should == 100
|
49
|
+
img = Image.create(small_image: small_image, big_image: big_image)
|
50
|
+
img.big_image.width(:thumb).should == 100
|
67
51
|
|
68
52
|
img.big_image = nil
|
69
53
|
img.save!
|
70
54
|
|
71
|
-
img.big_image.width(:
|
55
|
+
img.big_image.width(:thumb).should be_nil
|
72
56
|
end
|
73
57
|
|
74
58
|
it "does not fails when file is not an image" do
|
75
59
|
img = Image.new
|
76
|
-
img.small_image =
|
77
|
-
|
78
|
-
img.small_image.width(:
|
60
|
+
img.small_image = not_image
|
61
|
+
-> { img.save! }.should_not raise_error
|
62
|
+
img.small_image.width(:thumb).should be_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def small_path
|
68
|
+
File.join(File.dirname(__FILE__), '..', 'fixtures', 'small.png')
|
69
|
+
end
|
70
|
+
|
71
|
+
def small_image
|
72
|
+
File.open(small_path)
|
79
73
|
end
|
80
74
|
|
81
|
-
|
75
|
+
def geometry_for(path)
|
76
|
+
Paperclip::Geometry.from_file(path)
|
77
|
+
end
|
78
|
+
|
79
|
+
def big_image
|
80
|
+
File.open(File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.jpg'))
|
81
|
+
end
|
82
|
+
|
83
|
+
def not_image
|
84
|
+
File.open(File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.zip'))
|
85
|
+
end
|
86
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -7,7 +7,11 @@ ActiveRecord::Base.establish_connection(
|
|
7
7
|
database: ":memory:"
|
8
8
|
)
|
9
9
|
|
10
|
-
|
10
|
+
if ENV["VERBOSE"]
|
11
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
12
|
+
else
|
13
|
+
Paperclip.options[:log] = false
|
14
|
+
end
|
11
15
|
|
12
16
|
load(File.join(File.dirname(__FILE__), 'schema.rb'))
|
13
17
|
|
@@ -24,5 +28,5 @@ class Image < ActiveRecord::Base
|
|
24
28
|
:storage => :filesystem,
|
25
29
|
:path => "./spec/tmp/fixtures/tmp/:style/:id.:extension",
|
26
30
|
:url => "./spec/tmp/fixtures/tmp/:style/:id.extension",
|
27
|
-
:styles => { :
|
31
|
+
:styles => { :thumb => "100x100#" }
|
28
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paperclip-meta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexey Bondar
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: paperclip
|
@@ -43,30 +43,30 @@ dependencies:
|
|
43
43
|
name: rake
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - "~>"
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: '
|
48
|
+
version: '10.1'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- - "
|
53
|
+
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: '
|
55
|
+
version: '10.1'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: rspec
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- - "
|
60
|
+
- - "~>"
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: '
|
62
|
+
version: '2.14'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- - "
|
67
|
+
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: '
|
69
|
+
version: '2.14'
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: activerecord
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|