paperclip 3.0.3 → 3.5.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of paperclip might be problematic. Click here for more details.

Files changed (121) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +2 -1
  3. data/.travis.yml +3 -0
  4. data/Appraisals +8 -3
  5. data/Gemfile +1 -1
  6. data/LICENSE +1 -1
  7. data/NEWS +198 -35
  8. data/README.md +332 -113
  9. data/features/basic_integration.feature +24 -12
  10. data/features/migration.feature +94 -0
  11. data/features/rake_tasks.feature +2 -3
  12. data/features/step_definitions/attachment_steps.rb +28 -0
  13. data/features/step_definitions/rails_steps.rb +94 -8
  14. data/features/step_definitions/s3_steps.rb +1 -1
  15. data/features/step_definitions/web_steps.rb +3 -3
  16. data/features/support/fakeweb.rb +4 -1
  17. data/features/support/file_helpers.rb +10 -0
  18. data/features/support/rails.rb +18 -2
  19. data/gemfiles/3.0.gemfile +2 -2
  20. data/gemfiles/3.1.gemfile +2 -2
  21. data/gemfiles/3.2.gemfile +2 -2
  22. data/gemfiles/4.0.gemfile +11 -0
  23. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +4 -8
  24. data/lib/paperclip/attachment.rb +96 -43
  25. data/lib/paperclip/attachment_registry.rb +57 -0
  26. data/lib/paperclip/callbacks.rb +2 -2
  27. data/lib/paperclip/content_type_detector.rb +78 -0
  28. data/lib/paperclip/file_command_content_type_detector.rb +32 -0
  29. data/lib/paperclip/filename_cleaner.rb +16 -0
  30. data/lib/paperclip/geometry.rb +66 -30
  31. data/lib/paperclip/geometry_detector_factory.rb +41 -0
  32. data/lib/paperclip/geometry_parser_factory.rb +31 -0
  33. data/lib/paperclip/glue.rb +2 -8
  34. data/lib/paperclip/has_attached_file.rb +99 -0
  35. data/lib/paperclip/helpers.rb +12 -15
  36. data/lib/paperclip/interpolations/plural_cache.rb +17 -0
  37. data/lib/paperclip/interpolations.rb +15 -5
  38. data/lib/paperclip/io_adapters/abstract_adapter.rb +45 -0
  39. data/lib/paperclip/io_adapters/attachment_adapter.rb +14 -49
  40. data/lib/paperclip/io_adapters/data_uri_adapter.rb +27 -0
  41. data/lib/paperclip/io_adapters/empty_string_adapter.rb +18 -0
  42. data/lib/paperclip/io_adapters/file_adapter.rb +8 -69
  43. data/lib/paperclip/io_adapters/identity_adapter.rb +1 -1
  44. data/lib/paperclip/io_adapters/nil_adapter.rb +2 -2
  45. data/lib/paperclip/io_adapters/stringio_adapter.rb +16 -45
  46. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +17 -40
  47. data/lib/paperclip/io_adapters/uri_adapter.rb +44 -0
  48. data/lib/paperclip/matchers/have_attached_file_matcher.rb +1 -5
  49. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +36 -17
  50. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +5 -1
  51. data/lib/paperclip/matchers.rb +3 -3
  52. data/lib/paperclip/missing_attachment_styles.rb +11 -16
  53. data/lib/paperclip/processor.rb +12 -0
  54. data/lib/paperclip/railtie.rb +5 -1
  55. data/lib/paperclip/schema.rb +59 -23
  56. data/lib/paperclip/storage/filesystem.rb +23 -5
  57. data/lib/paperclip/storage/fog.rb +64 -25
  58. data/lib/paperclip/storage/s3.rb +93 -52
  59. data/lib/paperclip/style.rb +2 -2
  60. data/lib/paperclip/tempfile_factory.rb +21 -0
  61. data/lib/paperclip/thumbnail.rb +18 -3
  62. data/lib/paperclip/validators/attachment_content_type_validator.rb +38 -10
  63. data/lib/paperclip/validators/attachment_presence_validator.rb +8 -8
  64. data/lib/paperclip/validators/attachment_size_validator.rb +12 -7
  65. data/lib/paperclip/validators.rb +21 -2
  66. data/lib/paperclip/version.rb +1 -1
  67. data/lib/paperclip.rb +15 -44
  68. data/lib/tasks/paperclip.rake +26 -7
  69. data/paperclip.gemspec +11 -7
  70. data/test/attachment_definitions_test.rb +12 -0
  71. data/test/attachment_processing_test.rb +83 -0
  72. data/test/attachment_registry_test.rb +77 -0
  73. data/test/attachment_test.rb +253 -44
  74. data/test/content_type_detector_test.rb +50 -0
  75. data/test/file_command_content_type_detector_test.rb +25 -0
  76. data/test/filename_cleaner_test.rb +14 -0
  77. data/test/fixtures/animated +0 -0
  78. data/test/fixtures/animated.unknown +0 -0
  79. data/test/fixtures/rotated.jpg +0 -0
  80. data/test/generator_test.rb +26 -24
  81. data/test/geometry_detector_test.rb +24 -0
  82. data/test/geometry_parser_test.rb +73 -0
  83. data/test/geometry_test.rb +55 -4
  84. data/test/has_attached_file_test.rb +125 -0
  85. data/test/helper.rb +38 -7
  86. data/test/integration_test.rb +105 -89
  87. data/test/interpolations_test.rb +12 -0
  88. data/test/io_adapters/abstract_adapter_test.rb +58 -0
  89. data/test/io_adapters/attachment_adapter_test.rb +120 -33
  90. data/test/io_adapters/data_uri_adapter_test.rb +60 -0
  91. data/test/io_adapters/empty_string_adapter_test.rb +17 -0
  92. data/test/io_adapters/file_adapter_test.rb +32 -1
  93. data/test/io_adapters/stringio_adapter_test.rb +29 -10
  94. data/test/io_adapters/uploaded_file_adapter_test.rb +53 -5
  95. data/test/io_adapters/uri_adapter_test.rb +102 -0
  96. data/test/matchers/validate_attachment_presence_matcher_test.rb +22 -0
  97. data/test/meta_class_test.rb +32 -0
  98. data/test/paperclip_missing_attachment_styles_test.rb +4 -8
  99. data/test/paperclip_test.rb +27 -51
  100. data/test/plural_cache_test.rb +36 -0
  101. data/test/processor_test.rb +16 -0
  102. data/test/rake_test.rb +103 -0
  103. data/test/schema_test.rb +179 -77
  104. data/test/storage/filesystem_test.rb +26 -3
  105. data/test/storage/fog_test.rb +181 -3
  106. data/test/storage/s3_test.rb +239 -4
  107. data/test/style_test.rb +18 -14
  108. data/test/tempfile_factory_test.rb +13 -0
  109. data/test/thumbnail_test.rb +96 -16
  110. data/test/validators/attachment_content_type_validator_test.rb +181 -55
  111. data/test/validators/attachment_size_validator_test.rb +10 -0
  112. data/test/validators_test.rb +8 -1
  113. metadata +126 -92
  114. data/Gemfile.lock +0 -157
  115. data/features/support/fixtures/.boot_config.rb.swo +0 -0
  116. data/images.rake +0 -21
  117. data/lib/.DS_Store +0 -0
  118. data/lib/paperclip/.DS_Store +0 -0
  119. data/lib/paperclip/attachment_options.rb +0 -9
  120. data/lib/paperclip/instance_methods.rb +0 -35
  121. data/test/attachment_options_test.rb +0 -27
