paperclip-meta 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +7 -0
- data/README.rdoc +2 -1
- data/init.rb +4 -2
- data/lib/paperclip-meta.rb +2 -89
- data/lib/paperclip-meta/attachment.rb +99 -0
- data/lib/paperclip-meta/railtie.rb +24 -0
- data/lib/paperclip-meta/version.rb +1 -1
- data/paperclip-meta.gemspec +1 -0
- data/spec/paperclip-meta/paperclip-meta_spec.rb +23 -0
- data/spec/spec_helper.rb +1 -0
- metadata +28 -14
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.rdoc
CHANGED
data/init.rb
CHANGED
data/lib/paperclip-meta.rb
CHANGED
@@ -1,91 +1,4 @@
|
|
1
1
|
require "paperclip"
|
2
2
|
require "paperclip-meta/version"
|
3
|
-
|
4
|
-
|
5
|
-
module Meta
|
6
|
-
# TODO: Move most of paperclip-meta crap here.
|
7
|
-
end
|
8
|
-
|
9
|
-
# TODO: Make paperclip-meta less monkey-style.
|
10
|
-
class Attachment
|
11
|
-
alias :original_post_process_styles :post_process_styles
|
12
|
-
alias :original_save :save
|
13
|
-
|
14
|
-
# If attachment deleted - destroy meta data
|
15
|
-
def save
|
16
|
-
if (not @queued_for_delete.empty?) and @queued_for_write.empty?
|
17
|
-
instance_write(:meta, meta_encode({})) if instance.respond_to?(:"#{name}_meta=")
|
18
|
-
end
|
19
|
-
original_save
|
20
|
-
end
|
21
|
-
|
22
|
-
# If model has #{name}_meta column we getting sizes of processed
|
23
|
-
# thumbnails and saving it to #{name}_meta column.
|
24
|
-
def post_process_styles(*style_args)
|
25
|
-
# Check arity of :original_post_process_styles to maintain compatibility with
|
26
|
-
# Paperclip 2.3.9 and older.
|
27
|
-
method(:original_post_process_styles).arity == 0 ? original_post_process_styles : original_post_process_styles(*style_args)
|
28
|
-
|
29
|
-
if instance.respond_to?(:"#{name}_meta=")
|
30
|
-
meta = {}
|
31
|
-
|
32
|
-
@queued_for_write.each do |style, file|
|
33
|
-
begin
|
34
|
-
geo = Geometry.from_file file
|
35
|
-
meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) }
|
36
|
-
rescue NotIdentifiedByImageMagickError => e
|
37
|
-
meta[style] = {}
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
instance_write(:meta, meta_encode(meta))
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Define meta accessors methods
|
46
|
-
[:width, :height, :size].each do |meth|
|
47
|
-
define_method(meth) do |*args|
|
48
|
-
style = args.first || default_style
|
49
|
-
meta_read(style, meth)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns image dimesions ("WxH") for given style name. If style name not given,
|
54
|
-
# returns dimesions for default_style.
|
55
|
-
def image_size(style = default_style)
|
56
|
-
"#{width(style)}x#{height(style)}"
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
# Returns meta data for given style
|
61
|
-
def meta_read(style, item)
|
62
|
-
if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
|
63
|
-
if meta = meta_decode(instance_read(:meta))
|
64
|
-
meta.key?(style) ? meta[style][item] : nil
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# Return encoded metadata as String
|
70
|
-
def meta_encode(meta)
|
71
|
-
# Use Base64 class if aviliable, to prevent
|
72
|
-
# ActiveSupport deprecation warnings.
|
73
|
-
if Module.const_defined? "Base64"
|
74
|
-
::Base64.encode64(Marshal.dump(meta))
|
75
|
-
else
|
76
|
-
ActiveSupport::Base64.encode64(Marshal.dump(meta))
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Return decoded metadata as Object
|
81
|
-
def meta_decode(meta)
|
82
|
-
# Use Base64 class if aviliable, to prevent
|
83
|
-
# ActiveSupport deprecation warnings.
|
84
|
-
if Module.const_defined? "Base64"
|
85
|
-
Marshal.load(::Base64.decode64(meta))
|
86
|
-
else
|
87
|
-
Marshal.load(ActiveSupport::Base64.decode64(meta))
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
3
|
+
require 'paperclip-meta/railtie'
|
4
|
+
require 'paperclip-meta/attachment'
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Paperclip
|
2
|
+
module Meta
|
3
|
+
module Attachment
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send :include, InstanceMethods
|
7
|
+
base.alias_method_chain :save, :meta_data
|
8
|
+
base.alias_method_chain :post_process_styles, :meta_data
|
9
|
+
base.alias_method_chain :size, :meta_data
|
10
|
+
end
|
11
|
+
|
12
|
+
module InstanceMethods
|
13
|
+
|
14
|
+
def save_with_meta_data
|
15
|
+
if (not @queued_for_delete.empty?) and @queued_for_write.empty?
|
16
|
+
instance_write(:meta, meta_encode({})) if instance.respond_to?(:"#{name}_meta=")
|
17
|
+
end
|
18
|
+
save_without_meta_data
|
19
|
+
end
|
20
|
+
|
21
|
+
def post_process_styles_with_meta_data(*style_args)
|
22
|
+
post_process_styles_without_meta_data(*style_args)
|
23
|
+
|
24
|
+
if instance.respond_to?(:"#{name}_meta=")
|
25
|
+
meta = {}
|
26
|
+
@queued_for_write.each do |style, file|
|
27
|
+
begin
|
28
|
+
geo = Geometry.from_file file
|
29
|
+
meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) }
|
30
|
+
rescue NotIdentifiedByImageMagickError => e
|
31
|
+
meta[style] = {}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
unless meta == {}
|
36
|
+
instance.send("#{name}_meta=", meta_encode(meta))
|
37
|
+
instance.class.update_all({ "#{name}_meta" => meta_encode(meta) }, instance.class.primary_key => instance.id)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#Use meta info for style if required
|
43
|
+
def size_with_meta_data(passed_style = nil)
|
44
|
+
passed_style ? meta_read(passed_style, :size) : size_without_meta_data
|
45
|
+
end
|
46
|
+
|
47
|
+
# Define meta accessors methods
|
48
|
+
[:width, :height ].each do |meth|
|
49
|
+
define_method(meth) do |*args|
|
50
|
+
style = args.first || default_style
|
51
|
+
meta_read(style, meth)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns image dimesions ("WxH") for given style name. If style name not given,
|
56
|
+
# returns dimesions for default_style.
|
57
|
+
def image_size(style = default_style)
|
58
|
+
"#{width(style)}x#{height(style)}"
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
# Returns meta data for given style
|
64
|
+
def meta_read(style, item)
|
65
|
+
if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
|
66
|
+
if meta = meta_decode(instance_read(:meta))
|
67
|
+
meta.key?(style) ? meta[style][item] : nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Return encoded metadata as String
|
73
|
+
def meta_encode(meta)
|
74
|
+
# Use Base64 class if aviliable, to prevent
|
75
|
+
# ActiveSupport deprecation warnings.
|
76
|
+
if Module.const_defined? "Base64"
|
77
|
+
::Base64.encode64(Marshal.dump(meta))
|
78
|
+
else
|
79
|
+
ActiveSupport::Base64.encode64(Marshal.dump(meta))
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Return decoded metadata as Object
|
84
|
+
def meta_decode(meta)
|
85
|
+
# Use Base64 class if aviliable, to prevent
|
86
|
+
# ActiveSupport deprecation warnings.
|
87
|
+
if Module.const_defined? "Base64"
|
88
|
+
Marshal.load(::Base64.decode64(meta))
|
89
|
+
else
|
90
|
+
Marshal.load(ActiveSupport::Base64.decode64(meta))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'paperclip-meta'
|
2
|
+
|
3
|
+
module Paperclip
|
4
|
+
module Meta
|
5
|
+
|
6
|
+
if defined? Rails::Railtie
|
7
|
+
require 'rails'
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
initializer 'paperclip_meta.insert_into_active_record' do
|
10
|
+
ActiveSupport.on_load :active_record do
|
11
|
+
Paperclip::Meta::Railtie.insert
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Railtie
|
18
|
+
def self.insert
|
19
|
+
Paperclip::Attachment.send(:include, Paperclip::Meta::Attachment)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
data/paperclip-meta.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
# Development depensencies
|
22
22
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
23
|
+
s.add_development_dependency "rake"
|
23
24
|
s.add_development_dependency "paperclip"
|
24
25
|
s.add_development_dependency "rspec"
|
25
26
|
s.add_development_dependency "activerecord"
|
@@ -33,6 +33,28 @@ describe "Geometry saver plugin" do
|
|
33
33
|
img.big_image.height(:small).should == 100
|
34
34
|
end
|
35
35
|
|
36
|
+
describe 'file size' do
|
37
|
+
before do
|
38
|
+
@image = Image.new
|
39
|
+
@image.big_image = @big_image
|
40
|
+
@image.save!
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should save file size with meta data ' do
|
44
|
+
@image.big_image.size(:small).should == 3526
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should access normal paperclip method when no style passed' do
|
48
|
+
@image.big_image.should_receive(:size_without_meta_data).once.and_return(1234)
|
49
|
+
@image.big_image.size.should == 1234
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should have access to original file size' do
|
53
|
+
@image.big_image.size.should == 37042
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
36
58
|
it "clears geometry fields when image is destroyed" do
|
37
59
|
img = Image.new
|
38
60
|
img.small_image = @small_image
|
@@ -53,4 +75,5 @@ describe "Geometry saver plugin" do
|
|
53
75
|
lambda { img.save! }.should_not raise_error
|
54
76
|
img.small_image.width(:small).should be_nil
|
55
77
|
end
|
78
|
+
|
56
79
|
end
|
data/spec/spec_helper.rb
CHANGED
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.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-03-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70335412175260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: 1.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70335412175260
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70335412173700 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70335412173700
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: paperclip
|
27
|
-
requirement: &
|
38
|
+
requirement: &70335412172380 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70335412172380
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rspec
|
38
|
-
requirement: &
|
49
|
+
requirement: &70335412171460 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70335412171460
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: activerecord
|
49
|
-
requirement: &
|
60
|
+
requirement: &70335412170640 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,10 +65,10 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70335412170640
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: sqlite3-ruby
|
60
|
-
requirement: &
|
71
|
+
requirement: &70335412169360 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
@@ -65,10 +76,10 @@ dependencies:
|
|
65
76
|
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70335412169360
|
69
80
|
- !ruby/object:Gem::Dependency
|
70
81
|
name: paperclip
|
71
|
-
requirement: &
|
82
|
+
requirement: &70335412168500 !ruby/object:Gem::Requirement
|
72
83
|
none: false
|
73
84
|
requirements:
|
74
85
|
- - ! '>='
|
@@ -76,7 +87,7 @@ dependencies:
|
|
76
87
|
version: '0'
|
77
88
|
type: :runtime
|
78
89
|
prerelease: false
|
79
|
-
version_requirements: *
|
90
|
+
version_requirements: *70335412168500
|
80
91
|
description: Add width, height and size methods to paperclip thumbnails
|
81
92
|
email:
|
82
93
|
- y8@ya.ru
|
@@ -86,12 +97,15 @@ extra_rdoc_files: []
|
|
86
97
|
files:
|
87
98
|
- .gitignore
|
88
99
|
- .rspec
|
100
|
+
- .travis.yml
|
89
101
|
- Gemfile
|
90
102
|
- Gemfile.lock
|
91
103
|
- README.rdoc
|
92
104
|
- Rakefile
|
93
105
|
- init.rb
|
94
106
|
- lib/paperclip-meta.rb
|
107
|
+
- lib/paperclip-meta/attachment.rb
|
108
|
+
- lib/paperclip-meta/railtie.rb
|
95
109
|
- lib/paperclip-meta/version.rb
|
96
110
|
- paperclip-meta.gemspec
|
97
111
|
- spec/fixtures/big.jpg
|