jmcnevin-paperclip 2.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE +26 -0
  2. data/README.md +414 -0
  3. data/Rakefile +86 -0
  4. data/generators/paperclip/USAGE +5 -0
  5. data/generators/paperclip/paperclip_generator.rb +27 -0
  6. data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  7. data/init.rb +4 -0
  8. data/lib/generators/paperclip/USAGE +8 -0
  9. data/lib/generators/paperclip/paperclip_generator.rb +33 -0
  10. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  11. data/lib/paperclip.rb +480 -0
  12. data/lib/paperclip/attachment.rb +520 -0
  13. data/lib/paperclip/callback_compatibility.rb +61 -0
  14. data/lib/paperclip/geometry.rb +155 -0
  15. data/lib/paperclip/interpolations.rb +171 -0
  16. data/lib/paperclip/iostream.rb +45 -0
  17. data/lib/paperclip/matchers.rb +33 -0
  18. data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
  19. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
  20. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
  21. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
  22. data/lib/paperclip/missing_attachment_styles.rb +87 -0
  23. data/lib/paperclip/options.rb +78 -0
  24. data/lib/paperclip/processor.rb +58 -0
  25. data/lib/paperclip/railtie.rb +26 -0
  26. data/lib/paperclip/storage.rb +3 -0
  27. data/lib/paperclip/storage/filesystem.rb +81 -0
  28. data/lib/paperclip/storage/fog.rb +163 -0
  29. data/lib/paperclip/storage/s3.rb +270 -0
  30. data/lib/paperclip/style.rb +95 -0
  31. data/lib/paperclip/thumbnail.rb +105 -0
  32. data/lib/paperclip/upfile.rb +62 -0
  33. data/lib/paperclip/version.rb +3 -0
  34. data/lib/tasks/paperclip.rake +101 -0
  35. data/rails/init.rb +2 -0
  36. data/shoulda_macros/paperclip.rb +124 -0
  37. data/test/attachment_test.rb +1161 -0
  38. data/test/database.yml +4 -0
  39. data/test/fixtures/12k.png +0 -0
  40. data/test/fixtures/50x50.png +0 -0
  41. data/test/fixtures/5k.png +0 -0
  42. data/test/fixtures/animated.gif +0 -0
  43. data/test/fixtures/bad.png +1 -0
  44. data/test/fixtures/double spaces in name.png +0 -0
  45. data/test/fixtures/fog.yml +8 -0
  46. data/test/fixtures/s3.yml +8 -0
  47. data/test/fixtures/spaced file.png +0 -0
  48. data/test/fixtures/text.txt +1 -0
  49. data/test/fixtures/twopage.pdf +0 -0
  50. data/test/fixtures/uppercase.PNG +0 -0
  51. data/test/fog_test.rb +192 -0
  52. data/test/geometry_test.rb +206 -0
  53. data/test/helper.rb +158 -0
  54. data/test/integration_test.rb +781 -0
  55. data/test/interpolations_test.rb +202 -0
  56. data/test/iostream_test.rb +71 -0
  57. data/test/matchers/have_attached_file_matcher_test.rb +24 -0
  58. data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
  59. data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
  60. data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
  61. data/test/options_test.rb +75 -0
  62. data/test/paperclip_missing_attachment_styles_test.rb +80 -0
  63. data/test/paperclip_test.rb +340 -0
  64. data/test/processor_test.rb +10 -0
  65. data/test/storage/filesystem_test.rb +56 -0
  66. data/test/storage/s3_live_test.rb +88 -0
  67. data/test/storage/s3_test.rb +689 -0
  68. data/test/style_test.rb +180 -0
  69. data/test/thumbnail_test.rb +383 -0
  70. data/test/upfile_test.rb +53 -0
  71. metadata +294 -0
