carrierwave-rails3 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/README.rdoc +527 -0
  2. data/lib/carrierwave.rb +103 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/core_ext/file.rb +11 -0
  5. data/lib/carrierwave/mount.rb +359 -0
  6. data/lib/carrierwave/orm/activerecord.rb +75 -0
  7. data/lib/carrierwave/orm/datamapper.rb +27 -0
  8. data/lib/carrierwave/orm/mongoid.rb +23 -0
  9. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  10. data/lib/carrierwave/orm/sequel.rb +45 -0
  11. data/lib/carrierwave/processing/image_science.rb +116 -0
  12. data/lib/carrierwave/processing/mini_magick.rb +261 -0
  13. data/lib/carrierwave/processing/rmagick.rb +278 -0
  14. data/lib/carrierwave/sanitized_file.rb +273 -0
  15. data/lib/carrierwave/storage/abstract.rb +30 -0
  16. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  17. data/lib/carrierwave/storage/file.rb +48 -0
  18. data/lib/carrierwave/storage/grid_fs.rb +104 -0
  19. data/lib/carrierwave/storage/right_s3.rb +3 -0
  20. data/lib/carrierwave/storage/s3.rb +206 -0
  21. data/lib/carrierwave/test/matchers.rb +164 -0
  22. data/lib/carrierwave/uploader.rb +44 -0
  23. data/lib/carrierwave/uploader/cache.rb +146 -0
  24. data/lib/carrierwave/uploader/callbacks.rb +41 -0
  25. data/lib/carrierwave/uploader/configuration.rb +134 -0
  26. data/lib/carrierwave/uploader/default_url.rb +19 -0
  27. data/lib/carrierwave/uploader/download.rb +60 -0
  28. data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
  29. data/lib/carrierwave/uploader/mountable.rb +39 -0
  30. data/lib/carrierwave/uploader/processing.rb +84 -0
  31. data/lib/carrierwave/uploader/proxy.rb +62 -0
  32. data/lib/carrierwave/uploader/remove.rb +23 -0
  33. data/lib/carrierwave/uploader/store.rb +90 -0
  34. data/lib/carrierwave/uploader/url.rb +33 -0
  35. data/lib/carrierwave/uploader/versions.rb +147 -0
  36. data/lib/generators/templates/uploader.rb +47 -0
  37. data/lib/generators/uploader_generator.rb +13 -0
  38. data/spec/compatibility/paperclip_spec.rb +52 -0
  39. data/spec/mount_spec.rb +538 -0
  40. data/spec/orm/activerecord_spec.rb +271 -0
  41. data/spec/orm/datamapper_spec.rb +168 -0
  42. data/spec/orm/mongoid_spec.rb +202 -0
  43. data/spec/orm/mongomapper_spec.rb +202 -0
  44. data/spec/orm/sequel_spec.rb +183 -0
  45. data/spec/processing/image_science_spec.rb +56 -0
  46. data/spec/processing/mini_magick_spec.rb +76 -0
  47. data/spec/processing/rmagick_spec.rb +75 -0
  48. data/spec/sanitized_file_spec.rb +623 -0
  49. data/spec/spec_helper.rb +92 -0
  50. data/spec/storage/cloudfiles_spec.rb +78 -0
  51. data/spec/storage/grid_fs_spec.rb +86 -0
  52. data/spec/storage/s3_spec.rb +118 -0
  53. data/spec/uploader/cache_spec.rb +209 -0
  54. data/spec/uploader/callback_spec.rb +24 -0
  55. data/spec/uploader/configuration_spec.rb +105 -0
  56. data/spec/uploader/default_url_spec.rb +85 -0
  57. data/spec/uploader/download_spec.rb +75 -0
  58. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  59. data/spec/uploader/mountable_spec.rb +33 -0
  60. data/spec/uploader/paths_spec.rb +22 -0
  61. data/spec/uploader/processing_spec.rb +73 -0
  62. data/spec/uploader/proxy_spec.rb +54 -0
  63. data/spec/uploader/remove_spec.rb +70 -0
  64. data/spec/uploader/store_spec.rb +264 -0
  65. data/spec/uploader/url_spec.rb +102 -0
  66. data/spec/uploader/versions_spec.rb +298 -0
  67. metadata +128 -0
