cloudfuji_paperclip 2.4.6 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +9 -6
- data/Appraisals +6 -6
- data/CONTRIBUTING.md +34 -2
- data/Gemfile +2 -0
- data/NEWS +90 -0
- data/README.md +62 -29
- data/RUNNING_TESTS.md +4 -0
- data/Rakefile +7 -2
- data/UPGRADING +14 -0
- data/features/basic_integration.feature +11 -7
- data/features/rake_tasks.feature +1 -1
- data/features/step_definitions/attachment_steps.rb +11 -2
- data/features/step_definitions/rails_steps.rb +17 -79
- data/features/support/env.rb +3 -0
- data/features/support/fakeweb.rb +7 -0
- data/features/support/file_helpers.rb +24 -0
- data/features/support/rails.rb +3 -3
- data/gemfiles/{rails3_1.gemfile → 3.0.gemfile} +3 -1
- data/gemfiles/{rails2.gemfile → 3.1.gemfile} +3 -1
- data/gemfiles/{rails3.gemfile → 3.2.gemfile} +3 -1
- data/images.rake +21 -0
- data/lib/cloudfuji_paperclip.rb +1 -0
- data/lib/generators/paperclip/paperclip_generator.rb +1 -2
- data/lib/paperclip.rb +54 -319
- data/lib/paperclip/attachment.rb +86 -107
- data/lib/paperclip/attachment_options.rb +9 -0
- data/lib/paperclip/callbacks.rb +30 -0
- data/lib/paperclip/errors.rb +27 -0
- data/lib/paperclip/geometry.rb +6 -4
- data/lib/paperclip/glue.rb +23 -0
- data/lib/paperclip/helpers.rb +71 -0
- data/lib/paperclip/instance_methods.rb +35 -0
- data/lib/paperclip/interpolations.rb +4 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +69 -0
- data/lib/paperclip/io_adapters/file_adapter.rb +81 -0
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
- data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
- data/lib/paperclip/io_adapters/registry.rb +32 -0
- data/lib/paperclip/io_adapters/stringio_adapter.rb +64 -0
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +63 -0
- data/lib/paperclip/locales/en.yml +17 -0
- data/lib/paperclip/logger.rb +21 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +5 -5
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -7
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +11 -11
- data/lib/paperclip/missing_attachment_styles.rb +6 -9
- data/lib/paperclip/processor.rb +32 -17
- data/lib/paperclip/railtie.rb +13 -17
- data/lib/paperclip/storage/filesystem.rb +4 -13
- data/lib/paperclip/storage/fog.rb +33 -24
- data/lib/paperclip/storage/s3.rb +36 -28
- data/lib/paperclip/tempfile.rb +41 -0
- data/lib/paperclip/thumbnail.rb +2 -3
- data/lib/paperclip/validators.rb +45 -0
- data/lib/paperclip/validators/attachment_content_type_validator.rb +54 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +26 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +102 -0
- data/lib/paperclip/version.rb +1 -1
- data/lib/tasks/paperclip.rake +4 -12
- data/paperclip.gemspec +15 -5
- data/test/adapter_registry_test.rb +32 -0
- data/test/attachment_adapter_test.rb +51 -0
- data/test/attachment_options_test.rb +27 -0
- data/test/attachment_test.rb +130 -46
- data/test/file_adapter_test.rb +88 -0
- data/test/generator_test.rb +78 -0
- data/test/geometry_test.rb +5 -5
- data/test/helper.rb +21 -22
- data/test/identity_adapter_test.rb +8 -0
- data/test/integration_test.rb +55 -102
- data/test/interpolations_test.rb +15 -5
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +23 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +21 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +37 -2
- data/test/nil_adapter_test.rb +25 -0
- data/test/paperclip_missing_attachment_styles_test.rb +16 -0
- data/test/paperclip_test.rb +34 -183
- data/test/storage/filesystem_test.rb +27 -27
- data/test/storage/fog_test.rb +68 -12
- data/test/storage/s3_live_test.rb +79 -38
- data/test/storage/s3_test.rb +204 -34
- data/test/stringio_adapter_test.rb +42 -0
- data/test/thumbnail_test.rb +29 -8
- data/test/uploaded_file_adapter_test.rb +98 -0
- data/test/url_generator_test.rb +8 -8
- data/test/validators/attachment_content_type_validator_test.rb +192 -0
- data/test/validators/attachment_presence_validator_test.rb +85 -0
- data/test/validators/attachment_size_validator_test.rb +207 -0
- data/test/validators_test.rb +25 -0
- metadata +166 -59
- data/generators/paperclip/USAGE +0 -5
- data/generators/paperclip/paperclip_generator.rb +0 -27
- data/generators/paperclip/templates/paperclip_migration.rb.erb +0 -19
- data/init.rb +0 -4
- data/lib/paperclip/callback_compatibility.rb +0 -61
- data/lib/paperclip/iostream.rb +0 -45
- data/lib/paperclip/upfile.rb +0 -62
- data/rails/init.rb +0 -2
- data/test/.gitignore +0 -1
- data/test/fixtures/question?mark.png +0 -0
- data/test/iostream_test.rb +0 -71
- data/test/upfile_test.rb +0 -53
data/test/interpolations_test.rb
CHANGED
@@ -46,8 +46,11 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
46
46
|
should "return the extension of the file as the format if defined in the style" do
|
47
47
|
attachment = mock
|
48
48
|
attachment.expects(:original_filename).never
|
49
|
-
attachment.expects(:styles).returns({:style => {:format => "png"}})
|
50
|
-
|
49
|
+
attachment.expects(:styles).twice.returns({:style => {:format => "png"}})
|
50
|
+
|
51
|
+
[:style, 'style'].each do |style|
|
52
|
+
assert_equal "png", Paperclip::Interpolations.extension(attachment, style)
|
53
|
+
end
|
51
54
|
end
|
52
55
|
|
53
56
|
should "return the extension of the file based on the content type" do
|
@@ -74,6 +77,13 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
74
77
|
assert_equal "found", interpolations.content_type_extension(attachment, :style)
|
75
78
|
end
|
76
79
|
|
80
|
+
should "be able to handle numeric style names" do
|
81
|
+
attachment = mock(
|
82
|
+
:styles => {:"4" => {:format => :expected_extension}}
|
83
|
+
)
|
84
|
+
assert_equal :expected_extension, Paperclip::Interpolations.extension(attachment, 4)
|
85
|
+
end
|
86
|
+
|
77
87
|
should "return the #to_param of the attachment" do
|
78
88
|
attachment = mock
|
79
89
|
attachment.expects(:to_param).returns("23-awesome")
|
@@ -145,7 +155,7 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
145
155
|
Paperclip::Interpolations.url(self, :style)
|
146
156
|
end
|
147
157
|
end
|
148
|
-
assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
|
158
|
+
assert_raises(Paperclip::Errors::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
|
149
159
|
end
|
150
160
|
|
151
161
|
should "return the filename as basename.extension" do
|
@@ -195,14 +205,14 @@ class InterpolationsTest < Test::Unit::TestCase
|
|
195
205
|
should "return attachment's hash when passing both arguments" do
|
196
206
|
attachment = mock
|
197
207
|
fake_hash = "a_wicked_secure_hash"
|
198
|
-
attachment.expects(:
|
208
|
+
attachment.expects(:hash_key).returns(fake_hash)
|
199
209
|
assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
|
200
210
|
end
|
201
211
|
|
202
212
|
should "return Object#hash when passing no argument" do
|
203
213
|
attachment = mock
|
204
214
|
fake_hash = "a_wicked_secure_hash"
|
205
|
-
attachment.expects(:
|
215
|
+
attachment.expects(:hash_key).never.returns(fake_hash)
|
206
216
|
assert_not_equal fake_hash, Paperclip::Interpolations.hash
|
207
217
|
end
|
208
218
|
|
@@ -83,5 +83,28 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
|
|
83
83
|
|
84
84
|
should_reject_dummy_class
|
85
85
|
end
|
86
|
+
|
87
|
+
context "using an :if to control the validation" do
|
88
|
+
setup do
|
89
|
+
@dummy_class.class_eval do
|
90
|
+
validates_attachment_content_type :avatar, :content_type => %r{image/*} , :if => :go
|
91
|
+
attr_accessor :go
|
92
|
+
end
|
93
|
+
@matcher = self.class.validate_attachment_content_type(:avatar).
|
94
|
+
allowing(%w(image/png image/jpeg)).
|
95
|
+
rejecting(%w(audio/mp3 application/octet-stream))
|
96
|
+
@dummy = @dummy_class.new
|
97
|
+
end
|
98
|
+
|
99
|
+
should "run the validation if the control is true" do
|
100
|
+
@dummy.go = true
|
101
|
+
assert_accepts @matcher, @dummy
|
102
|
+
end
|
103
|
+
|
104
|
+
should "not run the validation if the control is false" do
|
105
|
+
@dummy.go = false
|
106
|
+
assert_rejects @matcher, @dummy
|
107
|
+
end
|
108
|
+
end
|
86
109
|
end
|
87
110
|
end
|
@@ -22,5 +22,26 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
|
|
22
22
|
|
23
23
|
should_accept_dummy_class
|
24
24
|
end
|
25
|
+
|
26
|
+
context "using an :if to control the validation" do
|
27
|
+
setup do
|
28
|
+
@dummy_class.class_eval do
|
29
|
+
validates_attachment_presence :avatar, :if => :go
|
30
|
+
attr_accessor :go
|
31
|
+
end
|
32
|
+
@dummy = @dummy_class.new
|
33
|
+
@dummy.avatar = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
should "run the validation if the control is true" do
|
37
|
+
@dummy.go = true
|
38
|
+
assert_accepts @matcher, @dummy
|
39
|
+
end
|
40
|
+
|
41
|
+
should "not run the validation if the control is false" do
|
42
|
+
@dummy.go = false
|
43
|
+
assert_rejects @matcher, @dummy
|
44
|
+
end
|
45
|
+
end
|
25
46
|
end
|
26
47
|
end
|
@@ -34,7 +34,7 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
context "
|
37
|
+
context "allowing anything" do
|
38
38
|
setup{ @matcher = self.class.validate_attachment_size(:avatar) }
|
39
39
|
|
40
40
|
context "given a class with an upper limit" do
|
@@ -42,10 +42,45 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
|
|
42
42
|
should_accept_dummy_class
|
43
43
|
end
|
44
44
|
|
45
|
-
context "given a class with
|
45
|
+
context "given a class with a lower limit" do
|
46
46
|
setup { @dummy_class.validates_attachment_size :avatar, :greater_than => 1 }
|
47
47
|
should_accept_dummy_class
|
48
48
|
end
|
49
49
|
end
|
50
|
+
|
51
|
+
context "using an :if to control the validation" do
|
52
|
+
setup do
|
53
|
+
@dummy_class.class_eval do
|
54
|
+
validates_attachment_size :avatar, :greater_than => 1024, :if => :go
|
55
|
+
attr_accessor :go
|
56
|
+
end
|
57
|
+
@dummy = @dummy_class.new
|
58
|
+
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
|
59
|
+
end
|
60
|
+
|
61
|
+
should "run the validation if the control is true" do
|
62
|
+
@dummy.go = true
|
63
|
+
assert_accepts @matcher, @dummy
|
64
|
+
end
|
65
|
+
|
66
|
+
should "not run the validation if the control is false" do
|
67
|
+
@dummy.go = false
|
68
|
+
assert_rejects @matcher, @dummy
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "post processing" do
|
73
|
+
setup do
|
74
|
+
@dummy_class.validates_attachment_size :avatar, :greater_than => 1024
|
75
|
+
|
76
|
+
@dummy = @dummy_class.new
|
77
|
+
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "be skipped" do
|
81
|
+
@dummy.avatar.expects(:post_process).never
|
82
|
+
assert_accepts @matcher, @dummy
|
83
|
+
end
|
84
|
+
end
|
50
85
|
end
|
51
86
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class NilAdapterTest < Test::Unit::TestCase
|
4
|
+
context 'a new instance' do
|
5
|
+
setup do
|
6
|
+
@subject = Paperclip.io_adapters.for(nil)
|
7
|
+
end
|
8
|
+
|
9
|
+
should "get the right filename" do
|
10
|
+
assert_equal "", @subject.original_filename
|
11
|
+
end
|
12
|
+
|
13
|
+
should "get the content type" do
|
14
|
+
assert_equal "", @subject.content_type
|
15
|
+
end
|
16
|
+
|
17
|
+
should "get the file's size" do
|
18
|
+
assert_equal 0, @subject.size
|
19
|
+
end
|
20
|
+
|
21
|
+
should "return true for a call to nil?" do
|
22
|
+
assert @subject.nil?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -68,6 +68,22 @@ class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
|
|
68
68
|
Paperclip.save_current_attachments_styles!
|
69
69
|
assert_equal Hash.new, Paperclip.missing_attachments_styles
|
70
70
|
end
|
71
|
+
|
72
|
+
should "be able to calculate differences when a new attachment is added to a model" do
|
73
|
+
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
|
74
|
+
Paperclip.save_current_attachments_styles!
|
75
|
+
|
76
|
+
class ::Dummy
|
77
|
+
has_attached_file :photo, :styles => {:small => 'x100', :large => '1000x1000>'}
|
78
|
+
end
|
79
|
+
|
80
|
+
expected_hash = {
|
81
|
+
:Dummy => {:photo => [:large, :small]}
|
82
|
+
}
|
83
|
+
assert_equal expected_hash, Paperclip.missing_attachments_styles
|
84
|
+
Paperclip.save_current_attachments_styles!
|
85
|
+
assert_equal Hash.new, Paperclip.missing_attachments_styles
|
86
|
+
end
|
71
87
|
|
72
88
|
# It's impossible to build styles hash without loading from database whole bunch of records
|
73
89
|
should "skip lambda-styles" do
|
data/test/paperclip_test.rb
CHANGED
@@ -22,8 +22,34 @@ class PaperclipTest < Test::Unit::TestCase
|
|
22
22
|
Paperclip.run("convert", "stuff")
|
23
23
|
assert_equal [Cocaine::CommandLine.path].flatten.include?("/opt/my_app/bin"), true
|
24
24
|
end
|
25
|
+
|
26
|
+
should "not duplicate Cocaine::CommandLine.path on multiple runs" do
|
27
|
+
Cocaine::CommandLine.expects(:new).with("convert", "more_stuff", {}).returns(stub(:run))
|
28
|
+
Cocaine::CommandLine.path = nil
|
29
|
+
Paperclip.options[:command_path] = "/opt/my_app/bin"
|
30
|
+
Paperclip.run("convert", "stuff")
|
31
|
+
Paperclip.run("convert", "more_stuff")
|
32
|
+
assert_equal 1, [Cocaine::CommandLine.path].flatten.size
|
33
|
+
end
|
25
34
|
end
|
26
35
|
|
36
|
+
context "Calling Paperclip.log without options[:logger] set" do
|
37
|
+
setup do
|
38
|
+
Paperclip.logger = nil
|
39
|
+
Paperclip.options[:logger] = nil
|
40
|
+
end
|
41
|
+
|
42
|
+
teardown do
|
43
|
+
Paperclip.options[:logger] = ActiveRecord::Base.logger
|
44
|
+
Paperclip.logger = ActiveRecord::Base.logger
|
45
|
+
end
|
46
|
+
|
47
|
+
should "not raise an error when log is called" do
|
48
|
+
silence_stream(STDOUT) do
|
49
|
+
Paperclip.log('something')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
27
53
|
context "Calling Paperclip.run with a logger" do
|
28
54
|
should "pass the defined logger if :log_command is set" do
|
29
55
|
Paperclip.options[:log_command] = true
|
@@ -40,6 +66,9 @@ class PaperclipTest < Test::Unit::TestCase
|
|
40
66
|
d3 = Dummy.create(:avatar => @file)
|
41
67
|
@expected = [d1, d3]
|
42
68
|
end
|
69
|
+
|
70
|
+
teardown { @file.close }
|
71
|
+
|
43
72
|
should "yield every instance of a model that has an attachment" do
|
44
73
|
actual = []
|
45
74
|
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
|
@@ -78,12 +107,13 @@ class PaperclipTest < Test::Unit::TestCase
|
|
78
107
|
end
|
79
108
|
|
80
109
|
should "generate warning if attachment is redefined with the same url string" do
|
81
|
-
|
110
|
+
expected_log_msg = "Duplicate URL for blah with /system/:id/:style/:filename. This will clash with attachment defined in Dummy class"
|
111
|
+
Paperclip.expects(:log).with(expected_log_msg)
|
82
112
|
Dummy.class_eval do
|
83
|
-
has_attached_file :blah
|
113
|
+
has_attached_file :blah, :url => '/system/:id/:style/:filename'
|
84
114
|
end
|
85
115
|
Dummy2.class_eval do
|
86
|
-
has_attached_file :blah
|
116
|
+
has_attached_file :blah, :url => '/system/:id/:style/:filename'
|
87
117
|
end
|
88
118
|
end
|
89
119
|
|
@@ -156,6 +186,7 @@ class PaperclipTest < Test::Unit::TestCase
|
|
156
186
|
end
|
157
187
|
|
158
188
|
teardown do
|
189
|
+
SubDummy.delete_all
|
159
190
|
Object.send(:remove_const, "SubDummy") rescue nil
|
160
191
|
end
|
161
192
|
end
|
@@ -179,191 +210,11 @@ class PaperclipTest < Test::Unit::TestCase
|
|
179
210
|
end
|
180
211
|
end
|
181
212
|
|
182
|
-
context "a validation with an if guard clause" do
|
183
|
-
context "as a lambda" do
|
184
|
-
setup do
|
185
|
-
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
|
186
|
-
@dummy = Dummy.new
|
187
|
-
@dummy.stubs(:avatar_file_name).returns(nil)
|
188
|
-
end
|
189
|
-
|
190
|
-
should "attempt validation if the guard returns true" do
|
191
|
-
@dummy.expects(:foo).returns(true)
|
192
|
-
assert ! @dummy.valid?
|
193
|
-
end
|
194
|
-
|
195
|
-
should "not attempt validation if the guard returns false" do
|
196
|
-
@dummy.expects(:foo).returns(false)
|
197
|
-
assert @dummy.valid?
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
context "as a method name" do
|
202
|
-
setup do
|
203
|
-
Dummy.send(:"validates_attachment_presence", :avatar, :if => :foo)
|
204
|
-
@dummy = Dummy.new
|
205
|
-
@dummy.stubs(:avatar_file_name).returns(nil)
|
206
|
-
end
|
207
|
-
|
208
|
-
should "attempt validation if the guard returns true" do
|
209
|
-
@dummy.expects(:foo).returns(true)
|
210
|
-
assert ! @dummy.valid?
|
211
|
-
end
|
212
|
-
|
213
|
-
should "not attempt validation if the guard returns false" do
|
214
|
-
@dummy.expects(:foo).returns(false)
|
215
|
-
assert @dummy.valid?
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
context "a validation with an unless guard clause" do
|
221
|
-
context "as a lambda" do
|
222
|
-
setup do
|
223
|
-
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
|
224
|
-
@dummy = Dummy.new
|
225
|
-
@dummy.stubs(:avatar_file_name).returns(nil)
|
226
|
-
end
|
227
|
-
|
228
|
-
should "attempt validation if the guard returns true" do
|
229
|
-
@dummy.expects(:foo).returns(false)
|
230
|
-
assert ! @dummy.valid?
|
231
|
-
end
|
232
|
-
|
233
|
-
should "not attempt validation if the guard returns false" do
|
234
|
-
@dummy.expects(:foo).returns(true)
|
235
|
-
assert @dummy.valid?
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
context "as a method name" do
|
240
|
-
setup do
|
241
|
-
Dummy.send(:"validates_attachment_presence", :avatar, :unless => :foo)
|
242
|
-
@dummy = Dummy.new
|
243
|
-
@dummy.stubs(:avatar_file_name).returns(nil)
|
244
|
-
end
|
245
|
-
|
246
|
-
should "attempt validation if the guard returns true" do
|
247
|
-
@dummy.expects(:foo).returns(false)
|
248
|
-
assert ! @dummy.valid?
|
249
|
-
end
|
250
|
-
|
251
|
-
should "not attempt validation if the guard returns false" do
|
252
|
-
@dummy.expects(:foo).returns(true)
|
253
|
-
assert @dummy.valid?
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
213
|
should "not have Attachment in the ActiveRecord::Base namespace" do
|
259
214
|
assert_raises(NameError) do
|
260
215
|
ActiveRecord::Base::Attachment
|
261
216
|
end
|
262
217
|
end
|
263
|
-
|
264
|
-
def self.should_validate validation, options, valid_file, invalid_file
|
265
|
-
context "with #{validation} validation and #{options.inspect} options" do
|
266
|
-
setup do
|
267
|
-
rebuild_class
|
268
|
-
Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
|
269
|
-
@dummy = Dummy.new
|
270
|
-
end
|
271
|
-
context "and assigning nil" do
|
272
|
-
setup do
|
273
|
-
@dummy.avatar = nil
|
274
|
-
@dummy.valid?
|
275
|
-
end
|
276
|
-
if validation == :presence
|
277
|
-
should "have an error on the attachment" do
|
278
|
-
assert @dummy.errors[:avatar]
|
279
|
-
assert @dummy.errors[:avatar_file_name]
|
280
|
-
end
|
281
|
-
else
|
282
|
-
should "not have an error on the attachment" do
|
283
|
-
assert @dummy.errors.blank?, @dummy.errors.full_messages.join(", ")
|
284
|
-
end
|
285
|
-
end
|
286
|
-
end
|
287
|
-
context "and assigned a valid file" do
|
288
|
-
setup do
|
289
|
-
@dummy.avatar = valid_file
|
290
|
-
@dummy.valid?
|
291
|
-
end
|
292
|
-
should "not have an error" do
|
293
|
-
assert_equal 0, @dummy.errors.size, @dummy.errors.full_messages.join(", ")
|
294
|
-
end
|
295
|
-
end
|
296
|
-
context "and assigned an invalid file" do
|
297
|
-
setup do
|
298
|
-
@dummy.avatar = invalid_file
|
299
|
-
@dummy.valid?
|
300
|
-
end
|
301
|
-
should "have an error" do
|
302
|
-
assert @dummy.errors.size > 0
|
303
|
-
end
|
304
|
-
end
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
[[:presence, {}, "5k.png", nil],
|
309
|
-
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
|
310
|
-
[:size, {:less_than => 10240}, "5k.png", "12k.png"],
|
311
|
-
[:size, {:greater_than => 8096}, "12k.png", "5k.png"],
|
312
|
-
[:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"],
|
313
|
-
[:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"],
|
314
|
-
[:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args|
|
315
|
-
validation, options, valid_file, invalid_file = args
|
316
|
-
valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb")
|
317
|
-
invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb")
|
318
|
-
|
319
|
-
should_validate validation, options, valid_file, invalid_file
|
320
|
-
end
|
321
|
-
|
322
|
-
context "with content_type validation and lambda message" do
|
323
|
-
context "and assigned an invalid file" do
|
324
|
-
setup do
|
325
|
-
Dummy.send(:"validates_attachment_content_type", :avatar, :content_type => %r{image/.*}, :message => lambda {'lambda content type message'})
|
326
|
-
@dummy = Dummy.new
|
327
|
-
@dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "text.txt"), "rb")
|
328
|
-
@dummy.valid?
|
329
|
-
end
|
330
|
-
|
331
|
-
should "have a content type error message" do
|
332
|
-
assert [@dummy.errors[:avatar_content_type]].flatten.any?{|error| error =~ %r/lambda content type message/ }
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
context "with size validation and less_than 10240 option" do
|
338
|
-
context "and assigned an invalid file" do
|
339
|
-
setup do
|
340
|
-
Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240)
|
341
|
-
@dummy = Dummy.new
|
342
|
-
@dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
|
343
|
-
@dummy.valid?
|
344
|
-
end
|
345
|
-
|
346
|
-
should "have a file size min/max error message" do
|
347
|
-
assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/between 0 and 10240 bytes/ }
|
348
|
-
end
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
context "with size validation and less_than 10240 option with lambda message" do
|
353
|
-
context "and assigned an invalid file" do
|
354
|
-
setup do
|
355
|
-
Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240, :message => lambda {'lambda between 0 and 10240 bytes'})
|
356
|
-
@dummy = Dummy.new
|
357
|
-
@dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
|
358
|
-
@dummy.valid?
|
359
|
-
end
|
360
|
-
|
361
|
-
should "have a file size min/max error message" do
|
362
|
-
assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/lambda between 0 and 10240 bytes/ }
|
363
|
-
end
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
218
|
end
|
368
219
|
|
369
220
|
context "configuring a custom processor" do
|