@@ -0,0 +1,202 @@
1
+ require './test/helper'
2
+
3
+ class InterpolationsTest < Test::Unit::TestCase
4
+ should "return all methods but the infrastructure when sent #all" do
5
+ methods = Paperclip::Interpolations.all
6
+ assert ! methods.include?(:[])
7
+ assert ! methods.include?(:[]=)
8
+ assert ! methods.include?(:all)
9
+ methods.each do |m|
10
+ assert Paperclip::Interpolations.respond_to?(m)
11
+ end
12
+ end
13
+
14
+ should "return the Rails.root" do
15
+ assert_equal Rails.root, Paperclip::Interpolations.rails_root(:attachment, :style)
16
+ end
17
+
18
+ should "return the Rails.env" do
19
+ assert_equal Rails.env, Paperclip::Interpolations.rails_env(:attachment, :style)
20
+ end
21
+
22
+ should "return the class of the Interpolations module when called with no params" do
23
+ assert_equal Module, Paperclip::Interpolations.class
24
+ end
25
+
26
+ should "return the class of the instance" do
27
+ attachment = mock
28
+ attachment.expects(:instance).returns(attachment)
29
+ attachment.expects(:class).returns("Thing")
30
+ assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
31
+ end
32
+
33
+ should "return the basename of the file" do
34
+ attachment = mock
35
+ attachment.expects(:original_filename).returns("one.jpg").times(2)
36
+ assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
37
+ end
38
+
39
+ should "return the extension of the file" do
40
+ attachment = mock
41
+ attachment.expects(:original_filename).returns("one.jpg")
42
+ attachment.expects(:styles).returns({})
43
+ assert_equal "jpg", Paperclip::Interpolations.extension(attachment, :style)
44
+ end
45
+
46
+ should "return the extension of the file as the format if defined in the style" do
47
+ attachment = mock
48
+ attachment.expects(:original_filename).never
49
+ attachment.expects(:styles).returns({:style => {:format => "png"}})
50
+ assert_equal "png", Paperclip::Interpolations.extension(attachment, :style)
51
+ end
52
+
53
+ should "return the extension of the file based on the content type" do
54
+ attachment = mock
55
+ attachment.expects(:content_type).returns('image/jpeg')
56
+ interpolations = Paperclip::Interpolations
57
+ interpolations.expects(:extension).returns('random')
58
+ assert_equal "jpeg", interpolations.content_type_extension(attachment, :style)
59
+ end
60
+
61
+ should "return the original extension of the file if it matches a content type extension" do
62
+ attachment = mock
63
+ attachment.expects(:content_type).returns('image/jpeg')
64
+ interpolations = Paperclip::Interpolations
65
+ interpolations.expects(:extension).returns('jpe')
66
+ assert_equal "jpe", interpolations.content_type_extension(attachment, :style)
67
+ end
68
+
69
+ should "return the latter half of the content type of the extension if no match found" do
70
+ attachment = mock
71
+ attachment.expects(:content_type).at_least_once().returns('not/found')
72
+ interpolations = Paperclip::Interpolations
73
+ interpolations.expects(:extension).returns('random')
74
+ assert_equal "found", interpolations.content_type_extension(attachment, :style)
75
+ end
76
+
77
+ should "return the #to_param of the attachment" do
78
+ attachment = mock
79
+ attachment.expects(:to_param).returns("23-awesome")
80
+ attachment.expects(:instance).returns(attachment)
81
+ assert_equal "23-awesome", Paperclip::Interpolations.param(attachment, :style)
82
+ end
83
+
84
+ should "return the id of the attachment" do
85
+ attachment = mock
86
+ attachment.expects(:id).returns(23)
87
+ attachment.expects(:instance).returns(attachment)
88
+ assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
89
+ end
90
+
91
+ should "return the partitioned id of the attachment when the id is an integer" do
92
+ attachment = mock
93
+ attachment.expects(:id).returns(23)
94
+ attachment.expects(:instance).returns(attachment)
95
+ assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
96
+ end
97
+
98
+ should "return the partitioned id of the attachment when the id is a string" do
99
+ attachment = mock
100
+ attachment.expects(:id).returns("32fnj23oio2f")
101
+ attachment.expects(:instance).returns(attachment)
102
+ assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
103
+ end
104
+
105
+ should "return the name of the attachment" do
106
+ attachment = mock
107
+ attachment.expects(:name).returns("file")
108
+ assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
109
+ end
110
+
111
+ should "return the style" do
112
+ assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
113
+ end
114
+
115
+ should "return the default style" do
116
+ attachment = mock
117
+ attachment.expects(:default_style).returns(:default_style)
118
+ assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
119
+ end
120
+
121
+ should "reinterpolate :url" do
122
+ attachment = mock
123
+ attachment.expects(:url).with(:style, :timestamp => false, :escape => false).returns("1234")
124
+ assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
125
+ end
126
+
127
+ should "raise if infinite loop detcted reinterpolating :url" do
128
+ attachment = Object.new
129
+ class << attachment
130
+ def url(*args)
131
+ Paperclip::Interpolations.url(self, :style)
132
+ end
133
+ end
134
+ assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
135
+ end
136
+
137
+ should "return the filename as basename.extension" do
138
+ attachment = mock
139
+ attachment.expects(:styles).returns({})
140
+ attachment.expects(:original_filename).returns("one.jpg").times(3)
141
+ assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
142
+ end
143
+
144
+ should "return the filename as basename.extension when format supplied" do
145
+ attachment = mock
146
+ attachment.expects(:styles).returns({:style => {:format => :png}})
147
+ attachment.expects(:original_filename).returns("one.jpg").times(2)
148
+ assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
149
+ end
150
+
151
+ should "return the filename as basename when extension is blank" do
152
+ attachment = mock
153
+ attachment.stubs(:styles).returns({})
154
+ attachment.stubs(:original_filename).returns("one")
155
+ assert_equal "one", Paperclip::Interpolations.filename(attachment, :style)
156
+ end
157
+
158
+ should "return the basename when the extension contains regexp special characters" do
159
+ attachment = mock
160
+ attachment.stubs(:styles).returns({})
161
+ attachment.stubs(:original_filename).returns("one.ab)")
162
+ assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
163
+ end
164
+
165
+ should "return the timestamp" do
166
+ now = Time.now
167
+ zone = 'UTC'
168
+ attachment = mock
169
+ attachment.expects(:instance_read).with(:updated_at).returns(now)
170
+ attachment.expects(:time_zone).returns(zone)
171
+ assert_equal now.in_time_zone(zone).to_s, Paperclip::Interpolations.timestamp(attachment, :style)
172
+ end
173
+
174
+ should "return updated_at" do
175
+ attachment = mock
176
+ seconds_since_epoch = 1234567890
177
+ attachment.expects(:updated_at).returns(seconds_since_epoch)
178
+ assert_equal seconds_since_epoch, Paperclip::Interpolations.updated_at(attachment, :style)
179
+ end
180
+
181
+ should "return attachment's hash when passing both arguments" do
182
+ attachment = mock
183
+ fake_hash = "a_wicked_secure_hash"
184
+ attachment.expects(:hash).returns(fake_hash)
185
+ assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
186
+ end
187
+
188
+ should "return Object#hash when passing no argument" do
189
+ attachment = mock
190
+ fake_hash = "a_wicked_secure_hash"
191
+ attachment.expects(:hash).never.returns(fake_hash)
192
+ assert_not_equal fake_hash, Paperclip::Interpolations.hash
193
+ end
194
+
195
+ should "call all expected interpolations with the given arguments" do
196
+ Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
197
+ Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
198
+ Paperclip::Interpolations.expects(:notreal).never
199
+ value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
200
+ assert_equal ":notreal/1234/attachments", value
201
+ end
202
+ end
@@ -0,0 +1,71 @@
1
+ require './test/helper'
2
+
3
+ class IOStreamTest < Test::Unit::TestCase
4
+ include IOStream
5
+ context "A file" do
6
+ setup do
7
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
8
+ end
9
+
10
+ teardown { @file.close }
11
+
12
+ context "that is sent #stream_to" do
13
+
14
+ context "and given a String" do
15
+ setup do
16
+ FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
17
+ assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
18
+ end
19
+
20
+ should "return a File" do
21
+ assert @result.is_a?(File)
22
+ end
23
+
24
+ should "contain the same data as the original file" do
25
+ @file.rewind; @result.rewind
26
+ assert_equal @file.read, @result.read
27
+ end
28
+ end
29
+
30
+ context "and given a Tempfile" do
31
+ setup do
32
+ tempfile = Tempfile.new('iostream.test')
33
+ tempfile.binmode
34
+ assert @result = stream_to(@file, tempfile)
35
+ end
36
+
37
+ should "return a Tempfile" do
38
+ assert @result.is_a?(Tempfile)
39
+ end
40
+
41
+ should "contain the same data as the original file" do
42
+ @file.rewind; @result.rewind
43
+ assert_equal @file.read, @result.read
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ context "that is converted #to_tempfile" do
50
+ setup do
51
+ assert @tempfile = to_tempfile(@file)
52
+ end
53
+
54
+ should "convert it to a Paperclip Tempfile" do
55
+ assert @tempfile.is_a?(Paperclip::Tempfile)
56
+ end
57
+
58
+ should "have the name be based on the original_filename" do
59
+ name = File.basename(@file.path)
60
+ extension = File.extname(name)
61
+ basename = File.basename(name, extension)
62
+ assert_match %r[^stream.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
63
+ end
64
+
65
+ should "have the Tempfile contain the same data as the file" do
66
+ @file.rewind; @tempfile.rewind
67
+ assert_equal @file.read, @tempfile.read
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,24 @@
1
+ require './test/helper'
2
+
3
+ class HaveAttachedFileMatcherTest < Test::Unit::TestCase
4
+ context "have_attached_file" do
5
+ setup do
6
+ @dummy_class = reset_class "Dummy"
7
+ reset_table "dummies"
8
+ @matcher = self.class.have_attached_file(:avatar)
9
+ end
10
+
11
+ context "given a class with no attachment" do
12
+ should_reject_dummy_class
13
+ end
14
+
15
+ context "given a class with an attachment" do
16
+ setup do
17
+ modify_table("dummies"){|d| d.string :avatar_file_name }
18
+ @dummy_class.has_attached_file :avatar
19
+ end
20
+
21
+ should_accept_dummy_class
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,87 @@
1
+ require './test/helper'
2
+
3
+ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
4
+ context "validate_attachment_content_type" do
5
+ setup do
6
+ reset_table("dummies") do |d|
7
+ d.string :title
8
+ d.string :avatar_file_name
9
+ d.string :avatar_content_type
10
+ end
11
+ @dummy_class = reset_class "Dummy"
12
+ @dummy_class.has_attached_file :avatar
13
+ @matcher = self.class.validate_attachment_content_type(:avatar).
14
+ allowing(%w(image/png image/jpeg)).
15
+ rejecting(%w(audio/mp3 application/octet-stream))
16
+ end
17
+
18
+ context "given a class with no validation" do
19
+ should_reject_dummy_class
20
+ end
21
+
22
+ context "given a class with a validation that doesn't match" do
23
+ setup do
24
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
25
+ end
26
+
27
+ should_reject_dummy_class
28
+ end
29
+
30
+ context "given a class with a matching validation" do
31
+ setup do
32
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
33
+ end
34
+
35
+ should_accept_dummy_class
36
+ end
37
+
38
+ context "given a class with other validations but matching types" do
39
+ setup do
40
+ @dummy_class.validates_presence_of :title
41
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
42
+ end
43
+
44
+ should_accept_dummy_class
45
+ end
46
+
47
+ context "given a class that matches and a matcher that only specifies 'allowing'" do
48
+ setup do
49
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
50
+ @matcher = self.class.validate_attachment_content_type(:avatar).
51
+ allowing(%w(image/png image/jpeg))
52
+ end
53
+
54
+ should_accept_dummy_class
55
+ end
56
+
57
+ context "given a class that does not match and a matcher that only specifies 'allowing'" do
58
+ setup do
59
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
60
+ @matcher = self.class.validate_attachment_content_type(:avatar).
61
+ allowing(%w(image/png image/jpeg))
62
+ end
63
+
64
+ should_reject_dummy_class
65
+ end
66
+
67
+ context "given a class that matches and a matcher that only specifies 'rejecting'" do
68
+ setup do
69
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
70
+ @matcher = self.class.validate_attachment_content_type(:avatar).
71
+ rejecting(%w(audio/mp3 application/octet-stream))
72
+ end
73
+
74
+ should_accept_dummy_class
75
+ end
76
+
77
+ context "given a class that does not match and a matcher that only specifies 'rejecting'" do
78
+ setup do
79
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
80
+ @matcher = self.class.validate_attachment_content_type(:avatar).
81
+ rejecting(%w(audio/mp3 application/octet-stream))
82
+ end
83
+
84
+ should_reject_dummy_class
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ require './test/helper'
2
+
3
+ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
4
+ context "validate_attachment_presence" do
5
+ setup do
6
+ reset_table("dummies") do |d|
7
+ d.string :avatar_file_name
8
+ end
9
+ @dummy_class = reset_class "Dummy"
10
+ @dummy_class.has_attached_file :avatar
11
+ @matcher = self.class.validate_attachment_presence(:avatar)
12
+ end
13
+
14
+ context "given a class with no validation" do
15
+ should_reject_dummy_class
16
+ end
17
+
18
+ context "given a class with a matching validation" do
19
+ setup do
20
+ @dummy_class.validates_attachment_presence :avatar
21
+ end
22
+
23
+ should_accept_dummy_class
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ require './test/helper'
2
+
3
+ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
4
+ context "validate_attachment_size" do
5
+ setup do
6
+ reset_table("dummies") do |d|
7
+ d.string :avatar_file_name
8
+ d.integer :avatar_file_size
9
+ end
10
+ @dummy_class = reset_class "Dummy"
11
+ @dummy_class.has_attached_file :avatar
12
+ end
13
+
14
+ context "of limited size" do
15
+ setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) }
16
+
17
+ context "given a class with no validation" do
18
+ should_reject_dummy_class
19
+ end
20
+
21
+ context "given a class with a validation that's too high" do
22
+ setup { @dummy_class.validates_attachment_size :avatar, :in => 256..2048 }
23
+ should_reject_dummy_class
24
+ end
25
+
26
+ context "given a class with a validation that's too low" do
27
+ setup { @dummy_class.validates_attachment_size :avatar, :in => 0..1024 }
28
+ should_reject_dummy_class
29
+ end
30
+
31
+ context "given a class with a validation that matches" do
32
+ setup { @dummy_class.validates_attachment_size :avatar, :in => 256..1024 }
33
+ should_accept_dummy_class
34
+ end
35
+ end
36
+
37
+ context "validates_attachment_size with infinite range" do
38
+ setup{ @matcher = self.class.validate_attachment_size(:avatar) }
39
+
40
+ context "given a class with an upper limit" do
41
+ setup { @dummy_class.validates_attachment_size :avatar, :less_than => 1 }
42
+ should_accept_dummy_class
43
+ end
44
+
45
+ context "given a class with no upper limit" do
46
+ setup { @dummy_class.validates_attachment_size :avatar, :greater_than => 1 }
47
+ should_accept_dummy_class
48
+ end
49
+ end
50
+ end
51
+ end