carrierwave 0.2.0 → 0.2.1

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

Potentially problematic release.


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

data/README.rdoc CHANGED
@@ -172,6 +172,8 @@ in the case of images, a small thumbnail would be a good indicator:
172
172
  </p>
173
173
  <% end %>
174
174
 
175
+ NOTE: this feature currently requires write access to your filesystem. If write access is unavailable (e.g. Heroku) you will not be able to upload files. You can prevent CarrierWave from writing to the file system by setting `CarrierWave.config[:cache_to_cache_dir] = false`. This will however break redisplays of forms.
176
+
175
177
  == Using Amazon S3
176
178
 
177
179
  You'll need to configure a bucket, access id and secret key like this:
data/Rakefile CHANGED
@@ -3,12 +3,13 @@ require 'rake/gempackagetask'
3
3
  require 'rake/rdoctask'
4
4
  gem 'rdoc', '>=2.4.0'
5
5
  require 'rdoc'
6
+ require 'sdoc'
6
7
 
7
8
  require 'spec/rake/spectask'
8
9
  require 'cucumber/rake/task'
9
10
 
10
11
  NAME = "carrierwave"
11
- GEM_VERSION = "0.2.0"
12
+ GEM_VERSION = "0.2.1"
12
13
  AUTHOR = "Jonas Nicklas"
13
14
  EMAIL = "jonas.nicklas@gmail.com"
14
15
  HOMEPAGE = "http://www.example.com"
@@ -43,6 +44,7 @@ end
43
44
  Rake::RDocTask.new do |rd|
44
45
  rd.main = "README.rdoc"
45
46
  rd.title = "CarrierWave"
47
+ rd.template = 'direct'
46
48
  rd.options << "--diagram" if ENV["DIAGRAM"]
47
49
  rd.rdoc_dir = File.join(File.dirname(__FILE__), 'doc')
48
50
  rd.rdoc_files.include("README.rdoc", "LICENSE", "TODO", 'lib/carrierwave/**/*.rb')
data/lib/carrierwave.rb CHANGED
@@ -48,6 +48,7 @@ CarrierWave.config = {
48
48
  },
49
49
  :store_dir => 'uploads',
50
50
  :cache_dir => 'uploads/tmp',
