dm-paperclip 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/Gemfile +29 -0
  2. data/Gemfile.lock +100 -0
  3. data/README.md +145 -0
  4. data/Rakefile +37 -71
  5. data/VERSION +1 -0
  6. data/dm-paperclip.gemspec +103 -0
  7. data/lib/dm-paperclip.rb +88 -74
  8. data/lib/dm-paperclip/attachment.rb +139 -102
  9. data/lib/dm-paperclip/callbacks.rb +55 -0
  10. data/lib/dm-paperclip/command_line.rb +86 -0
  11. data/lib/dm-paperclip/ext/blank.rb +24 -0
  12. data/lib/dm-paperclip/ext/class.rb +50 -0
  13. data/lib/dm-paperclip/ext/compatibility.rb +11 -0
  14. data/lib/dm-paperclip/ext/try_dup.rb +12 -0
  15. data/lib/dm-paperclip/geometry.rb +3 -5
  16. data/lib/dm-paperclip/interpolations.rb +57 -32
  17. data/lib/dm-paperclip/iostream.rb +12 -26
  18. data/lib/dm-paperclip/processor.rb +14 -4
  19. data/lib/dm-paperclip/storage.rb +2 -257
  20. data/lib/dm-paperclip/storage/filesystem.rb +73 -0
  21. data/lib/dm-paperclip/storage/s3.rb +209 -0
  22. data/lib/dm-paperclip/storage/s3/aws_library.rb +41 -0
  23. data/lib/dm-paperclip/storage/s3/aws_s3_library.rb +60 -0
  24. data/lib/dm-paperclip/style.rb +90 -0
  25. data/lib/dm-paperclip/thumbnail.rb +33 -24
  26. data/lib/dm-paperclip/upfile.rb +13 -5
  27. data/lib/dm-paperclip/validations.rb +40 -37
  28. data/lib/dm-paperclip/version.rb +4 -0
  29. data/test/attachment_test.rb +510 -67
  30. data/test/command_line_test.rb +138 -0
  31. data/test/fixtures/s3.yml +8 -0
  32. data/test/fixtures/twopage.pdf +0 -0
  33. data/test/fixtures/uppercase.PNG +0 -0
  34. data/test/geometry_test.rb +54 -19
  35. data/test/helper.rb +91 -28
  36. data/test/integration_test.rb +252 -79
  37. data/test/interpolations_test.rb +150 -0
  38. data/test/iostream_test.rb +8 -15
  39. data/test/paperclip_test.rb +222 -69
  40. data/test/processor_test.rb +10 -0
  41. data/test/storage_test.rb +102 -23
  42. data/test/style_test.rb +141 -0
  43. data/test/thumbnail_test.rb +106 -18
  44. data/test/upfile_test.rb +36 -0
  45. metadata +136 -121
  46. data/README.rdoc +0 -116
  47. data/init.rb +0 -1
  48. data/lib/dm-paperclip/callback_compatability.rb +0 -33