@@ -50,6 +50,32 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
50
50
  end
51
51
  end
52
52
 
53
+ context "with a failing validation" do
54
+ setup do
55
+ build_validator :content_type => "image/png", :allow_nil => false
56
+ @dummy.stubs(:avatar_content_type => nil)
57
+ @validator.validate(@dummy)
58
+ end
59
+
60
+ should "add error to the base object" do
61
+ assert @dummy.errors[:avatar].present?,
62
+ "Error not added to base attribute"
63
+ end
64
+ end
65
+
66
+ context "with a successful validation" do
67
+ setup do
68
+ build_validator :content_type => "image/png", :allow_nil => false
69
+ @dummy.stubs(:avatar_content_type => "image/png")
70
+ @validator.validate(@dummy)
71
+ end
72
+
73
+ should "not add error to the base object" do
74
+ assert @dummy.errors[:avatar].blank?,
75
+ "Error was added to base attribute"
76
+ end
77
+ end
78
+
53
79
  context "with :allow_blank option" do
54
80
  context "as true" do
55
81
  setup do
@@ -76,93 +102,189 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
76
102
  end
77
103
  end
78
104
 
79
- context "with an allowed type" do
80
- context "as a string" do
81
- setup do
82
- build_validator :content_type => "image/jpg"
83
- @dummy.stubs(:avatar_content_type => "image/jpg")
84
- @validator.validate(@dummy)
85
- end
105
+ context "whitelist format" do
106
+ context "with an allowed type" do
107
+ context "as a string" do
108
+ setup do
109
+ build_validator :content_type => "image/jpg"
110
+ @dummy.stubs(:avatar_content_type => "image/jpg")
111
+ @validator.validate(@dummy)
112
+ end
86
113
 
