paperclip-meta 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09bf77a5e70ee81ee3351672d693240dbd66f77a
4
- data.tar.gz: 9c39a349cbd84204781a34f2543233cc87237c2f
3
+ metadata.gz: 87d602bc919395b882cfca317ccfb2ab02b69e98
4
+ data.tar.gz: c47a819d68995c5a2023bf4c6c68ad2a821a9713
5
5
  SHA512:
6
- metadata.gz: d772a6566f2aad7f0fe991f5a33dd5dfeea6bdeae9dd8153f450719b3965e1e958b92ff9f0f952dab0ffde8ccd0bf1d8b55c38eb667bac1dead97d07857a5e2f
7
- data.tar.gz: 3cdf009ec88e3495c2bb82ce0f446648fb2345c15b8435f443abac7e339531ea560404b89fa7e76fca409b6449a05f11710733fd82a2b9b707e6a12eb81993e3
6
+ metadata.gz: 003cca26c110f4b95373db5500d0257523d0006d1508b880ec063d1590f69bead4b3ad326805a1e5bb0507101db9ad63b891f1458a182c234ee626e1988becdb
7
+ data.tar.gz: c4a1a5c6778cbf778614542641c5382473f17a861eb719d0bf3c580e4e08c57fe09daf3c9c990b8437966b67dd4f8b585a69da718d70825ce68828318ebd0383
@@ -1 +1 @@
1
- ruby-2.1.0
1
+ ruby-2.1.1
@@ -1,8 +1,11 @@
1
1
  rvm:
2
- - 1.9.3
2
+ - 2.1
3
3
  - 2.0.0
4
- - 2.1.0
5
- - rbx
4
+ - 1.9.3
5
+ - rbx-2
6
6
  gemfile:
7
- - spec/gemfiles/Gemfile.paperclip-3
8
7
  - spec/gemfiles/Gemfile.paperclip-4
8
+ - spec/gemfiles/Gemfile.paperclip-3
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: rbx-2
data/Gemfile CHANGED
@@ -4,11 +4,6 @@ platforms :jruby do
4
4
  gem 'activerecord-jdbcsqlite3-adapter'
5
5
  end
6
6
 
7
- platforms :rbx do
8
- gem 'json'
9
- gem 'rubysl', '~> 2.0'
10
- end
11
-
12
7
  # See paperclip-meta.gemspec
13
8
  gemspec
14
9
 
@@ -16,36 +16,27 @@ module Paperclip
16
16
  save_without_meta_data
17
17
  end
18
18
 
19
- def post_process_styles_with_meta_data(*style_args)
20
- post_process_styles_without_meta_data(*style_args)
19
+ def post_process_styles_with_meta_data(*styles)
20
+ post_process_styles_without_meta_data(*styles)
21
21
  return unless instance.respond_to?(:"#{name}_meta=")
22
22
 
23
- meta = {}
24
- @queued_for_write.each do |style, file|
25
- begin
26
- geo = Geometry.from_file file
27
- meta[style] = { width: geo.width.to_i, height: geo.height.to_i, size: file.size }
28
- rescue Paperclip::Errors::NotIdentifiedByImageMagickError
29
- meta[style] = {}
30
- end
31
- end
32
-
23
+ meta = populate_meta(@queued_for_write)
33
24
  return if meta == {}
34
25
 
35
- instance.send("#{name}_meta=", meta_encode(meta))
26
+ write_meta(meta)
36
27
  end
37
28
 
38
29
  # Use meta info for style if required
39
30
  def size_with_meta_data(style = nil)
40
- style ? meta_read(style, :size) : size_without_meta_data
31
+ style ? read_meta(style, :size) : size_without_meta_data
41
32
  end
42
33
 
43
34
  def height(style = default_style)
44
- meta_read style, :height
35
+ read_meta style, :height
45
36
  end
46
37
 
47
38
  def width(style = default_style)
48
- meta_read style, :width
39
+ read_meta style, :width
49
40
  end
50
41
 
51
42
  # Return image dimesions ("WxH") for given style name. If style name not given,
@@ -56,11 +47,29 @@ module Paperclip
56
47
 
57
48
  private
58
49
 