51
+ :cache_to_cache_dir => true,
51
52
  :mount => {
52
53
  :ignore_integrity_errors => true,
53
54
  :ignore_processing_errors => true,
@@ -57,6 +58,7 @@ CarrierWave.config = {
57
58
  }
58
59
 
59
60
  if defined?(Merb)
61
+
60
62
  CarrierWave.config[:root] = Merb.root
61
63
  CarrierWave.config[:public] = Merb.dir_for(:public)
62
64
 
@@ -66,18 +68,19 @@ if defined?(Merb)
66
68
  Merb.push_path(:uploader, Merb.root / "app" / "uploaders")
67
69
 
68
70
  Merb.add_generators File.dirname(__FILE__) / 'generators' / 'uploader_generator'
69
- end
70
71
 
71
- if defined?(Rails)
72
+ elsif defined?(Rails)
73
+
72
74
  CarrierWave.config[:root] = Rails.root
73
75
  CarrierWave.config[:public] = File.join(Rails.root, 'public')
74
76
 
75
77
  require File.join(File.dirname(__FILE__), "carrierwave", "orm", 'activerecord')
76
78
 
77
79
  ActiveSupport::Dependencies.load_paths << File.join(Rails.root, "app", "uploaders")
78
- end
79
80
 
80
- if defined?(Sinatra)
81
+ elsif defined?(Sinatra)
82
+
81
83
  CarrierWave.config[:root] = Sinatra::Application.root
82
84
  CarrierWave.config[:public] = Sinatra::Application.public
85
+
83
86
  end
@@ -56,23 +56,36 @@ module CarrierWave
56
56
  #
57
57
  # [image] Returns an instance of the uploader only if anything has been uploaded
58
58
  # [image=] Caches the given file
59
+ #
60
+ # [image_url] Returns the url to the uploaded file
61
+ #
59
62
  # [image_cache] Returns a string that identifies the cache location of the file
60
63
  # [image_cache=] Retrieves the file from the cache based on the given cache name
64
+ #
61
65
  # [image_uploader] Returns an instance of the uploader
62
66
  # [image_uploader=] Sets the uploader (be careful!)
67
+ #
68
+ # [remove_image] An attribute reader that can be used with a checkbox to mark a file for removal
69
+ # [remove_image=] An attribute writer that can be used with a checkbox to mark a file for removal
70
+ # [remove_image?] Whether the file should be removed when store_image! is called.
71
+ #
63
72
  # [store_image!] Stores a file that has been assigned with +image=+
73
+ #
64
74
  # [image_integrity_error] Returns an error object if the last file to be assigned caused an integrty error
75
+ # [image_processing_error] Returns an error object if the last file to be assigned caused a processing error
65
76
  #
66
77
  # === Parameters
67
78
  #
68
- # [column (Symbol)] the attribute to mount this uploader on
69
- # [uploader (CarrierWave::Uploader)] the uploader class to mount
70
- # [options (Hash{Symbol => Object})] a set of options
71
- # [&block (Proc)] customize anonymous uploaders
79
+ # [column (Symbol)] the attribute to mount this uploader on
80
+ # [uploader (CarrierWave::Uploader)] the uploader class to mount
81
+ # [options (Hash{Symbol => Object})] a set of options
82
+ # [&block (Proc)] customize anonymous uploaders
72
83
  #
73
84
  # === Options
74
85
  #
75
- # [:ignore_integrity_errors (Boolean)] if set to true, integrity errors will result in caching failing silently
86
+ # [:mount_on => Symbol] if the name of the column to be serialized to differs you can override it using this option
87
+ # [:ignore_integrity_errors => Boolean] if set to true, integrity errors will result in caching failing silently
88
+ # [:ignore_processing_errors => Boolean] if set to true, processing errors will result in caching failing silently
76
89
  #
77
90
  # === Examples
78
91
  #
@@ -122,6 +135,10 @@ module CarrierWave
122
135
  _uploader_set(:#{column}, uploader) # _uploader_set(:image, uploader)
123
136
  end # end
124
137
  #
138
+ def #{column}_url(*args) # def image_url(*args)
139
+ _uploader_get_url(:#{column}, *args) # _uploader_get_url(:image, *args)
140
+ end # end
141
+ #
125
142
  def #{column} # def image
126
143
  _uploader_get_column(:#{column}) # _uploader_get_column(:image)
127
144
  end # end
@@ -138,6 +155,18 @@ module CarrierWave
138
155
  _uploader_set_cache(:#{column}, cache_name) # _uploader_set_cache(:image, cache_name)
139
156
  end # end
140
157
  #
158
+ def remove_#{column} # def remove_image
159
+ _uploader_remove[:#{column}] # _uploader_remove[:image]
160
+ end # end
161
+ #
162
+ def remove_#{column}=(value) # def remove_image=(value)
163
+ _uploader_remove[:#{column}] = value # _uploader_remove[:image] = value
164
+ end # end
165
+ #
166
+ def remove_#{column}? # def remove_image?
167
+ _uploader_remove?(:#{column}) # _uploader_remove?(:image)
168
+ end # end
169
+ #
141
170
  def store_#{column}! # def store_image!
142
171
  _uploader_store!(:#{column}) # _uploader_store!(:image)
143
172
  end # end
@@ -183,7 +212,7 @@ module CarrierWave
183
212
  def _uploader_get_column(column)
184
213
  return _uploader_get(column) unless _uploader_get(column).blank?
185
214
 
186
- identifier = read_uploader(column)
215
+ identifier = read_uploader(_uploader_options(column)[:mount_on] || column)
187
216
 
188
217
  unless identifier.blank?
189
218
  _uploader_get(column).retrieve_from_store!(identifier)
@@ -203,6 +232,11 @@ module CarrierWave
203
232
  raise e unless _uploader_options(column)[:ignore_processing_errors]
204
233
  end
205
234
 
235
+ def _uploader_get_url(column, *args)
236
+ _uploader_get_column(column)
237
+ _uploader_get(column).url(*args)
238
+ end
239
+
206
240
  def _uploader_get_cache(column)
207
241
  _uploader_get(column).cache_name
208
242
  end
@@ -213,11 +247,24 @@ module CarrierWave
213
247
 
214
248
  def _uploader_store!(column)
215
249
  unless _uploader_get(column).blank?
216
- _uploader_get(column).store!
217
- write_uploader(column, _uploader_get(column).identifier)
250
+ if _uploader_remove?(column)
251
+ _uploader_set(column, nil)
252
+ write_uploader(column, '')
253
+ else
254
+ _uploader_get(column).store!
255
+ write_uploader(_uploader_options(column)[:mount_on] || column, _uploader_get(column).identifier)
256
+ end
218
257
  end
219
258
  end
220
259
 
260
+ def _uploader_remove
261
+ @_uploader_remove ||= {}
262
+ end
263
+
264
+ def _uploader_remove?(column)
265
+ !_uploader_remove[column].blank? and _uploader_remove[column] !~ /\A0|false$\z/
266
+ end
267
+
221
268
  def _uploader_integrity_errors
222
269
  @_uploader_integrity_errors ||= {}
223
270
  end
@@ -7,7 +7,7 @@ module CarrierWave
7
7
  #
8
8
  module Matchers
9
9
 
10
- class BeIdenticalTo
10
+ class BeIdenticalTo # :nodoc:
11
11
  def initialize(expected)
12
12
  @expected = expected
13
13
  end
@@ -27,7 +27,7 @@ module CarrierWave
27
27
  BeIdenticalTo.new(expected)
28
28
  end
29
29
 
30
- class HavePermissions
30
+ class HavePermissions # :nodoc:
31
31
  def initialize(expected)
32
32
  @expected = expected
33
33
  end
@@ -51,7 +51,7 @@ module CarrierWave
51
51
  HavePermissions.new(expected)
52
52
  end
53
53
 
54
- class BeNoLargerThan
54
+ class BeNoLargerThan # :nodoc:
55
55
  def initialize(width, height)
56
56
  @width, @height = width, height
57
57
  end
@@ -79,7 +79,7 @@ module CarrierWave
79
79
  BeNoLargerThan.new(width, height)
80
80
  end
81
81
 
82
- class HaveDimensions
82
+ class HaveDimensions # :nodoc:
83
83
  def initialize(width, height)
84
84
  @width, @height = width, height
85
85
  end
@@ -259,11 +259,16 @@ module CarrierWave
259
259
  #
260
260
  # [String] the location where this file is accessible via a url
261
261
  #
262
- def url
263
- if file.respond_to?(:url) and not file.url.blank?
264
- file.url
265
- elsif current_path
266
- File.expand_path(current_path).gsub(File.expand_path(public), '')
262
+ def url(*args)
263
+ if(args.first)
264
+ # recursively proxy to version
265
+ versions[args.first.to_sym].url(*args[1..-1])
266
+ else
267
+ if file.respond_to?(:url) and not file.url.blank?
268
+ file.url
269
+ elsif current_path
270
+ File.expand_path(current_path).gsub(File.expand_path(public), '')
271
+ end
267
272
  end
268
273
  end
269
274
 
@@ -291,6 +296,17 @@ module CarrierWave
291
296
  file.read if file.respond_to?(:read)
292
297
  end
293
298
 
299
+ ##
300
+ # Fetches the size of the currently stored/cached file
301
+ #
302
+ # === Returns
303
+ #
304
+ # [Integer] size of the file
305
+ #
306
+ def size
307
+ file.respond_to?(:size) ? file.size : 0
308
+ end
309
+
294
310
  ##
295
311
  # Override this in your Uploader to change the filename.
296
312
  #
@@ -356,6 +372,12 @@ module CarrierWave
356
372
  ## Cache
357
373
  ####################
358
374
 
375
+ ##
376
+ #
377
+ def cached?
378
+ @cache_id
379
+ end
380
+
359
381
  ##
360
382
  # Override this in your Uploader to change the directory where files are cached.
361
383
  #
@@ -420,7 +442,10 @@ module CarrierWave
420
442
  @filename = new_file.filename
421
443
  self.original_filename = new_file.filename
422
444
 
423
- @file = @file.copy_to(cache_path, CarrierWave.config[:permissions])
445
+ if CarrierWave.config[:cache_to_cache_dir]
446
+ @file = @file.copy_to(cache_path, CarrierWave.config[:permissions])
447
+ end
448
+
424
449
  process!
425
450
 
426
451
  versions.each do |name, v|
@@ -520,7 +545,7 @@ module CarrierWave
520
545
  #
521
546
  def store!(new_file=nil)
522
547
  cache!(new_file) if new_file
523
- if @file
548
+ if @file and @cache_id
524
549
  @file = storage.store!(self, @file)
525
550
  @cache_id = nil
526
551
  versions.each { |name, v| v.store!(new_file) }
@@ -1,41 +1,41 @@
1
1
  class <%= class_name %>Uploader
2
2
 
3
3
  include CarrierWave::Uploader
4
-
4
+
5
5
  # Include RMagick or ImageScience support
6
6
  # include CarrierWave::RMagick
7
7
  # include CarrierWave::ImageScience
8
-
8
+
9
9
  # Choose what kind of storage to use for this uploader
10
10
  storage :file
11
11
  # storage :s3
12
-
12
+
13
13
  # Process files as they are uploaded.
14
14
  # process :scale => [200, 300]
15
- #
15
+ #
16
16
  # def scale(width, height)
17
17
  # # do something
18
- # end
19
-
18
+ # end
19
+
20
20
  # Create different versions of your uploaded files
21
21
  # version :thumb do
22
22
  # process :scale => [50, 50]
23
23
  # end
24
-
24
+
25
25
  # Add a white list of extensions which are allowed to be uploaded,
26
26
  # for images you might use something like this:
27
27
  # def extension_white_list
28
28
  # %w(jpg jpeg gif png)
29
29
  # end
30
-
30
+
31
31
  # Override the filename of the uploaded files
32
32
  # def filename
33
33
  # "something.jpg"
34
34
  # end
35
-
35
+
36
36
  # Override the directory where uploaded files will be stored
37
- # def store_dir
38
- # "something"
39
- # end
40
-
41
- end
37
+ # This is a sensible default for uploaders that are meant to be mounted:
38
+ def store_dir
39
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
40
+ end
41
+ end
@@ -1 +1 @@
1
- bork bork bork
1
+ bork bork bork Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
data/spec/mount_spec.rb CHANGED
@@ -86,9 +86,9 @@ describe CarrierWave::Mount do
86
86
 
87
87
  it "should do nothing when an empty string is assigned" do
88
88
  @instance.should_not_receive(:write_uploader)
89
- @instance.image = ''
89
+ @instance.image = stub_file('test.jpg')
90
90
  end
91
-
91
+
92
92
  it "should fail silently if the image fails an integrity check" do
93
93
  @uploader.class_eval do
94
94
  def extension_white_list
@@ -111,6 +111,36 @@ describe CarrierWave::Mount do
111
111
 
112
112
  end
113
113
 
114
+ describe '#image_url' do
115
+
116
+ it "should return nil when nothing has been assigned" do
117
+ @instance.should_receive(:read_uploader).with(:image).and_return(nil)
118
+ @instance.image_url.should be_nil
119
+ end
120
+
121
+ it "should return nil when an empty string has been assigned" do
122
+ @instance.should_receive(:read_uploader).with(:image).and_return('')
123
+ @instance.image_url.should be_nil
124
+ end
125
+
126
+ it "should get the url from a retrieved file" do
127
+ @instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
128
+ @instance.image_url.should == '/uploads/test.jpg'
129
+ end
130
+
131
+ it "should get the url from a cached file" do
132
+ @instance.image = stub_file('test.jpg')
133
+ @instance.image_url.should =~ %r{uploads/tmp/[\d\-]+/test.jpg}
134
+ end
135
+
136
+ it "should get the url from a cached file's version" do
137
+ @uploader.version(:thumb)
138
+ @instance.image = stub_file('test.jpg')
139
+ @instance.image_url(:thumb).should =~ %r{uploads/tmp/[\d\-]+/thumb_test.jpg}
140
+ end
141
+
142
+ end
143
+
114
144
  describe '#image_cache' do
115
145
 
116
146
  before do
@@ -182,8 +212,59 @@ describe CarrierWave::Mount do
182
212
  @instance.store_image!
183
213
  @instance.image.current_path.should == public_path('uploads/test.jpg')
184
214
  end
215
+
216
+ it "write to the column" do
217
+ @instance.should_receive(:write_uploader).with(:image, "test.jpg")
218
+ @instance.image = stub_file('test.jpg')
219
+ @instance.store_image!
220
+ end
221
+
222
+ it "should remove an uploaded file when remove_image? returns true" do
223
+ @instance.image = stub_file('test.jpg')
224
+ @instance.remove_image = true
225
+ @instance.store_image!
226
+ @instance.image.should be_nil
227
+ end
185
228
  end
186
-
229
+
230
+ describe '#remove_image' do
231
+
232
+ it "should store a value" do
233
+ @instance.remove_image = true
234
+ @instance.remove_image.should be_true
235
+ end
236
+
237
+ end
238
+
239
+ describe '#remove_image?' do
240
+
241
+ it "should be true when the value is truthy" do
242
+ @instance.remove_image = true
243
+ @instance.remove_image?.should be_true
244
+ end
245
+
246
+ it "should be false when the value is falsey" do
247
+ @instance.remove_image = false
248
+ @instance.remove_image?.should be_false
249
+ end
250
+
251
+ it "should be false when the value is ''" do
252
+ @instance.remove_image = ''
253
+ @instance.remove_image?.should be_false
254
+ end
255
+
256
+ it "should be false when the value is '0'" do
257
+ @instance.remove_image = '0'
258
+ @instance.remove_image?.should be_false
259
+ end
260
+
261
+ it "should be false when the value is 'false'" do
262
+ @instance.remove_image = 'false'
263
+ @instance.remove_image?.should be_false
264
+ end
265
+
266
+ end
267
+
187
268
  describe '#image_integrity_error' do
188
269
 
189
270
  it "should be nil by default" do
@@ -321,5 +402,38 @@ describe CarrierWave::Mount do
321
402
 
322
403
  end
323
404
 
405
+ describe '#mount_uploader with :mount_on => :monkey' do
406
+
407
+
408
+ before do
409
+ @class = Class.new
410
+ @class.send(:extend, CarrierWave::Mount)
411
+
412
+ @uploader = Class.new do
413
+ include CarrierWave::Uploader
414
+ end
415
+
416
+ @class.mount_uploader(:image, @uploader, :mount_on => :monkey)
417
+ @instance = @class.new
418
+ end
419
+
420
+ describe '#image' do
421
+
422
+ it "should retrieve a file from the storage if a value is stored in the database" do
423
+ @instance.should_receive(:read_uploader).with(:monkey).twice.and_return('test.jpg')
424
+ @instance.image.should be_an_instance_of(@uploader)
425
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
426
+ end
427
+
428
+ end
429
+
430
+ describe '#store_image!' do
431
+ it "should write to the given column" do
432
+ @instance.should_receive(:write_uploader).with(:monkey, "test.jpg")
433
+ @instance.image = stub_file('test.jpg')
434
+ @instance.store_image!
435
+ end
436
+ end
437
+ end
324
438
 
325
439
  end
@@ -25,6 +25,7 @@ class TestMigration < ActiveRecord::Migration
25
25
  end
26
26
 
27
27
  class Event < ActiveRecord::Base; end # setup a basic AR class for testing
28
+ $arclass = 0
28
29
 
29
30
  describe CarrierWave::ActiveRecord do
30
31
 
@@ -35,7 +36,13 @@ describe CarrierWave::ActiveRecord do
35
36
  after { Event.delete_all }
36
37
 
37
38
  before do
38
- @class = Class.new(ActiveRecord::Base)
39
+ # My god, what a horrible, horrible solution, but AR validations don't work
40
+ # unless the class has a name. This is the best I could come up with :S
41
+ $arclass += 1
42
+ eval <<-RUBY
43
+ class Event#{$arclass} < Event; end
44
+ @class = Event#{$arclass}
45
+ RUBY
39
46
  @class.table_name = "events"
40
47
  @uploader = Class.new do
41
48
  include CarrierWave::Uploader
@@ -151,6 +158,16 @@ describe CarrierWave::ActiveRecord do
151
158
  @event[:image].should == 'test.jpeg'
152
159
  end
153
160
 
161
+ it "should remove the image if remove_image? returns true" do
162
+ @event.image = stub_file('test.jpeg')
163
+ @event.save!
164
+ @event.remove_image = true
165
+ @event.save!
166
+ @event.reload
167
+ @event.image.should be_nil
168
+ @event[:image].should == ''
169
+ end
170
+
154
171
  end
155
172
 
156
173
  describe 'with overriddent filename' do
@@ -184,6 +201,43 @@ describe CarrierWave::ActiveRecord do
184
201
 
185
202
  end
186
203
 
204
+ describe 'with validates_presence_of' do
205
+
206
+ before do
207
+ @class.validates_presence_of :image
208
+ @event.stub!(:name).and_return('jonas')
209
+ end
210
+
211
+ it "should be valid if a file has been cached" do
212
+ @event.image = stub_file('test.jpeg')
213
+ @event.should be_valid
214
+ end
215
+
216
+ it "should not be valid if a file has not been cached" do
217
+ @event.should_not be_valid
218
+ end
219
+
220
+ end
221
+
222
+ describe 'with validates_size_of' do
223
+
224
+ before do
225
+ @class.validates_size_of :image, :maximum => 40
226
+ @event.stub!(:name).and_return('jonas')
227
+ end
228
+
229
+ it "should be valid if a file has been cached that matches the size criteria" do
230
+ @event.image = stub_file('test.jpeg')
231
+ @event.should be_valid
232
+ end
233
+
234
+ it "should not be valid if a file has been cached that does not match the size criteria" do
235
+ @event.image = stub_file('bork.txt')
236
+ @event.should_not be_valid
237
+ end
238
+
239
+ end
240
+
187
241
  end
188
242
 
189
243
  end
@@ -127,7 +127,17 @@ describe CarrierWave::DataMapper do
127
127
  @event.reload
128
128
  @event.attribute_get(:image).should == 'test.jpeg'
129
129
  end
130
-
130
+
131
+ it "should remove the image if remove_image? returns true" do
132
+ @event.image = stub_file('test.jpeg')
133
+ @event.save
134
+ @event.remove_image = true
135
+ @event.save
136
+ @event.reload
137
+ @event.image.should be_nil
138
+ @event.attribute_get(:image).should == ''
139
+ end
140
+
131
141
  end
132
142
 
133
143
  end
@@ -132,7 +132,16 @@ describe CarrierWave::Sequel do
132
132
  @event.reload
133
133
  @event[:image].should == 'test.jpeg'
134
134
  end
135
-
135
+
136
+ it "should remove the image if remove_image? returns true" do
137
+ @event.image = stub_file('test.jpeg')
138
+ @event.save
139
+ @event.remove_image = true
140
+ @event.save
141
+ @event.reload
142
+ @event.image.should be_nil
143
+ @event[:image].should == ''
144
+ end
136
145
  end
137
146
 
138
147
  describe 'with overriddent filename' do
@@ -200,6 +200,17 @@ describe CarrierWave::Uploader do
200
200
  @uploader.read.should == "this is stuff"
201
201
  end
202
202
  end
203
+
204
+ describe '#size' do
205
+ it "should be zero by default" do
206
+ @uploader.size.should == 0
207
+ end
208
+
209
+ it "should get the size of a cached file" do
210
+ @uploader.cache!(File.open(file_path('test.jpg')))
211
+ @uploader.size.should == 13
212
+ end
213
+ end
203
214
 
204
215
  describe '#store_dir' do
205
216
  it "should default to the config option" do
@@ -241,7 +252,7 @@ describe CarrierWave::Uploader do
241
252
  @uploader.mounted_as.should == :llama
242
253
  end
243
254
  end
244
-
255
+
245
256
  describe '#url' do
246
257
  before do
247
258
  CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
@@ -255,7 +266,21 @@ describe CarrierWave::Uploader do
255
266
  @uploader.cache!(File.open(file_path('test.jpg')))
256
267
  @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
257
268
  end
258
-
269
+
270
+ it "should get the directory relative to public for a specific version" do
271
+ @uploader_class.version(:thumb)
272
+ @uploader.cache!(File.open(file_path('test.jpg')))
273
+ @uploader.url(:thumb).should == '/uploads/tmp/20071201-1234-345-2255/thumb_test.jpg'
274
+ end
275
+
276
+ it "should get the directory relative to public for a nested version" do
277
+ @uploader_class.version(:thumb) do
278
+ version(:mini)
279
+ end
280
+ @uploader.cache!(File.open(file_path('test.jpg')))
281
+ @uploader.url(:thumb, :mini).should == '/uploads/tmp/20071201-1234-345-2255/thumb_mini_test.jpg'
282
+ end
283
+
259
284
  it "should return file#url if available" do
260
285
  @uploader.cache!(File.open(file_path('test.jpg')))
261
286
  @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
@@ -270,25 +295,25 @@ describe CarrierWave::Uploader do
270
295
  end
271
296
 
272
297
  describe '#to_s' do
273
- before do
274
- CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
275
- end
298
+ before do
299
+ CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
300
+ end
276
301
 
277
- it "should default to nil" do
278
- @uploader.to_s.should be_nil
279
- end
302
+ it "should default to nil" do
303
+ @uploader.to_s.should be_nil
304
+ end
280
305
 
281
- it "should get the directory relative to public, prepending a slash" do
282
- @uploader.cache!(File.open(file_path('test.jpg')))
283
- @uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
284
- end
306
+ it "should get the directory relative to public, prepending a slash" do
307
+ @uploader.cache!(File.open(file_path('test.jpg')))
308
+ @uploader.to_s.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
309
+ end
285
310
 
286
- it "should return file#url if available" do
287
- @uploader.cache!(File.open(file_path('test.jpg')))
288
- @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
289
- @uploader.to_s.should == 'http://www.example.com/someurl.jpg'
290
- end
311
+ it "should return file#url if available" do
312
+ @uploader.cache!(File.open(file_path('test.jpg')))
313
+ @uploader.file.stub!(:url).and_return('http://www.example.com/someurl.jpg')
314
+ @uploader.to_s.should == 'http://www.example.com/someurl.jpg'
291
315
  end
316
+ end
292
317
 
293
318
  describe '#cache!' do
294
319
 
@@ -300,6 +325,11 @@ describe CarrierWave::Uploader do
300
325
  @uploader.cache!(File.open(file_path('test.jpg')))
301
326
  @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
302
327
  end
328
+
329
+ it "should be cached" do
330
+ @uploader.cache!(File.open(file_path('test.jpg')))
331
+ @uploader.should be_cached
332
+ end
303
333
 
304
334
  it "should store the cache name" do
305
335
  @uploader.cache!(File.open(file_path('test.jpg')))
@@ -316,6 +346,15 @@ describe CarrierWave::Uploader do
316
346
  @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
317
347
  @uploader.file.exists?.should be_true
318
348
  end
349
+
350
+ it "should not move it if cache_to_cache_dir is false" do
351
+ CarrierWave.config[:cache_to_cache_dir] = false
352
+ path = file_path('test.jpg')
353
+ @uploader.cache!(File.open(path))
354
+ @uploader.current_path.should == path
355
+ @uploader.file.exists?.should be_true
356
+ CarrierWave.config[:cache_to_cache_dir] = true
357
+ end
319
358
 
320
359
  it "should set the url" do
321
360
  @uploader.cache!(File.open(file_path('test.jpg')))
@@ -380,6 +419,11 @@ describe CarrierWave::Uploader do
380
419
  @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
381
420
  @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
382
421
  end
422
+
423
+ it "should be cached" do
424
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
425
+ @uploader.should be_cached
426
+ end
383
427
 
384
428
  it "should set the path to the tmp dir" do
385
429
  @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
@@ -474,7 +518,12 @@ describe CarrierWave::Uploader do
474
518
  @uploader.store!(@file)
475
519
  @uploader.current_path.should == '/path/to/somewhere'
476
520
  end
477
-
521
+
522
+ it "should not be cached" do
523
+ @uploader.store!(@file)
524
+ @uploader.should_not be_cached
525
+ end
526
+
478
527
  it "should set the url" do
479
528
  @uploader.store!(@file)
480
529
  @uploader.url.should == 'http://www.example.com'
@@ -516,6 +565,15 @@ describe CarrierWave::Uploader do
516
565
  it "should do nothing when trying to store an empty file" do
517
566
  @uploader.store!(nil)
518
567
  end
568
+
569
+ it "should not re-store a retrieved file" do
570
+ @stored_file = mock('a stored file')
571
+ @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
572
+
573
+ @uploader_class.storage.should_not_receive(:store!)
574
+ @uploader.retrieve_from_store!('monkey.txt')
575
+ @uploader.store!
576
+ end
519
577
  end
520
578
 
521
579
  describe '#retrieve_from_store!' do
@@ -533,6 +591,11 @@ describe CarrierWave::Uploader do
533
591
  @uploader.current_path.should == '/path/to/somewhere'
534
592
  end
535
593
 
594
+ it "should not be cached" do
595
+ @uploader.retrieve_from_store!('monkey.txt')
596
+ @uploader.should_not be_cached
597
+ end
598
+
536
599
  it "should set the url" do
537
600
  @uploader.retrieve_from_store!('monkey.txt')
538
601
  @uploader.url.should == 'http://www.example.com'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-15 00:00:00 +02:00
12
+ date: 2009-05-01 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -29,45 +29,37 @@ files:
29
29
  - README.rdoc
30
30
  - Rakefile
31
31
  - TODO
32
- - lib/carrierwave
33
32
  - lib/carrierwave/mount.rb
34
- - lib/carrierwave/orm
35
33
  - lib/carrierwave/orm/activerecord.rb
36
34
  - lib/carrierwave/orm/datamapper.rb
37
35
  - lib/carrierwave/orm/sequel.rb
38
- - lib/carrierwave/processing
39
36
  - lib/carrierwave/processing/image_science.rb
40
37
  - lib/carrierwave/processing/rmagick.rb
41
38
  - lib/carrierwave/sanitized_file.rb
42
- - lib/carrierwave/storage
43
39
  - lib/carrierwave/storage/abstract.rb
44
40
  - lib/carrierwave/storage/file.rb
45
41
  - lib/carrierwave/storage/s3.rb
46
- - lib/carrierwave/test
47
42
  - lib/carrierwave/test/matchers.rb
48
43
  - lib/carrierwave/uploader.rb
49
44
  - lib/carrierwave.rb
50
- - lib/generators
51
45
  - lib/generators/uploader_generator.rb
52
- - spec/fixtures
53
46
  - spec/fixtures/bork.txt
54
47
  - spec/fixtures/test.jpeg
55
48
  - spec/fixtures/test.jpg
56
49
  - spec/mount_spec.rb
57
- - spec/orm
58
50
  - spec/orm/activerecord_spec.rb
59
51
  - spec/orm/datamapper_spec.rb
60
52
  - spec/orm/sequel_spec.rb
61
53
  - spec/sanitized_file_spec.rb
62
54
  - spec/spec_helper.rb
63
55
  - spec/uploader_spec.rb
64
- - rails_generators/uploader
65
- - rails_generators/uploader/templates
66
56
  - rails_generators/uploader/templates/uploader.rb
67
57
  - rails_generators/uploader/uploader_generator.rb
68
58
  - rails_generators/uploader/USAGE
69
59
  has_rdoc: true
70
60
  homepage: http://www.example.com
61
+ licenses: []
62
+
71
63
  post_install_message:
72
64
  rdoc_options: []
73
65
 
@@ -88,9 +80,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
80
  requirements: []
89
81
 
90
82
  rubyforge_project: carrierwave
91
- rubygems_version: 1.3.1
83
+ rubygems_version: 1.3.2
92
84
  signing_key:
93
- specification_version: 2
85
+ specification_version: 3
94
86
  summary: Simple and powerful uploads for Merb and Rails
95
87
  test_files: []
96
88