@@ -0,0 +1,150 @@
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 id of the attachment" do
54
+ attachment = mock
55
+ attachment.expects(:id).returns(23)
56
+ attachment.expects(:instance).returns(attachment)
57
+ assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
58
+ end
59
+
60
+ should "return the partitioned id of the attachment" do
61
+ attachment = mock
62
+ attachment.expects(:id).returns(23)
63
+ attachment.expects(:instance).returns(attachment)
64
+ assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
65
+ end
66
+
67
+ should "return the name of the attachment" do
68
+ attachment = mock
69
+ attachment.expects(:name).returns("file")
70
+ assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
71
+ end
72
+
73
+ should "return the style" do
74
+ assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
75
+ end
76
+
77
+ should "return the default style" do
78
+ attachment = mock
79
+ attachment.expects(:default_style).returns(:default_style)
80
+ assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
81
+ end
82
+
83
+ should "reinterpolate :url" do
84
+ attachment = mock
85
+ attachment.expects(:url).with(:style, false).returns("1234")
86
+ assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
87
+ end
88
+
89
+ should "raise if infinite loop detcted reinterpolating :url" do
90
+ attachment = Object.new
91
+ class << attachment
92
+ def url(*args)
93
+ Paperclip::Interpolations.url(self, :style)
94
+ end
95
+ end
96
+ assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
97
+ end
98
+
99
+ should "return the filename as basename.extension" do
100
+ attachment = mock
101
+ attachment.expects(:styles).returns({})
102
+ attachment.expects(:original_filename).returns("one.jpg").times(3)
103
+ assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
104
+ end
105
+
106
+ should "return the filename as basename.extension when format supplied" do
107
+ attachment = mock
108
+ attachment.expects(:styles).returns({:style => {:format => :png}})
109
+ attachment.expects(:original_filename).returns("one.jpg").times(2)
110
+ assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
111
+ end
112
+
113
+ #should "return the timestamp" do
114
+ # now = Time.now
115
+ # zone = 'UTC'
116
+ # attachment = mock
117
+ # attachment.expects(:instance_read).with(:updated_at).returns(now)
118
+ # attachment.expects(:time_zone).returns(zone)
119
+ # assert_equal now.in_time_zone(zone).to_s, Paperclip::Interpolations.timestamp(attachment, :style)
120
+ #end
121
+
122
+ should "return the timestamp" do
123
+ now = Time.now
124
+ attachment = mock
125
+ attachment.expects(:instance_read).with(:updated_at).returns(now)
126
+ assert_equal now.to_s, Paperclip::Interpolations.timestamp(attachment, :style)
127
+ end
128
+
129
+ should "return updated_at" do
130
+ attachment = mock
131
+ seconds_since_epoch = 1234567890
132
+ attachment.expects(:updated_at).returns(seconds_since_epoch)
133
+ assert_equal seconds_since_epoch, Paperclip::Interpolations.updated_at(attachment, :style)
134
+ end
135
+
136
+ should "return hash" do
137
+ attachment = mock
138
+ fake_hash = "a_wicked_secure_hash"
139
+ attachment.expects(:hash).returns(fake_hash)
140
+ assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
141
+ end
142
+
143
+ should "call all expected interpolations with the given arguments" do
144
+ Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
145
+ Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
146
+ Paperclip::Interpolations.expects(:notreal).never
147
+ value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
148
+ assert_equal ":notreal/1234/attachments", value
149
+ end
150
+ end
@@ -1,14 +1,7 @@
1
- require 'test/helper'
1
+ require File.expand_path("./helper", File.dirname(__FILE__))
2
2
 
3
3
  class IOStreamTest < Test::Unit::TestCase
4
- context "IOStream" do
5
- should "be included in IO, File, Tempfile, and StringIO" do
6
- [IO, File, Tempfile, StringIO].each do |klass|
7
- assert klass.included_modules.include?(IOStream), "Not in #{klass}"
8
- end
9
- end
10
- end
11
-
4
+ include IOStream
12
5
  context "A file" do
13
6
  setup do
14
7
  @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@@ -21,7 +14,7 @@ class IOStreamTest < Test::Unit::TestCase
21
14
  context "and given a String" do
22
15
  setup do
23
16
  FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
24
- assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test'))
17
+ assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
25
18
  end
26
19
 
27
20
  should "return a File" do
@@ -38,7 +31,7 @@ class IOStreamTest < Test::Unit::TestCase
38
31
  setup do
39
32
  tempfile = Tempfile.new('iostream.test')
40
33
  tempfile.binmode
41
- assert @result = @file.stream_to(tempfile)
34
+ assert @result = stream_to(@file, tempfile)
42
35
  end
43
36
 
44
37
  should "return a Tempfile" do
@@ -53,9 +46,9 @@ class IOStreamTest < Test::Unit::TestCase
53
46
 
54
47
  end
55
48
 
56
- context "that is sent #to_tempfile" do
49
+ context "that is converted #to_tempfile" do
57
50
  setup do
58
- assert @tempfile = @file.to_tempfile
51
+ assert @tempfile = to_tempfile(@file)
59
52
  end
60
53
 
61
54
  should "convert it to a Paperclip Tempfile" do
@@ -66,7 +59,7 @@ class IOStreamTest < Test::Unit::TestCase
66
59
  name = File.basename(@file.path)
67
60
  extension = File.extname(name)
68
61
  basename = File.basename(name, extension)
