joshpuetz-paperclip 2.3.0
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.
- data/LICENSE +26 -0
- data/README.rdoc +174 -0
- data/Rakefile +99 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +1 -0
- data/lib/paperclip/attachment.rb +414 -0
- data/lib/paperclip/callback_compatability.rb +33 -0
- data/lib/paperclip/geometry.rb +115 -0
- data/lib/paperclip/interpolations.rb +105 -0
- data/lib/paperclip/iostream.rb +58 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +49 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +66 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +48 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +83 -0
- data/lib/paperclip/matchers.rb +4 -0
- data/lib/paperclip/processor.rb +49 -0
- data/lib/paperclip/storage.rb +236 -0
- data/lib/paperclip/thumbnail.rb +70 -0
- data/lib/paperclip/upfile.rb +48 -0
- data/lib/paperclip.rb +350 -0
- data/shoulda_macros/paperclip.rb +68 -0
- data/tasks/paperclip_tasks.rake +79 -0
- data/test/attachment_test.rb +767 -0
- data/test/database.yml +4 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/s3.yml +4 -0
- data/test/fixtures/text.txt +0 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/geometry_test.rb +177 -0
- data/test/helper.rb +99 -0
- data/test/integration_test.rb +481 -0
- data/test/interpolations_test.rb +120 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +21 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +30 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +21 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +50 -0
- data/test/paperclip_test.rb +291 -0
- data/test/processor_test.rb +10 -0
- data/test/storage_test.rb +282 -0
- data/test/thumbnail_test.rb +177 -0
- metadata +125 -0
data/test/database.yml
ADDED
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
This is not an image.
|
File without changes
|
Binary file
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'test/helper'
|
2
|
+
|
3
|
+
class GeometryTest < Test::Unit::TestCase
|
4
|
+
context "Paperclip::Geometry" do
|
5
|
+
should "correctly report its given dimensions" do
|
6
|
+
assert @geo = Paperclip::Geometry.new(1024, 768)
|
7
|
+
assert_equal 1024, @geo.width
|
8
|
+
assert_equal 768, @geo.height
|
9
|
+
end
|
10
|
+
|
11
|
+
should "set height to 0 if height dimension is missing" do
|
12
|
+
assert @geo = Paperclip::Geometry.new(1024)
|
13
|
+
assert_equal 1024, @geo.width
|
14
|
+
assert_equal 0, @geo.height
|
15
|
+
end
|
16
|
+
|
17
|
+
should "set width to 0 if width dimension is missing" do
|
18
|
+
assert @geo = Paperclip::Geometry.new(nil, 768)
|
19
|
+
assert_equal 0, @geo.width
|
20
|
+
assert_equal 768, @geo.height
|
21
|
+
end
|
22
|
+
|
23
|
+
should "be generated from a WxH-formatted string" do
|
24
|
+
assert @geo = Paperclip::Geometry.parse("800x600")
|
25
|
+
assert_equal 800, @geo.width
|
26
|
+
assert_equal 600, @geo.height
|
27
|
+
end
|
28
|
+
|
29
|
+
should "be generated from a xH-formatted string" do
|
30
|
+
assert @geo = Paperclip::Geometry.parse("x600")
|
31
|
+
assert_equal 0, @geo.width
|
32
|
+
assert_equal 600, @geo.height
|
33
|
+
end
|
34
|
+
|
35
|
+
should "be generated from a Wx-formatted string" do
|
36
|
+
assert @geo = Paperclip::Geometry.parse("800x")
|
37
|
+
assert_equal 800, @geo.width
|
38
|
+
assert_equal 0, @geo.height
|
39
|
+
end
|
40
|
+
|
41
|
+
should "be generated from a W-formatted string" do
|
42
|
+
assert @geo = Paperclip::Geometry.parse("800")
|
43
|
+
assert_equal 800, @geo.width
|
44
|
+
assert_equal 0, @geo.height
|
45
|
+
end
|
46
|
+
|
47
|
+
should "ensure the modifier is nil if not present" do
|
48
|
+
assert @geo = Paperclip::Geometry.parse("123x456")
|
49
|
+
assert_nil @geo.modifier
|
50
|
+
end
|
51
|
+
|
52
|
+
should "treat x and X the same in geometries" do
|
53
|
+
@lower = Paperclip::Geometry.parse("123x456")
|
54
|
+
@upper = Paperclip::Geometry.parse("123X456")
|
55
|
+
assert_equal 123, @lower.width
|
56
|
+
assert_equal 123, @upper.width
|
57
|
+
assert_equal 456, @lower.height
|
58
|
+
assert_equal 456, @upper.height
|
59
|
+
end
|
60
|
+
|
61
|
+
['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
|
62
|
+
should "ensure the modifier #{mod.inspect} is preserved" do
|
63
|
+
assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
|
64
|
+
assert_equal mod, @geo.modifier
|
65
|
+
assert_equal "123x456#{mod}", @geo.to_s
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
|
70
|
+
should "ensure the modifier #{mod.inspect} is preserved with no height" do
|
71
|
+
assert @geo = Paperclip::Geometry.parse("123x#{mod}")
|
72
|
+
assert_equal mod, @geo.modifier
|
73
|
+
assert_equal "123#{mod}", @geo.to_s
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
should "make sure the modifier gets passed during transformation_to" do
|
78
|
+
assert @src = Paperclip::Geometry.parse("123x456")
|
79
|
+
assert @dst = Paperclip::Geometry.parse("123x456>")
|
80
|
+
assert_equal ["123x456>", nil], @src.transformation_to(@dst)
|
81
|
+
end
|
82
|
+
|
83
|
+
should "generate correct ImageMagick formatting string for W-formatted string" do
|
84
|
+
assert @geo = Paperclip::Geometry.parse("800")
|
85
|
+
assert_equal "800", @geo.to_s
|
86
|
+
end
|
87
|
+
|
88
|
+
should "generate correct ImageMagick formatting string for Wx-formatted string" do
|
89
|
+
assert @geo = Paperclip::Geometry.parse("800x")
|
90
|
+
assert_equal "800", @geo.to_s
|
91
|
+
end
|
92
|
+
|
93
|
+
should "generate correct ImageMagick formatting string for xH-formatted string" do
|
94
|
+
assert @geo = Paperclip::Geometry.parse("x600")
|
95
|
+
assert_equal "x600", @geo.to_s
|
96
|
+
end
|
97
|
+
|
98
|
+
should "generate correct ImageMagick formatting string for WxH-formatted string" do
|
99
|
+
assert @geo = Paperclip::Geometry.parse("800x600")
|
100
|
+
assert_equal "800x600", @geo.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
should "be generated from a file" do
|
104
|
+
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
|
105
|
+
file = File.new(file, 'rb')
|
106
|
+
assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
|
107
|
+
assert @geo.height > 0
|
108
|
+
assert @geo.width > 0
|
109
|
+
end
|
110
|
+
|
111
|
+
should "be generated from a file path" do
|
112
|
+
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
|
113
|
+
assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
|
114
|
+
assert @geo.height > 0
|
115
|
+
assert @geo.width > 0
|
116
|
+
end
|
117
|
+
|
118
|
+
should "not generate from a bad file" do
|
119
|
+
file = "/home/This File Does Not Exist.omg"
|
120
|
+
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
|
121
|
+
end
|
122
|
+
|
123
|
+
[['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
|
124
|
+
['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333],
|
125
|
+
['square', 100, 100, false, false, true, 100, 100, 1]].each do |args|
|
126
|
+
context "performing calculations on a #{args[0]} viewport" do
|
127
|
+
setup do
|
128
|
+
@geo = Paperclip::Geometry.new(args[1], args[2])
|
129
|
+
end
|
130
|
+
|
131
|
+
should "#{args[3] ? "" : "not"} be vertical" do
|
132
|
+
assert_equal args[3], @geo.vertical?
|
133
|
+
end
|
134
|
+
|
135
|
+
should "#{args[4] ? "" : "not"} be horizontal" do
|
136
|
+
assert_equal args[4], @geo.horizontal?
|
137
|
+
end
|
138
|
+
|
139
|
+
should "#{args[5] ? "" : "not"} be square" do
|
140
|
+
assert_equal args[5], @geo.square?
|
141
|
+
end
|
142
|
+
|
143
|
+
should "report that #{args[6]} is the larger dimension" do
|
144
|
+
assert_equal args[6], @geo.larger
|
145
|
+
end
|
146
|
+
|
147
|
+
should "report that #{args[7]} is the smaller dimension" do
|
148
|
+
assert_equal args[7], @geo.smaller
|
149
|
+
end
|
150
|
+
|
151
|
+
should "have an aspect ratio of #{args[8]}" do
|
152
|
+
assert_in_delta args[8], @geo.aspect, 0.0001
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
[[ [1000, 100], [64, 64], "x64", "64x64+288+0" ],
|
158
|
+
[ [100, 1000], [50, 950], "x950", "50x950+22+0" ],
|
159
|
+
[ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args|
|
160
|
+
context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do
|
161
|
+
setup do
|
162
|
+
@geo = Paperclip::Geometry.new(*args[0])
|
163
|
+
@dst = Paperclip::Geometry.new(*args[1])
|
164
|
+
@scale, @crop = @geo.transformation_to @dst, true
|
165
|
+
end
|
166
|
+
|
167
|
+
should "be able to return the correct scaling transformation geometry #{args[2]}" do
|
168
|
+
assert_equal args[2], @scale
|
169
|
+
end
|
170
|
+
|
171
|
+
should "be able to return the correct crop transformation geometry #{args[3]}" do
|
172
|
+
assert_equal args[3], @crop
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
gem 'sqlite3-ruby'
|
8
|
+
|
9
|
+
require 'active_record'
|
10
|
+
require 'active_support'
|
11
|
+
begin
|
12
|
+
require 'ruby-debug'
|
13
|
+
rescue LoadError
|
14
|
+
puts "ruby-debug not loaded"
|
15
|
+
end
|
16
|
+
|
17
|
+
ROOT = File.join(File.dirname(__FILE__), '..')
|
18
|
+
RAILS_ROOT = ROOT
|
19
|
+
RAILS_ENV = "test"
|
20
|
+
|
21
|
+
$LOAD_PATH << File.join(ROOT, 'lib')
|
22
|
+
$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip')
|
23
|
+
|
24
|
+
require File.join(ROOT, 'lib', 'paperclip.rb')
|
25
|
+
|
26
|
+
require 'shoulda_macros/paperclip'
|
27
|
+
|
28
|
+
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
|
29
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
30
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
31
|
+
ActiveRecord::Base.establish_connection(config['test'])
|
32
|
+
|
33
|
+
def reset_class class_name
|
34
|
+
ActiveRecord::Base.send(:include, Paperclip)
|
35
|
+
Object.send(:remove_const, class_name) rescue nil
|
36
|
+
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
37
|
+
klass.class_eval{ include Paperclip }
|
38
|
+
klass
|
39
|
+
end
|
40
|
+
|
41
|
+
def reset_table table_name, &block
|
42
|
+
block ||= lambda { |table| true }
|
43
|
+
ActiveRecord::Base.connection.create_table :dummies, {:force => true}, &block
|
44
|
+
end
|
45
|
+
|
46
|
+
def modify_table table_name, &block
|
47
|
+
ActiveRecord::Base.connection.change_table :dummies, &block
|
48
|
+
end
|
49
|
+
|
50
|
+
def rebuild_model options = {}
|
51
|
+
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
|
52
|
+
table.column :other, :string
|
53
|
+
table.column :avatar_file_name, :string
|
54
|
+
table.column :avatar_content_type, :string
|
55
|
+
table.column :avatar_file_size, :integer
|
56
|
+
table.column :avatar_updated_at, :datetime
|
57
|
+
end
|
58
|
+
rebuild_class options
|
59
|
+
end
|
60
|
+
|
61
|
+
def rebuild_class options = {}
|
62
|
+
ActiveRecord::Base.send(:include, Paperclip)
|
63
|
+
Object.send(:remove_const, "Dummy") rescue nil
|
64
|
+
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
|
65
|
+
Dummy.class_eval do
|
66
|
+
include Paperclip
|
67
|
+
has_attached_file :avatar, options
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def temporary_rails_env(new_env)
|
72
|
+
old_env = Object.const_defined?("RAILS_ENV") ? RAILS_ENV : nil
|
73
|
+
silence_warnings do
|
74
|
+
Object.const_set("RAILS_ENV", new_env)
|
75
|
+
end
|
76
|
+
yield
|
77
|
+
silence_warnings do
|
78
|
+
Object.const_set("RAILS_ENV", old_env)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class FakeModel
|
83
|
+
attr_accessor :avatar_file_name,
|
84
|
+
:avatar_file_size,
|
85
|
+
:avatar_last_updated,
|
86
|
+
:avatar_content_type,
|
87
|
+
:id
|
88
|
+
|
89
|
+
def errors
|
90
|
+
@errors ||= []
|
91
|
+
end
|
92
|
+
|
93
|
+
def run_callbacks name, *args
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def attachment options
|
98
|
+
Paperclip::Attachment.new(:avatar, FakeModel.new, options)
|
99
|
+
end
|