87
- should "not set an error message" do
88
- assert @dummy.errors[:avatar_content_type].blank?
114
+ should "not set an error message" do
115
+ assert @dummy.errors[:avatar_content_type].blank?
116
+ end
89
117
  end
90
- end
91
118
 
92
- context "as an regexp" do
93
- setup do
94
- build_validator :content_type => /^image\/.*/
95
- @dummy.stubs(:avatar_content_type => "image/jpg")
96
- @validator.validate(@dummy)
119
+ context "as an regexp" do
120
+ setup do
121
+ build_validator :content_type => /^image\/.*/
122
+ @dummy.stubs(:avatar_content_type => "image/jpg")
123
+ @validator.validate(@dummy)
124
+ end
125
+
126
+ should "not set an error message" do
127
+ assert @dummy.errors[:avatar_content_type].blank?
128
+ end
97
129
  end
98
130
 
99
- should "not set an error message" do
100
- assert @dummy.errors[:avatar_content_type].blank?
131
+ context "as a list" do
132
+ setup do
133
+ build_validator :content_type => ["image/png", "image/jpg", "image/jpeg"]
134
+ @dummy.stubs(:avatar_content_type => "image/jpg")
135
+ @validator.validate(@dummy)
136
+ end
137
+
138
+ should "not set an error message" do
139
+ assert @dummy.errors[:avatar_content_type].blank?
140
+ end
101
141
  end
102
142
  end
103
143
 
104
- context "as a list" do
105
- setup do
106
- build_validator :content_type => ["image/png", "image/jpg", "image/jpeg"]
107
- @dummy.stubs(:avatar_content_type => "image/jpg")
108
- @validator.validate(@dummy)
144
+ context "with a disallowed type" do
145
+ context "as a string" do
146
+ setup do
147
+ build_validator :content_type => "image/png"
148
+ @dummy.stubs(:avatar_content_type => "image/jpg")
149
+ @validator.validate(@dummy)
150
+ end
151
+
152
+ should "set a correct default error message" do
153
+ assert @dummy.errors[:avatar_content_type].present?
154
+ assert_includes @dummy.errors[:avatar_content_type], "is invalid"
155
+ end
109
156
  end
110
157
 
111
- should "not set an error message" do
112
- assert @dummy.errors[:avatar_content_type].blank?
158
+ context "as a regexp" do
159
+ setup do
160
+ build_validator :content_type => /^text\/.*/
161
+ @dummy.stubs(:avatar_content_type => "image/jpg")
162
+ @validator.validate(@dummy)
163
+ end
164
+
165
+ should "set a correct default error message" do
166
+ assert @dummy.errors[:avatar_content_type].present?
167
+ assert_includes @dummy.errors[:avatar_content_type], "is invalid"
168
+ end
169
+ end
170
+
171
+ context "with :message option" do
172
+ context "without interpolation" do
173
+ setup do
174
+ build_validator :content_type => "image/png", :message => "should be a PNG image"
175
+ @dummy.stubs(:avatar_content_type => "image/jpg")
176
+ @validator.validate(@dummy)
177
+ end
178
+
179
+ should "set a correct error message" do
180
+ assert_includes @dummy.errors[:avatar_content_type], "should be a PNG image"
181
+ end
182
+ end
183
+
184
+ context "with interpolation" do
185
+ setup do
186
+ build_validator :content_type => "image/png", :message => "should have content type %{types}"
187
+ @dummy.stubs(:avatar_content_type => "image/jpg")
188
+ @validator.validate(@dummy)
189
+ end
190
+
191
+ should "set a correct error message" do
192
+ assert_includes @dummy.errors[:avatar_content_type], "should have content type image/png"
193
+ end
194
+ end
113
195
  end