69
- assert_match %r[^#{Regexp.quote(basename)}.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
62
+ assert_match %r[^stream.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
70
63
  end
71
64
 
72
65
  should "have the Tempfile contain the same data as the file" do
@@ -75,4 +68,4 @@ class IOStreamTest < Test::Unit::TestCase
75
68
  end
76
69
  end
77
70
  end
78
- end
71
+ end
@@ -1,12 +1,86 @@
1
- require 'test/helper.rb'
1
+ require File.expand_path("./helper", File.dirname(__FILE__))
2
2
 
3
3
  class PaperclipTest < Test::Unit::TestCase
4
+ context "Calling Paperclip.run" do
5
+ setup do
6
+ Paperclip.options[:image_magick_path] = nil
7
+ Paperclip.options[:command_path] = nil
8
+ Paperclip::CommandLine.stubs(:'`')
9
+ end
10
+
11
+ should "execute the right command with :image_magick_path" do
12
+ Paperclip.options[:image_magick_path] = "/usr/bin"
13
+ Paperclip.expects(:log).with(includes('[DEPRECATION]'))
14
+ Paperclip.expects(:log).with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
15
+ Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
16
+ Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
17
+ end
18
+
19
+ should "execute the right command with :command_path" do
20
+ Paperclip.options[:command_path] = "/usr/bin"
21
+ Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
22
+ Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
23
+ end
24
+
25
+ should "execute the right command with no path" do
26
+ Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{convert ['"]one.jpg['"] ['"]two.jpg['"]}))
27
+ Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
28
+ end
29
+
30
+ should "tell you the command isn't there if the shell returns 127" do
31
+ with_exitstatus_returning(127) do
32
+ assert_raises(Paperclip::CommandNotFoundError) do
33
+ Paperclip.run("command")
34
+ end
35
+ end
36
+ end
37
+
38
+ should "tell you the command isn't there if an ENOENT is raised" do
39
+ assert_raises(Paperclip::CommandNotFoundError) do
40
+ Paperclip::CommandLine.stubs(:"`").raises(Errno::ENOENT)
41
+ Paperclip.run("command")
42
+ end
43
+ end
44
+ end
45
+
46
+ context "Paperclip.each_instance_with_attachment" do
47
+ setup do
48
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
49
+ DataMapper.finalize
50
+ d1 = Dummy.create(:avatar => @file)
51
+ d2 = Dummy.create
52
+ d3 = Dummy.create(:avatar => @file)
53
+ @expected = [d1, d3]
54
+ end
55
+ should "yield every instance of a model that has an attachment" do
56
+ actual = []
57
+ Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
58
+ actual << instance
59
+ end
60
+ assert_same_elements @expected, actual
61
+ end
62
+ end
63
+
64
+ should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
65
+ assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
66
+ end
67
+
68
+ should "raise when sent #processor and the name of a class that doesn't exist" do
69
+ assert_raises(NameError){ Paperclip.processor(:boogey_man) }
70
+ end
71
+
72
+ should "return a class when sent #processor and the name of a class under Paperclip" do
73
+ assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
74
+ end
75
+
4
76
  context "A DataMapper model with an 'avatar' attachment" do
5
77
  setup do
6
78
  rebuild_model :path => "tmp/:class/omg/:style.:extension"
7
- @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
79
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
8
80
  end
9
81
 
82
+ teardown { @file.close }
83
+
10
84
  should "not error when trying to also create a 'blah' attachment" do
11
85
  assert_nothing_raised do
12
86
  Dummy.class_eval do
@@ -21,7 +95,7 @@ class PaperclipTest < Test::Unit::TestCase
21
95
  include DataMapper::Resource
22
96
  include DataMapper::Validate
23
97
  include Paperclip::Resource
24
- property :id, DataMapper::Types::Serial
98
+ property :id, DataMapper::Property::Serial
25
99
  property :other, String
26
100
  has_attached_file :file
27
101
  end
@@ -34,30 +108,31 @@ class PaperclipTest < Test::Unit::TestCase
34
108
  assert_equal [:avatar], Dummy.attachment_definitions.keys
35
109
  end
36
110
 
37
- context "that is write protected" do
38
- setup do
39
- Dummy.class_eval do
40
- has_attached_file :image, { :protected => true }
41
- end
42
- @dummy = Dummy.new
43
- end
111
+ #TODO: Check this test
112
+ # context "that is write protected" do
113
+ # setup do
114
+ # Dummy.class_eval do
115
+ # has_attached_file :image, { :protected => true }
116
+ # end
117
+ # @dummy = Dummy.new
118
+ # end
44
119
 
45
- should "not assign the avatar on mass-set" do
46
- @dummy.attributes = { :other => "I'm set!" } #,
47
- #:image => @file }
48
-
49
- assert_equal "I'm set!", @dummy.other
50
- assert ! @dummy.image?
51
- end
120
+ # should "not assign the avatar on mass-set" do
121
+ # @dummy.attributes = { :other => "I'm set!",
122
+ # :avatar => @file }
52
123
 
53
- should "still allow assigment on normal set" do
54
- @dummy.other = "I'm set!"
55
- @dummy.image = @file
56
-
57
- assert_equal "I'm set!", @dummy.other
58
- assert @dummy.image?
59
- end
60
- end
124
+ # assert_equal "I'm set!", @dummy.other
125
+ # assert ! @dummy.avatar?
126
+ # end
127
+
128
+ # should "still allow assigment on normal set" do
129
+ # @dummy.other = "I'm set!"
130
+ # @dummy.avatar = @file
131
+
132
+ # assert_equal "I'm set!", @dummy.other
133
+ # assert @dummy.avatar?
134
+ # end
135
+ # end
61
136
 
62
137
  context "with a subclass" do
63
138
  setup do
@@ -66,12 +141,13 @@ class PaperclipTest < Test::Unit::TestCase
66
141
 
67
142
  should "be able to use the attachment from the subclass" do
68
143
  assert_nothing_raised do
69
- @subdummy = SubDummy.create(:avatar => @file)
144
+ @subdummy = Dummy.create(:avatar => @file)
70
145
  end
71
146
  end
72
147
 
73
148
  should "be able to see the attachment definition from the subclass's class" do
74
- assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
149
+ assert_equal "tmp/:class/omg/:style.:extension",
150
+ SubDummy.attachment_definitions[:avatar][:path]
75
151
  end
76
152
 
77
153
  teardown do
@@ -96,68 +172,145 @@ class PaperclipTest < Test::Unit::TestCase
96
172
  should "be valid" do
97
173
  assert @dummy.valid?
98
174
  end
175
+ end
176
+
177
+ context "a validation with an if guard clause" do
178
+ setup do
179
+ Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
180
+ @dummy = Dummy.new
181
+ @dummy.stubs(:avatar_file_name).returns(nil)
182
+ end
183
+
184
+ should "attempt validation if the guard returns true" do
185
+ @dummy.expects(:foo).returns(true)
186
+ assert ! @dummy.valid?
187
+ end
188
+
189
+ should "not attempt validation if the guard returns false" do
190
+ @dummy.expects(:foo).returns(false)
191
+ assert @dummy.valid?
192
+ end
193
+ end
194
+
195
+ context "a validation with an unless guard clause" do
196
+ setup do
197
+ Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
198
+ @dummy = Dummy.new
199
+ @dummy.stubs(:avatar_file_name).returns(nil)
200
+ end
201
+
202
+ should "attempt validation if the guard returns true" do
203
+ @dummy.expects(:foo).returns(false)
204
+ assert ! @dummy.valid?
205
+ end
206
+
207
+ should "not attempt validation if the guard returns false" do
208
+ @dummy.expects(:foo).returns(true)
209
+ assert @dummy.valid?
210
+ end
211
+ end
99
212
 
100
- context "then has a validation added that makes it invalid" do
213
+ def self.should_validate validation, options, valid_file, invalid_file
214
+ context "with #{validation} validation and #{options.inspect} options" do
101
215
  setup do
102
- assert @dummy.save
103
- Dummy.class_eval do
104
- validates_attachment_content_type :avatar, :content_type => ["text/plain"]
216
+ rebuild_class
217
+ Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
218
+ @dummy = Dummy.new
219
+ end
220
+ context "and assigning nil" do
221
+ setup do
222
+ @dummy.avatar = nil
223
+ @dummy.valid?
224
+ end
225
+ if validation == :presence
226
+ should "have an error on the attachment" do
227
+ assert @dummy.errors[:avatar_file_name]
228
+ end
229
+ else
230
+ should "not have an error on the attachment" do
231
+ assert Paperclip::Ext.blank?(@dummy.errors), @dummy.errors.full_messages.join(", ")
232
+ end
105
233
  end
106
- @dummy2 = Dummy.get(@dummy.id)
107
234
  end
108
-
109
- should "be invalid when reloaded" do
110
- assert ! @dummy2.valid?, @dummy2.errors.inspect
235
+ context "and assigned a valid file" do
236
+ setup do
237
+ @dummy.avatar = valid_file
238
+ @dummy.valid?
239
+ end
240
+ should "not have an error when assigned a valid file" do
241
+ assert_equal 0, @dummy.errors.length, @dummy.errors.full_messages.join(", ")
242
+ end
111
243
  end
112
-
113
- should "be able to call #valid? twice without having duplicate errors" do
114
- @dummy2.avatar.valid?
115
- first_errors = @dummy2.avatar.errors
116
- @dummy2.avatar.valid?
117
- assert_equal first_errors, @dummy2.avatar.errors
244
+ context "and assigned an invalid file" do
245
+ setup do
246
+ @dummy.avatar = invalid_file
247
+ @dummy.valid?
248
+ end
249
+ should "have an error when assigned a valid file" do
250
+ assert @dummy.errors.length > 0
251
+ end
118
252
  end
119
253
  end
120
254
  end
121
255
 
122
- [[:presence, nil, "5k.png", nil],
256
+ [[:presence, {}, "5k.png", nil],
123
257
  [:size, {:in => 1..10240}, "5k.png", "12k.png"],
124
- [:size2, {:in => 1..10240}, nil, "12k.png"],
125
- [:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
126
- [:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
127
- [:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
128
- [:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
129
- context "with #{args[0]} validations" do
258
+ [:size, {:less_than => 10240}, "5k.png", "12k.png"],
259
+ [:size, {:greater_than => 8096}, "12k.png", "5k.png"],
260
+ [:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"],
261
+ [:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"],
262
+ [:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args|
263
+ validation, options, valid_file, invalid_file = args
264
+ valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb")
265
+ invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb")
266
+
267
+ should_validate validation, options, valid_file, invalid_file
268
+ end
269
+
270
+ context "with content_type validation and lambda message" do
271
+ context "and assigned an invalid file" do
130
272
  setup do
131
- Dummy.class_eval do
132
- send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
133
- end
273
+ Dummy.send(:"validates_attachment_content_type", :avatar, :content_type => %r{image/.*}, :message => lambda { |resource, property| 'lambda content type message' })
134
274
  @dummy = Dummy.new
275
+ @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "text.txt"), "rb")
276
+ @dummy.valid?
135
277
  end
136
278
 
137
- context "and a valid file" do
138
- setup do
139
- @file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
140
- end
279
+ should "have a content type error message" do
280
+ assert [@dummy.errors[:avatar]].flatten.any?{|error| error =~ %r/lambda content type message/ }
281
+ end
282
+ end
283
+ end
141
284
 
142
- should "not have any errors" do
143
- @dummy.avatar = @file
144
- assert @dummy.valid?
145
- assert_equal 0, @dummy.errors.length
146
- end
285
+ context "with size validation and less_than 10240 option" do
286
+ context "and assigned an invalid file" do
287
+ setup do
288
+ Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240)
289
+ @dummy = Dummy.new
290
+ @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
291
+ @dummy.valid?
147
292
  end
148
293
 
149
- context "and an invalid file" do
150
- setup do
151
- @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
152
- end
294
+ should "have a file size min/max error message" do
295
+ assert [@dummy.errors[:avatar]].flatten.any?{|error| error =~ %r/less than 10240 bytes/ }
296
+ end
297
+ end
298
+ end
153
299
 
154
- should "have errors" do
155
- @dummy.avatar = @file
156
- assert ! @dummy.valid?
157
- assert_equal 1, @dummy.errors.length
158
- end
300
+ context "with size validation and less_than 10240 option with lambda message" do
301
+ context "and assigned an invalid file" do
302
+ setup do
303
+ Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240, :message => lambda { |resource, property| 'lambda between 0 and 10240 bytes' })
304
+ @dummy = Dummy.new
305
+ @dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
306
+ @dummy.valid?
307
+ end
308
+
309
+ should "have a file size min/max error message" do
310
+ assert [@dummy.errors[:avatar]].flatten.any?{|error| error =~ %r/lambda between 0 and 10240 bytes/ }
159
311
  end
160
312
  end
161
313
  end
314
+
162
315
  end
163
316
  end