50
+ def populate_meta(queue)
51
+ meta = {}
52
+ queue.each do |style, file|
53
+ begin
54
+ geo = Geometry.from_file file
55
+ meta[style] = { width: geo.width.to_i, height: geo.height.to_i, size: file.size }
56
+ rescue Paperclip::Errors::NotIdentifiedByImageMagickError
57
+ meta[style] = {}
58
+ end
59
+ end
60
+ meta
61
+ end
62
+
63
+ def write_meta(meta)
64
+ merge_existing_meta_hash meta
65
+ instance.send("#{name}_meta=", meta_encode(meta))
66
+ end
67
+
59
68
  # Return meta data for given style
60
- def meta_read(style, item)
69
+ def read_meta(style, item)
61
70
  if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
62
71
  if (meta = meta_decode(instance_read(:meta)))
63
- meta.key?(style) ? meta[style][item] : nil
72
+ meta[style] && meta[style][item]
64
73
  end
65
74
  end
66
75
  end
@@ -74,6 +83,13 @@ module Paperclip
74
83
  def meta_decode(meta)
75
84
  Marshal.load(Base64.decode64(meta))
76
85
  end
86
+
87
+ # Retain existing meta values that will not be recalculated when
88
+ # reprocessing a subset of styles
89
+ def merge_existing_meta_hash(meta)
90
+ return unless (original_meta = instance.send("#{name}_meta"))
91
+ meta.reverse_merge! meta_decode(original_meta)
92
+ end
77
93
  end
78
94
  end
79
95
  end
@@ -1,5 +1,5 @@
1
1
  module Paperclip
2
2
  module Meta
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -7,6 +7,7 @@ describe "Attachment" do
7
7
  geometry = geometry_for(small_path)
8
8
  assert_equal geometry.width, img.small_image.width
9
9
  assert_equal geometry.height, img.small_image.height
10
+ assert_equal "50x64", img.small_image.image_size
10
11
  end
11
12
 
12
13
  it "saves geometry for styles" do
@@ -15,6 +16,12 @@ describe "Attachment" do
15
16
  assert_equal 100, img.big_image.height(:thumb)
16
17
  end
17
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
+
18
25
  it "sets geometry on update" do
19
26
  img = Image.create!
20
27
  img.small_image = small_image
@@ -24,7 +31,7 @@ describe "Attachment" do
24
31
  assert_equal geometry.height, img.small_image.height
25
32
  end
26
33
 
27
- describe 'file size' do
34
+ describe '#size' do
28
35
  before do
29
36
  @image = Image.create(big_image: big_image)
30
37
  end
@@ -68,12 +75,56 @@ describe "Attachment" do
68
75
  assert_nil img.small_image.height
69
76
  end
70
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
+
71
121
  private
72
122
 
73
123
  def small_path
74
124
  File.join(File.dirname(__FILE__), 'fixtures', 'small.png')
75
125
  end
76
126
 
127
+ # 50x64
77
128
  def small_image
78
129
  File.open(small_path)
79
130
  end
@@ -82,6 +133,7 @@ describe "Attachment" do
82
133
  Paperclip::Geometry.from_file(path)
83
134
  end
84
135
 
136
+ # 600x277
85
137
  def big_image
86
138
  File.open(File.join(File.dirname(__FILE__), 'fixtures', 'big.jpg'))
87
139
  end
@@ -32,7 +32,7 @@ class Image < ActiveRecord::Base
32
32
  storage: :filesystem,
33
33
  path: "./spec/tmp/fixtures/tmp/:style/:id.:extension",
34
34
  url: "./spec/tmp/fixtures/tmp/:style/:id.extension",
35
- styles: { thumb: "100x100#" }
35
+ styles: { thumb: "100x100#", large: "500x500#" }
36
36
 
37
37
  # paperclip 4.0 requires a validator
38
38
  validates_attachment_content_type :small_image, content_type: /\Aimage/
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: 1.0.1
4
+ version: 1.1.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-02-20 00:00:00.000000000 Z
12
+ date: 2014-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: paperclip
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  version: '0'
164
164
  requirements: []
165
165
  rubyforge_project:
166
- rubygems_version: 2.2.1
166
+ rubygems_version: 2.2.2
167
167
  signing_key:
168
168
  specification_version: 4
169
169
  summary: Add width, height, and size to paperclip images