@@ -0,0 +1,147 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Versions
6
+ extend ActiveSupport::Concern
7
+
8
+ include CarrierWave::Uploader::Callbacks
9
+
10
+ included do
11
+ after :cache, :cache_versions!
12
+ after :store, :store_versions!
13
+ after :remove, :remove_versions!
14
+ after :retrieve_from_cache, :retrieve_versions_from_cache!
15
+ after :retrieve_from_store, :retrieve_versions_from_store!
16
+ end
17
+
18
+ module ClassMethods
19
+
20
+ def version_names
21
+ @version_names ||= []
22
+ end
23
+
24
+ ##
25
+ # Adds a new version to this uploader
26
+ #
27
+ # === Parameters
28
+ #
29
+ # [name (#to_sym)] name of the version
30
+ # [&block (Proc)] a block to eval on this version of the uploader
31
+ #
32
+ def version(name, &block)
33
+ name = name.to_sym
34
+ unless versions[name]
35
+ versions[name] = Class.new(self)
36
+ versions[name].version_names.push(*version_names)
37
+ versions[name].version_names.push(name)
38
+ class_eval <<-RUBY
39
+ def #{name}
40
+ versions[:#{name}]
41
+ end
42
+ RUBY
43
+ end
44
+ versions[name].class_eval(&block) if block
45
+ versions[name]
46
+ end
47
+
48
+ ##
49
+ # === Returns
50
+ #
51
+ # [Hash{Symbol => Class}] a list of versions available for this uploader
52
+ #
53
+ def versions
54
+ @versions ||= {}
55
+ end
56
+
57
+ end # ClassMethods
58
+
59
+ ##
60
+ # Returns a hash mapping the name of each version of the uploader to an instance of it
61
+ #
62
+ # === Returns
63
+ #
64
+ # [Hash{Symbol => CarrierWave::Uploader}] a list of uploader instances
65
+ #
66
+ def versions
67
+ return @versions if @versions
68
+ @versions = {}
69
+ self.class.versions.each do |name, klass|
70
+ @versions[name] = klass.new(model, mounted_as)
71
+ end
72
+ @versions
73
+ end
74
+
75
+ ##
76
+ # === Returns
77
+ #
78
+ # [String] the name of this version of the uploader
79
+ #
80
+ def version_name
81
+ self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
82
+ end
83
+
84
+ ##
85
+ # When given a version name as a parameter, will return the url for that version
86
+ # This also works with nested versions.
87
+ #
88
+ # === Example
89
+ #
90
+ # my_uploader.url # => /path/to/my/uploader.gif
91
+ # my_uploader.url(:thumb) # => /path/to/my/thumb_uploader.gif
92
+ # my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
93
+ #
94
+ # === Parameters
95
+ #
96
+ # [*args (Symbol)] any number of versions
97
+ #
98
+ # === Returns
99
+ #
100
+ # [String] the location where this file is accessible via a url
101
+ #
102
+ def url(*args)
103
+ if(args.first)
104
+ raise ArgumentError, "Version #{args.first} doesn't exist!" if versions[args.first.to_sym].nil?
105
+ # recursively proxy to version
106
+ versions[args.first.to_sym].url(*args[1..-1])
107
+ else
108
+ super()
109
+ end
110
+ end
111
+
112
+ private
113
+
114
+ def full_filename(for_file)
115
+ [version_name, super(for_file)].compact.join('_')
116
+ end
117
+
118
+ def full_original_filename
119
+ [version_name, super].compact.join('_')
120
+ end
121
+
122
+ def cache_versions!(new_file)
123
+ versions.each do |name, v|
124
+ v.send(:cache_id=, cache_id)
125
+ v.cache!(new_file)
126
+ end
127
+ end
128
+
129
+ def store_versions!(new_file)
130
+ versions.each { |name, v| v.store!(new_file) }
131
+ end
132
+
133
+ def remove_versions!
134
+ versions.each { |name, v| v.remove! }
135
+ end
136
+
137
+ def retrieve_versions_from_cache!(cache_name)
138
+ versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
139
+ end
140
+
141
+ def retrieve_versions_from_store!(identifier)
142
+ versions.each { |name, v| v.retrieve_from_store!(identifier) }
143
+ end
144
+
145
+ end # Versions
146
+ end # Uploader
147
+ end # CarrierWave
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ class <%= class_name %>Uploader < CarrierWave::Uploader::Base
4
+
5
+ # Include RMagick or ImageScience support
6
+ # include CarrierWave::RMagick
7
+ # include CarrierWave::ImageScience
8
+
9
+ # Choose what kind of storage to use for this uploader
10
+ storage :file
11
+ # storage :s3
12
+
13
+ # Override the directory where uploaded files will be stored
14
+ # This is a sensible default for uploaders that are meant to be mounted:
15
+ def store_dir
16
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
17
+ end
18
+
19
+ # Provide a default URL as a default if there hasn't been a file uploaded
20
+ # def default_url
21
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
22
+ # end
23
+
24
+ # Process files as they are uploaded.
25
+ # process :scale => [200, 300]
26
+ #
27
+ # def scale(width, height)
28
+ # # do something
29
+ # end
30
+
31
+ # Create different versions of your uploaded files
32
+ # version :thumb do
33
+ # process :scale => [50, 50]
34
+ # end
35
+
36
+ # Add a white list of extensions which are allowed to be uploaded,
37
+ # for images you might use something like this:
38
+ # def extension_white_list
39
+ # %w(jpg jpeg gif png)
40
+ # end
41
+
42
+ # Override the filename of the uploaded files
43
+ # def filename
44
+ # "something.jpg" if original_filename
45
+ # end
46
+
47
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/named_base'
3
+
4
+ class UploaderGenerator < Rails::Generators::NamedBase
5
+ def create_uploader_file
6
+ template 'uploader.rb', File.join('app/uploaders', class_path, "#{file_name}.rb")
7
+ end
8
+
9
+ def self.source_root
10
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
11
+ end
12
+ end
13
+
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ require 'carrierwave/orm/activerecord'
6
+
7
+ module Rails
8
+ def self.root
9
+ File.expand_path(File.join('..'), File.dirname(__FILE__))
10
+ end
11
+ def self.env
12
+ "test"
13
+ end
14
+ end unless defined?(Rails)
15
+
16
+ describe CarrierWave::Compatibility::Paperclip do
17
+
18
+ before do
19
+ @uploader_class = Class.new(CarrierWave::Uploader::Base) do
20
+ include CarrierWave::Compatibility::Paperclip
21
+ end
22
+ @model = mock('a model')
23
+ @model.stub!(:id).and_return(23)
24
+ @uploader = @uploader_class.new(@model, :monkey)
25
+ end
26
+
27
+ after do
28
+ FileUtils.rm_rf(public_path)
29
+ end
30
+
31
+ describe '#store_path' do
32
+ it "should mimics paperclip default" do
33
+ @uploader.store_path("monkey.png").should == CarrierWave::Uploader::Base.root + "/system/monkeys/23/original/monkey.png"
34
+ end
35
+
36
+ it "should interpolate the root path" do
37
+ @uploader.stub!(:paperclip_path).and_return(":rails_root/foo/bar")
38
+ @uploader.store_path("monkey.png").should == Rails.root + "/foo/bar"
39
+ end
40
+
41
+ it "should interpolate the attachment" do
42
+ @uploader.stub!(:paperclip_path).and_return("/foo/:attachment/bar")
43
+ @uploader.store_path("monkey.png").should == "/foo/monkeys/bar"
44
+ end
45
+
46
+ it "should interpolate the id" do
47
+ @uploader.stub!(:paperclip_path).and_return("/foo/:id/bar")
48
+ @uploader.store_path("monkey.png").should == "/foo/23/bar"
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,538 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ describe CarrierWave::Mount do
6
+
7
+ after do
8
+ FileUtils.rm_rf(public_path)
9
+ end
10
+
11
+ describe '.mount_uploader' do
12
+
13
+ before do
14
+ @class = Class.new
15
+ @class.send(:extend, CarrierWave::Mount)
16
+
17
+ @uploader = Class.new(CarrierWave::Uploader::Base)
18
+
19
+ @class.mount_uploader(:image, @uploader)
20
+ @instance = @class.new
21
+ end
22
+
23
+ it "should maintain the ability to super" do
24
+ @class.class_eval do
25
+ def image_uploader
26
+ super
27
+ end
28
+
29
+ def image=(val)
30
+ super
31
+ end
32
+ end
33
+
34
+ @instance.image = stub_file('test.jpg')
35
+ @instance.image.should be_an_instance_of(@uploader)
36
+ end
37
+
38
+ it "should inherit uploaders to subclasses" do
39
+ @subclass = Class.new(@class)
40
+ @subclass_instance = @subclass.new
41
+ @subclass_instance.image = stub_file('test.jpg')
42
+ @subclass_instance.image.should be_an_instance_of(@uploader)
43
+ end
44
+
45
+ describe '#image' do
46
+
47
+ it "should return a blank uploader when nothing has been assigned" do
48
+ @instance.should_receive(:read_uploader).with(:image).twice.and_return(nil)
49
+ @instance.image.should be_an_instance_of(@uploader)
50
+ @instance.image.should be_blank
51
+ end
52
+
53
+ it "should return a blank uploader when an empty string has been assigned" do
54
+ @instance.should_receive(:read_uploader).with(:image).twice.and_return('')
55
+ @instance.image.should be_an_instance_of(@uploader)
56
+ @instance.image.should be_blank
57
+ end
58
+
59
+ it "should retrieve a file from the storage if a value is stored in the database" do
60
+ @instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
61
+ @instance.image.should be_an_instance_of(@uploader)
62
+ end
63
+
64
+ it "should set the path to the store dir" do
65
+ @instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
66
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
67
+ end
68
+
69
+ end
70
+
71
+ describe '#image=' do
72
+
73
+ it "should cache a file" do
74
+ @instance.image = stub_file('test.jpg')
75
+ @instance.image.should be_an_instance_of(@uploader)
76
+ end
77
+
78
+ it "should copy a file into into the cache directory" do
79
+ @instance.image = stub_file('test.jpg')
80
+ @instance.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
81
+ end
82
+
83
+ it "should do nothing when nil is assigned" do
84
+ @instance.should_not_receive(:write_uploader)
85
+ @instance.image = nil
86
+ end
87
+
88
+ it "should do nothing when an empty string is assigned" do
89
+ @instance.should_not_receive(:write_uploader)
90
+ @instance.image = stub_file('test.jpg')
91
+ end
92
+
93
+ it "should fail silently if the image fails an integrity check" do
94
+ @uploader.class_eval do
95
+ def extension_white_list
96
+ %(txt)
97
+ end
98
+ end
99
+ @instance.image = stub_file('test.jpg')
100
+ @instance.image.should be_blank
101
+ end
102
+
103
+ it "should fail silently if the image fails to be processed" do
104
+ @uploader.class_eval do
105
+ process :monkey
106
+ def monkey
107
+ raise CarrierWave::ProcessingError, "Ohh noez!"
108
+ end
109
+ end
110
+ @instance.image = stub_file('test.jpg')
111
+ end
112
+
113
+ end
114
+
115
+ describe '#image?' do
116
+
117
+ it "should be false when nothing has been assigned" do
118
+ @instance.should_receive(:read_uploader).with(:image).and_return(nil)
119
+ @instance.image?.should be_false
120
+ end
121
+
122
+ it "should be false when an empty string has been assigned" do
123
+ @instance.should_receive(:read_uploader).with(:image).and_return('')
124
+ @instance.image?.should be_false
125
+ end
126
+
127
+ it "should be true when a file has been cached" do
128
+ @instance.image = stub_file('test.jpg')
129
+ @instance.image?.should be_true
130
+ end
131
+
132
+ end
133
+
134
+ describe '#image_url' do
135
+
136
+ it "should return nil when nothing has been assigned" do
137
+ @instance.should_receive(:read_uploader).with(:image).and_return(nil)
138
+ @instance.image_url.should be_nil
139
+ end
140
+
141
+ it "should return nil when an empty string has been assigned" do
142
+ @instance.should_receive(:read_uploader).with(:image).and_return('')
143
+ @instance.image_url.should be_nil
144
+ end
145
+
146
+ it "should get the url from a retrieved file" do
147
+ @instance.should_receive(:read_uploader).at_least(:once).with(:image).and_return('test.jpg')
148
+ @instance.image_url.should == '/uploads/test.jpg'
149
+ end
150
+
151
+ it "should get the url from a cached file" do
152
+ @instance.image = stub_file('test.jpg')
153
+ @instance.image_url.should =~ %r{uploads/tmp/[\d\-]+/test.jpg}
154
+ end
155
+
156
+ it "should get the url from a cached file's version" do
157
+ @uploader.version(:thumb)
158
+ @instance.image = stub_file('test.jpg')
159
+ @instance.image_url(:thumb).should =~ %r{uploads/tmp/[\d\-]+/thumb_test.jpg}
160
+ end
161
+
162
+ end
163
+
164
+ describe '#image_cache' do
165
+
166
+ before do
167
+ @instance.stub!(:write_uploader)
168
+ @instance.stub!(:read_uploader).and_return(nil)
169
+ end
170
+
171
+ it "should return nil when nothing has been assigned" do
172
+ @instance.image_cache.should be_nil
173
+ end
174
+
175
+ it "should be nil when a file has been stored" do
176
+ @instance.image = stub_file('test.jpg')
177
+ @instance.image.store!
178
+ @instance.image_cache.should be_nil
179
+ end
180
+
181
+ it "should be the cache name when a file has been cached" do
182
+ @instance.image = stub_file('test.jpg')
183
+ @instance.image_cache.should =~ %r(^[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}/test\.jpg$)
184
+ end
185
+
186
+ end
187
+
188
+ describe '#image_cache=' do
189
+
190
+ before do
191
+ @instance.stub!(:write_uploader)
192
+ @instance.stub!(:read_uploader).and_return(nil)
193
+ CarrierWave::SanitizedFile.new(file_path('test.jpg')).copy_to(public_path('uploads/tmp/19990512-1202-123-1234/test.jpg'))
194
+ end
195
+
196
+ it "should do nothing when nil is assigned" do
197
+ @instance.image_cache = nil
198
+ @instance.image.should be_blank
199
+ end
200
+
201
+ it "should do nothing when an empty string is assigned" do
202
+ @instance.image_cache = ''
203
+ @instance.image.should be_blank
204
+ end
205
+
206
+ it "retrieve from cache when a cache name is assigned" do
207
+ @instance.image_cache = '19990512-1202-123-1234/test.jpg'
208
+ @instance.image.current_path.should == public_path('uploads/tmp/19990512-1202-123-1234/test.jpg')
209
+ end
210
+
211
+ it "should not write over a previously assigned file" do
212
+ @instance.image = stub_file('test.jpg')
213
+ @instance.image_cache = '19990512-1202-123-1234/monkey.jpg'
214
+ @instance.image.current_path.should =~ /test.jpg$/
215
+ end
216
+ end
217
+
218
+ describe '#remote_image_url' do
219
+ before do
220
+ response = mock('HTTP Response')
221
+ response.stub!(:body).and_return('Response Body')
222
+ Net::HTTP.stub!(:get_response).and_return(response)
223
+ end
224
+
225
+ it "should return nil" do
226
+ @instance.remote_image_url.should be_nil
227
+ end
228
+
229
+ it "should return previously cached URL" do
230
+ @instance.remote_image_url = 'http://www.example.com/funky/monkey.png'
231
+ @instance.remote_image_url.should == 'http://www.example.com/funky/monkey.png'
232
+ end
233
+ end
234
+
235
+ describe '#remote_image_url=' do
236
+ before do
237
+ response = mock('HTTP Response')
238
+ response.stub!(:body).and_return('Response Body')
239
+ Net::HTTP.stub!(:get_response).and_return(response)
240
+ end
241
+
242
+ it "should do nothing when nil is assigned" do
243
+ @instance.remote_image_url = nil
244
+ @instance.image.should be_blank
245
+ end
246
+
247
+ it "should do nothing when an empty string is assigned" do
248
+ @instance.remote_image_url = ''
249
+ @instance.image.should be_blank
250
+ end
251
+
252
+ it "retrieve from cache when a cache name is assigned" do
253
+ @instance.remote_image_url = 'http://www.example.com/funky/monkey.png'
254
+ @instance.image.current_path.should =~ /monkey.png$/
255
+ end
256
+
257
+ it "should not write over a previously assigned file" do
258
+ @instance.image = stub_file('test.jpg')
259
+ @instance.remote_image_url = '19990512-1202-123-1234/monkey.jpg'
260
+ @instance.image.current_path.should =~ /test.jpg$/
261
+ end
262
+ end
263
+
264
+ describe '#store_image!' do
265
+
266
+ before do
267
+ @instance.stub!(:write_uploader)
268
+ @instance.stub!(:read_uploader).and_return(nil)
269
+ end
270
+
271
+ it "should do nothing when no file has been uploaded" do
272
+ @instance.store_image!
273
+ @instance.image.should be_blank
274
+ end
275
+
276
+ it "store an assigned file" do
277
+ @instance.image = stub_file('test.jpg')
278
+ @instance.store_image!
279
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
280
+ end
281
+
282
+ it "should remove an uploaded file when remove_image? returns true" do
283
+ @instance.image = stub_file('test.jpg')
284
+ path = @instance.image.current_path
285
+ @instance.remove_image = true
286
+ @instance.store_image!
287
+ @instance.image.should be_blank
288
+ File.exist?(path).should be_false
289
+ end
290
+ end
291
+
292
+ describe '#remove_image!' do
293
+
294
+ before do
295
+ @instance.stub!(:write_uploader)
296
+ @instance.stub!(:read_uploader).and_return(nil)
297
+ end
298
+
299
+ it "should do nothing when no file has been uploaded" do
300
+ @instance.remove_image!
301
+ @instance.image.should be_blank
302
+ end
303
+
304
+ it "should remove an uploaded file" do
305
+ @instance.image = stub_file('test.jpg')
306
+ path = @instance.image.current_path
307
+ @instance.remove_image!
308
+ @instance.image.should be_blank
309
+ File.exist?(path).should be_false
310
+ end
311
+ end
312
+
313
+ describe '#remove_image' do
314
+
315
+ it "should store a value" do
316
+ @instance.remove_image = true
317
+ @instance.remove_image.should be_true
318
+ end
319
+
320
+ end
321
+
322
+ describe '#remove_image?' do
323
+
324
+ it "should be true when the value is truthy" do
325
+ @instance.remove_image = true
326
+ @instance.remove_image?.should be_true
327
+ end
328
+
329
+ it "should be false when the value is falsey" do
330
+ @instance.remove_image = false
331
+ @instance.remove_image?.should be_false
332
+ end
333
+
334
+ it "should be false when the value is ''" do
335
+ @instance.remove_image = ''
336
+ @instance.remove_image?.should be_false
337
+ end
338
+
339
+ it "should be false when the value is '0'" do
340
+ @instance.remove_image = '0'
341
+ @instance.remove_image?.should be_false
342
+ end
343
+
344
+ it "should be false when the value is 'false'" do
345
+ @instance.remove_image = 'false'
346
+ @instance.remove_image?.should be_false
347
+ end
348
+
349
+ end
350
+
351
+ describe '#image_integrity_error' do
352
+
353
+ it "should be nil by default" do
354
+ @instance.image_integrity_error.should be_nil
355
+ end
356
+
357
+ it "should be nil after a file is cached" do
358
+ @instance.image = stub_file('test.jpg')
359
+ @instance.image_integrity_error.should be_nil
360
+ end
361
+
362
+ it "should be an error instance after an integrity check has failed" do
363
+ @uploader.class_eval do
364
+ def extension_white_list
365
+ %(txt)
366
+ end
367
+ end
368
+ @instance.image = stub_file('test.jpg')
369
+ @instance.image_integrity_error.should be_an_instance_of(CarrierWave::IntegrityError)
370
+ end
371
+ end
372
+
373
+ describe '#image_processing_error' do
374
+
375
+ it "should be nil by default" do
376
+ @instance.image_processing_error.should be_nil
377
+ end
378
+
379
+ it "should be nil after a file is cached" do
380
+ @instance.image = stub_file('test.jpg')
381
+ @instance.image_processing_error.should be_nil
382
+ end
383
+
384
+ it "should be an error instance after an integrity check has failed" do
385
+ @uploader.class_eval do
386
+ process :monkey
387
+ def monkey
388
+ raise CarrierWave::ProcessingError, "Ohh noez!"
389
+ end
390
+ end
391
+ @instance.image = stub_file('test.jpg')
392
+ @instance.image_processing_error.should be_an_instance_of(CarrierWave::ProcessingError)
393
+ end
394
+ end
395
+
396
+ describe '#write_image_identifier' do
397
+ it "should write to the column" do
398
+ @instance.should_receive(:write_uploader).with(:image, "test.jpg")
399
+ @instance.image = stub_file('test.jpg')
400
+ @instance.write_image_identifier
401
+ end
402
+
403
+ it "should remove from the column when remove_image is true" do
404
+ @instance.image = stub_file('test.jpg')
405
+ @instance.store_image!
406
+ @instance.remove_image = true
407
+ @instance.should_receive(:write_uploader).with(:image, "")
408
+ @instance.write_image_identifier
409
+ end
410
+ end
411
+
412
+ end
413
+
414
+ describe '#mount_uploader with a block' do
415
+
416
+ before do
417
+ @class = Class.new
418
+ @class.send(:extend, CarrierWave::Mount)
419
+ @class.mount_uploader(:image) do
420
+ def monkey
421
+ 'blah'
422
+ end
423
+ end
424
+ @instance = @class.new
425
+ end
426
+
427
+ describe '#image' do
428
+
429
+ before do
430
+ @instance.stub!(:read_uploader).and_return('test.jpg')
431
+ end
432
+
433
+ it "should return an instance of a subclass of CarrierWave::Uploader::Base" do
434
+ @instance.image.should be_a(CarrierWave::Uploader::Base)
435
+ end
436
+
437
+ it "should set the path to the store dir" do
438
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
439
+ end
440
+
441
+ it "should apply any custom modifications" do
442
+ @instance.image.monkey.should == "blah"
443
+ end
444
+
445
+ end
446
+
447
+ end
448
+
449
+ describe '#mount_uploader with :ignore_integrity_errors => false' do
450
+
451
+ before do
452
+ @class = Class.new
453
+ @class.send(:extend, CarrierWave::Mount)
454
+
455
+ @uploader = Class.new(CarrierWave::Uploader::Base)
456
+
457
+ @class.mount_uploader(:image, @uploader, :ignore_integrity_errors => false)
458
+ @instance = @class.new
459
+ end
460
+
461
+ it "should raise an error if the image fails an integrity check" do
462
+ @uploader.class_eval do
463
+ def extension_white_list
464
+ %(txt)
465
+ end
466
+ end
467
+ running {
468
+ @instance.image = stub_file('test.jpg')
469
+ }.should raise_error(CarrierWave::IntegrityError)
470
+ end
471
+
472
+ end
473
+
474
+ describe '#mount_uploader with :ignore_processing_errors => false' do
475
+
476
+ before do
477
+ @class = Class.new
478
+ @class.send(:extend, CarrierWave::Mount)
479
+
480
+ @uploader = Class.new(CarrierWave::Uploader::Base)
481
+
482
+ @class.mount_uploader(:image, @uploader, :ignore_processing_errors => false)
483
+ @instance = @class.new
484
+ end
485
+
486
+ it "should raise an error if the image fails to be processed" do
487
+ @uploader.class_eval do
488
+ process :monkey
489
+ def monkey
490
+ raise CarrierWave::ProcessingError, "Ohh noez!"
491
+ end
492
+ end
493
+ running {
494
+ @instance.image = stub_file('test.jpg')
495
+ }.should raise_error(CarrierWave::ProcessingError)
496
+ end
497
+
498
+ end
499
+
500
+ describe '#mount_uploader with :mount_on => :monkey' do
501
+
502
+
503
+ before do
504
+ @class = Class.new
505
+ @class.send(:extend, CarrierWave::Mount)
506
+
507
+ @uploader = Class.new(CarrierWave::Uploader::Base)
508
+
509
+ @class.mount_uploader(:image, @uploader, :mount_on => :monkey)
510
+ @instance = @class.new
511
+ end
512
+
513
+ describe '#image' do
514
+ it "should retrieve a file from the storage if a value is stored in the database" do
515
+ @instance.should_receive(:read_uploader).at_least(:once).with(:monkey).twice.and_return('test.jpg')
516
+ @instance.image.should be_an_instance_of(@uploader)
517
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
518
+ end
519
+ end
520
+
521
+ describe '#write_image_identifier' do
522
+ it "should write to the given column" do
523
+ @instance.should_receive(:write_uploader).with(:monkey, "test.jpg")
524
+ @instance.image = stub_file('test.jpg')
525
+ @instance.write_image_identifier
526
+ end
527
+
528
+ it "should remove from the given column when remove_image is true" do
529
+ @instance.image = stub_file('test.jpg')
530
+ @instance.store_image!
531
+ @instance.remove_image = true
532
+ @instance.should_receive(:write_uploader).with(:monkey, "")
533
+ @instance.write_image_identifier
534
+ end
535
+ end
536
+ end
537
+
538
+ end