114
196
  end
115
197
  end
116
198
 
117
- context "with a disallowed type" do
118
- context "as a string" do
119
- setup do
120
- build_validator :content_type => "image/png"
121
- @dummy.stubs(:avatar_content_type => "image/jpg")
122
- @validator.validate(@dummy)
123
- end
199
+ context "blacklist format" do
200
+ context "with an allowed type" do
201
+ context "as a string" do
202
+ setup do
203
+ build_validator :not => "image/gif"
204
+ @dummy.stubs(:avatar_content_type => "image/jpg")
205
+ @validator.validate(@dummy)
206
+ end
124
207
 
125
- should "set a correct default error message" do
126
- assert @dummy.errors[:avatar_content_type].present?
127
- assert_includes @dummy.errors[:avatar_content_type], "is invalid"
208
+ should "not set an error message" do
209
+ assert @dummy.errors[:avatar_content_type].blank?
210
+ end
128
211
  end
129
- end
130
212
 
131
- context "as a regexp" do
132
- setup do
133
- build_validator :content_type => /^text\/.*/
134
- @dummy.stubs(:avatar_content_type => "image/jpg")
135
- @validator.validate(@dummy)
213
+ context "as an regexp" do
214
+ setup do
215
+ build_validator :not => /^text\/.*/
216
+ @dummy.stubs(:avatar_content_type => "image/jpg")
217
+ @validator.validate(@dummy)
218
+ end
219
+
220
+ should "not set an error message" do
221
+ assert @dummy.errors[:avatar_content_type].blank?
222
+ end
136
223
  end
137
224
 
138
- should "set a correct default error message" do
139
- assert @dummy.errors[:avatar_content_type].present?
140
- assert_includes @dummy.errors[:avatar_content_type], "is invalid"
225
+ context "as a list" do
226
+ setup do
227
+ build_validator :not => ["image/png", "image/jpg", "image/jpeg"]
228
+ @dummy.stubs(:avatar_content_type => "image/gif")
229
+ @validator.validate(@dummy)
230
+ end
231
+
232
+ should "not set an error message" do
233
+ assert @dummy.errors[:avatar_content_type].blank?
234
+ end
141
235
  end
142
236
  end
143
237
 
144
- context "with :message option" do
145
- context "without interpolation" do
238
+ context "with a disallowed type" do
239
+ context "as a string" do
146
240
  setup do
147
- build_validator :content_type => "image/png", :message => "should be a PNG image"
148
- @dummy.stubs(:avatar_content_type => "image/jpg")
241
+ build_validator :not => "image/png"
242
+ @dummy.stubs(:avatar_content_type => "image/png")
149
243
  @validator.validate(@dummy)
150
244
  end
151
245
 
152
- should "set a correct error message" do
153
- assert_includes @dummy.errors[:avatar_content_type], "should be a PNG image"
246
+ should "set a correct default error message" do
247
+ assert @dummy.errors[:avatar_content_type].present?
248
+ assert_includes @dummy.errors[:avatar_content_type], "is invalid"
154
249
  end
155
250
  end
156
251
 
157
- context "with interpolation" do
252
+ context "as a regexp" do
158
253
  setup do
159
- build_validator :content_type => "image/png", :message => "should have content type %{types}"
160
- @dummy.stubs(:avatar_content_type => "image/jpg")
254
+ build_validator :not => /^text\/.*/
255
+ @dummy.stubs(:avatar_content_type => "text/plain")
161
256
  @validator.validate(@dummy)
162
257
  end
163
258
 
164
- should "set a correct error message" do
165
- assert_includes @dummy.errors[:avatar_content_type], "should have content type image/png"
259
+ should "set a correct default error message" do
260
+ assert @dummy.errors[:avatar_content_type].present?
261
+ assert_includes @dummy.errors[:avatar_content_type], "is invalid"
262
+ end
263
+ end
264
+
265
+ context "with :message option" do
266
+ context "without interpolation" do
267
+ setup do
268
+ build_validator :not => "image/png", :message => "should not be a PNG image"
269
+ @dummy.stubs(:avatar_content_type => "image/png")
270
+ @validator.validate(@dummy)
271
+ end
272
+
273
+ should "set a correct error message" do
274
+ assert_includes @dummy.errors[:avatar_content_type], "should not be a PNG image"
275
+ end
276
+ end
277
+
278
+ context "with interpolation" do
279
+ setup do
280
+ build_validator :not => "image/png", :message => "should not have content type %{types}"
281
+ @dummy.stubs(:avatar_content_type => "image/png")
282
+ @validator.validate(@dummy)
283
+ end
284
+
285
+ should "set a correct error message" do
286
+ assert_includes @dummy.errors[:avatar_content_type], "should not have content type image/png"
287
+ end
166
288
  end
167
289
  end
168
290
  end
@@ -185,8 +307,12 @@ class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
185
307
  end
186
308
  end
187
309
 
188
- should "not raise arguemnt error if :content_type was given" do
310
+ should "not raise argument error if :content_type was given" do
189
311
  build_validator :content_type => "image/jpg"
190
312
  end
313
+
314
+ should "not raise argument error if :not was given" do
315
+ build_validator :not => "image/jpg"
316
+ end
191
317
  end
192
318
  end
@@ -20,6 +20,11 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
20
20
  assert @dummy.errors[:avatar_file_size].blank?,
21
21
  "Expect an error message on :avatar_file_size, got none."
22
22
  end
23
+
24
+ should "not add error to the base dummy object" do
25
+ assert @dummy.errors[:avatar].blank?,
26
+ "Error added to base attribute"
27
+ end
23
28
  end
24
29
  end
25
30
 
@@ -35,6 +40,11 @@ class AttachmentSizeValidatorTest < Test::Unit::TestCase
35
40
  "Unexpected error message on :avatar_file_size"
36
41
  end
37
42
 
43
+ should "add error to the base dummy object" do
44
+ assert @dummy.errors[:avatar].present?,
45
+ "Error not added to base attribute"
46
+ end
47
+
38
48
  if options[:message]
39
49
  should "return a correct error message" do
40
50
  assert_includes @dummy.errors[:avatar_file_size], options[:message]
@@ -7,7 +7,7 @@ class ValidatorsTest < Test::Unit::TestCase
7
7
 
8
8
  context "using the helper" do
9
9
  setup do
10
- Dummy.validates_attachment :avatar, :presence => true, :content_type => { :content_type => "image/jpg" }, :size => { :in => 0..10.kilobytes }
10
+ Dummy.validates_attachment :avatar, :presence => true, :content_type => { :content_type => "image/jpeg" }, :size => { :in => 0..10.kilobytes }
11
11
  end
12
12
 
13
13
  should "add the attachment_presence validator to the class" do
@@ -21,5 +21,12 @@ class ValidatorsTest < Test::Unit::TestCase
21
21
  should "add the attachment_size validator to the class" do
22
22
  assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_size }
23
23
  end
24
+
25
+ should 'prevent you from attaching a file that violates that validation' do
26
+ Dummy.class_eval{ validate(:name) { raise "DO NOT RUN THIS" } }
27
+ dummy = Dummy.new(:avatar => File.new(fixture_file("12k.png")))
28
+ assert_equal [:avatar_content_type, :avatar, :avatar_file_size], dummy.errors.keys
29
+ assert_raise(RuntimeError){ dummy.valid? }
30
+ end
24
31
  end
25
32
  end