carrierwave-mongoid 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,4 +3,5 @@ pkg/*
3
3
  .bundle
4
4
  spec/public
5
5
  .rvmrc
6
- Gemfile.lock
6
+ Gemfile.lock
7
+ log/*
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # CarrierWave for Mongoid
2
+
3
+ This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the
4
+ CarrierWave documentation for more detailed usage instructions.
5
+
6
+ ### This version supports ONLY version of mongoid ~> 2.1
7
+ Keep in mind that if you came up from previous versions you should make a few steps to go with it:
8
+
9
+ * change(rename in db) field name from `avatar_name` to `avatar`(appropriate to your uploader name)
10
+ * fix you code where you need to operate with uploaded file's filename from `avatar` to `avatar_identifier`
11
+
12
+ Install it like this:
13
+
14
+ gem install carrierwave-mongoid
15
+
16
+ Use it like this:
17
+
18
+ ```ruby
19
+ require 'carrierwave/mongoid'
20
+ ```
21
+
22
+ Make sure to disable auto_validation on the mounted column.
23
+
24
+ Using bundler:
25
+
26
+ ```ruby
27
+ gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
28
+ ```
29
+
30
+ This used to be part of CarrierWave but has been extracted.
31
+
32
+ ## Using MongoDB's GridFS store
33
+
34
+ You'll need to configure the database and host to use:
35
+
36
+ ```ruby
37
+ CarrierWave.configure do |config|
38
+ config.grid_fs_database = 'my_mongo_database'
39
+ config.grid_fs_host = 'mongo.example.com'
40
+ end
41
+ ```
42
+
43
+ The defaults are `carrierwave` and `localhost`.
44
+
45
+ And then in your uploader, set the storage to `:grid_fs`:
46
+
47
+ ```ruby
48
+ class AvatarUploader < CarrierWave::Uploader::Base
49
+ storage :grid_fs
50
+ end
51
+ ```
52
+
53
+ Since GridFS doesn't make the files available via HTTP, you'll need to stream
54
+ them yourself. In Rails for example, you could use the `send_data` method. You
55
+ can tell CarrierWave the URL you will serve your images from, allowing it to
56
+ generate the correct URL, by setting eg:
57
+
58
+ ```ruby
59
+ CarrierWave.configure do |config|
60
+ config.grid_fs_access_url = "/image/show"
61
+ end
62
+ ```
63
+
64
+ ## Known issues/ limitations
65
+
66
+ If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
67
+ You must explicitly call save on embedded documents in order to save their attached files.
68
+ You can read more about this [here](https://github.com/jnicklas/carrierwave/issues#issue/81)
@@ -19,9 +19,10 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency "carrierwave"
23
- s.add_dependency "mongoid"
24
- s.add_development_dependency "rspec", ["~> 2.0"]
25
- s.add_development_dependency "bson_ext", ["> 1.3.0"]
26
- s.add_development_dependency "sqlite3"
22
+ s.add_dependency "carrierwave", [">= 0.5.6"]
23
+ s.add_dependency "mongoid", ["~> 2.1"]
24
+ s.add_development_dependency "rspec", ["~> 2.6"]
25
+ s.add_development_dependency "bson_ext", ["~> 1.3"]
26
+ s.add_development_dependency "rake", ["~> 0.9"]
27
+ s.add_development_dependency "mini_magick"
27
28
  end
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'mongoid'
4
+ require 'carrierwave'
4
5
  require 'carrierwave/validations/active_model'
5
6
 
6
7
  module CarrierWave
@@ -10,22 +11,43 @@ module CarrierWave
10
11
  # See +CarrierWave::Mount#mount_uploader+ for documentation
11
12
  #
12
13
  def mount_uploader(column, uploader=nil, options={}, &block)
13
- options[:mount_on] ||= "#{column}_filename"
14
- field options[:mount_on]
14
+ field options[:mount_on] || column
15
15
 
16
16
  super
17
17
 
18
18
  alias_method :read_uploader, :read_attribute
19
19
  alias_method :write_uploader, :write_attribute
20
+ public :read_uploader
21
+ public :write_uploader
20
22
 
21
23
  include CarrierWave::Validations::ActiveModel
22
24
 
23
25
  validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
24
26
  validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
25
27
 
26
- after_save "store_#{column}!".to_sym
27
- before_save "write_#{column}_identifier".to_sym
28
- after_destroy "remove_#{column}!".to_sym
28
+ after_save :"store_#{column}!"
29
+ before_save :"write_#{column}_identifier"
30
+ after_destroy :"remove_#{column}!"
31
+ before_update :"store_previous_model_for_#{column}"
32
+ after_save :"remove_previously_stored_#{column}"
33
+
34
+ class_eval <<-RUBY, __FILE__, __LINE__+1
35
+ def #{column}=(new_file)
36
+ column = _mounter(:#{column}).serialization_column
37
+ send(:"\#{column}_will_change!")
38
+ super
39
+ end
40
+
41
+ def find_previous_model_for_#{column}
42
+ if self.embedded?
43
+ ancestors = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
44
+ reloaded_parent = ancestors.first.last.reload
45
+ ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }.find(to_key.first)
46
+ else
47
+ self.class.find(to_key.first)
48
+ end
49
+ end
50
+ RUBY
29
51
  end
30
52
  end # Mongoid
31
53
  end # CarrierWave
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Mongoid
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ this is stuff
@@ -0,0 +1 @@
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.
@@ -0,0 +1 @@
1
+ this is stuff
@@ -0,0 +1 @@
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/mongoid_spec.rb CHANGED
@@ -1,19 +1,21 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- connection = Mongo::Connection.new
5
- Mongoid.database = connection.db("carrierwave_test")
6
-
4
+ require 'carrierwave/mongoid'
7
5
 
8
6
  def reset_mongo_class(uploader = MongoUploader)
9
- class_name = 'MongoUser'
10
- Object.send(:remove_const, class_name) rescue nil
11
- klass = Object.const_set(class_name, Class.new)
12
- klass.class_eval do
7
+ define_mongo_class('MongoUser') do
13
8
  include Mongoid::Document
14
9
  store_in :users
10
+ field :folder, :default => ''
15
11
  mount_uploader :image, uploader
16
12
  end
13
+ end
14
+
15
+ def define_mongo_class(class_name, &block)
16
+ Object.send(:remove_const, class_name) rescue nil
17
+ klass = Object.const_set(class_name, Class.new)
18
+ klass.class_eval(&block)
17
19
  klass
18
20
  end
19
21
 
@@ -61,7 +63,7 @@ describe CarrierWave::Mongoid do
61
63
 
62
64
  before do
63
65
  mongo_user_klass = reset_mongo_class
64
- @document = mongo_user_klass.new(:image_filename => "")
66
+ @document = mongo_user_klass.new(:image => "")
65
67
  @document.save
66
68
  end
67
69
 
@@ -76,7 +78,9 @@ describe CarrierWave::Mongoid do
76
78
 
77
79
  before do
78
80
  mongo_user_klass = reset_mongo_class
79
- @document = mongo_user_klass.new(:image_filename => "test.jpg")
81
+ @document = mongo_user_klass.new
82
+ # should retrieve a file from the storage if a value is stored in the database
83
+ @document[:image] = "test.jpg" # NOT @document.image = 'text.jpg'
80
84
  @document.save
81
85
  @doc = MongoUser.first
82
86
  end
@@ -126,7 +130,7 @@ describe CarrierWave::Mongoid do
126
130
  end
127
131
 
128
132
  it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
129
- @doc.image_filename.should be_nil
133
+ @doc[:image].should be_nil
130
134
  end
131
135
 
132
136
  it "should copy a file into into the cache directory" do
@@ -151,13 +155,13 @@ describe CarrierWave::Mongoid do
151
155
  @doc.valid?
152
156
  @doc.errors[:image].should == ['is not an allowed file type']
153
157
 
154
- change_locale_and_store_translations(:pt,
158
+ change_locale_and_store_translations(:pt, :mongoid => {
155
159
  :errors => {
156
- :messages => {
157
- :carrierwave_integrity_error => 'tipo de imagem não permitido.'
158
- }
159
- }
160
- ) do
160
+ :messages => {
161
+ :carrierwave_integrity_error => 'tipo de imagem não permitido.'
162
+ }
163
+ }
164
+ }) do
161
165
  @doc.should_not be_valid
162
166
  @doc.errors[:image].should == ['tipo de imagem não permitido.']
163
167
  end
@@ -179,13 +183,13 @@ describe CarrierWave::Mongoid do
179
183
  @doc.valid?
180
184
  @doc.errors[:image].should == ['failed to be processed']
181
185
 
182
- change_locale_and_store_translations(:pt,
186
+ change_locale_and_store_translations(:pt, :mongoid => {
183
187
  :errors => {
184
- :messages => {
185
- :carrierwave_processing_error => 'falha ao processar imagem.'
186
- }
187
- }
188
- ) do
188
+ :messages => {
189
+ :carrierwave_processing_error => 'falha ao processar imagem.'
190
+ }
191
+ }
192
+ }) do
189
193
  @doc.should_not be_valid
190
194
  @doc.errors[:image].should == ['falha ao processar imagem.']
191
195
  end
@@ -196,6 +200,13 @@ describe CarrierWave::Mongoid do
196
200
 
197
201
  describe "#save" do
198
202
 
203
+ it "after it was initialized with params" do
204
+ doc = reset_mongo_class.new(:image => stub_file('test.jpg'))
205
+ doc.save.should be_true
206
+ doc.image.should be_an_instance_of(MongoUploader)
207
+ doc.image.current_path.should == public_path('uploads/test.jpg')
208
+ end
209
+
199
210
  before do
200
211
  mongo_user_klass = reset_mongo_class
201
212
  @doc = mongo_user_klass.new
@@ -204,7 +215,7 @@ describe CarrierWave::Mongoid do
204
215
  context "when no file is assigned" do
205
216
 
206
217
  it "image is blank" do
207
- @doc.save
218
+ @doc.save.should be_true
208
219
  @doc.image.should be_blank
209
220
  end
210
221
 
@@ -214,15 +225,16 @@ describe CarrierWave::Mongoid do
214
225
 
215
226
  it "copies the file to the upload directory" do
216
227
  @doc.image = stub_file('test.jpg')
217
- @doc.save
228
+ @doc.save.should be_true
218
229
  @doc.image.should be_an_instance_of(MongoUploader)
219
230
  @doc.image.current_path.should == public_path('uploads/test.jpg')
220
231
  end
221
232
 
222
233
  it "saves the filename in the database" do
223
234
  @doc.image = stub_file('test.jpg')
224
- @doc.save
225
- @doc.image_filename.should == 'test.jpg'
235
+ @doc.save.should be_true
236
+ @doc[:image].should == 'test.jpg'
237
+ @doc.image_identifier.should == 'test.jpg'
226
238
  end
227
239
 
228
240
  context "when remove_image? is true" do
@@ -231,14 +243,45 @@ describe CarrierWave::Mongoid do
231
243
  @doc.image = stub_file('test.jpeg')
232
244
  @doc.save
233
245
  @doc.remove_image = true
234
- @doc.save
246
+ @doc.save.should be_true
235
247
  @doc.reload
236
248
  @doc.image.should be_blank
237
- @doc.image_filename.should == ''
249
+ @doc.image_identifier.should == ''
238
250
  end
239
251
 
240
252
  end
241
253
 
254
+ it "should mark image as changed when saving a new image" do
255
+ @doc.image_changed?.should be_false
256
+ @doc.image = stub_file("test.jpeg")
257
+ @doc.image_changed?.should be_true
258
+ @doc.save
259
+ @doc.reload
260
+ @doc.image_changed?.should be_false
261
+ @doc.image = stub_file("test.jpg")
262
+ @doc.image_changed?.should be_true
263
+ end
264
+
265
+ end
266
+
267
+ end
268
+
269
+ describe '#update' do
270
+
271
+ before do
272
+ mongo_user_klass = reset_mongo_class
273
+ @doc = mongo_user_klass.new
274
+ @doc.image = stub_file('test.jpeg')
275
+ @doc.save
276
+ @doc.reload
277
+ end
278
+
279
+ it "replaced it by a file with the same name" do
280
+ @doc.image = stub_file('test.jpeg')
281
+ @doc.save
282
+ @doc.reload
283
+ @doc[:image].should == 'test.jpeg'
284
+ @doc.image_identifier.should == 'test.jpeg'
242
285
  end
243
286
 
244
287
  end
@@ -283,5 +326,295 @@ describe CarrierWave::Mongoid do
283
326
 
284
327
  end
285
328
 
329
+ describe '#mount_uploader removing old files' do
330
+
331
+ before do
332
+ @uploader = Class.new(MongoUploader)
333
+ @class = reset_mongo_class(@uploader)
334
+ @class.field :foo
335
+ @doc = @class.new
336
+ @doc.image = stub_file('old.jpeg')
337
+ @doc.save.should be_true
338
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
339
+ end
340
+
341
+ after do
342
+ FileUtils.rm_rf(file_path("uploads"))
343
+ end
344
+
345
+ describe 'normally' do
346
+
347
+ it "should remove old file if old file had a different path" do
348
+ @doc.image = stub_file('new.jpeg')
349
+ @doc.save.should be_true
350
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
351
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
352
+ end
353
+
354
+ it "should not remove old file if old file had a different path but config is false" do
355
+ @uploader.stub!(:remove_previously_stored_files_after_update).and_return(false)
356
+ @doc.image = stub_file('new.jpeg')
357
+ @doc.save.should be_true
358
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
359
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
360
+ end
361
+
362
+ it "should not remove file if old file had the same path" do
363
+ @doc.image = stub_file('old.jpeg')
364
+ @doc.save.should be_true
365
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
366
+ end
367
+
368
+ it "should not remove file if validations fail on save" do
369
+ @class.validate { |r| r.errors.add :textfile, "FAIL!" }
370
+ @doc.image = stub_file('new.jpeg')
371
+ @doc.save.should be_false
372
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
373
+ end
374
+ end
375
+
376
+ describe 'with an overriden filename' do
377
+ before do
378
+ @uploader.class_eval do
379
+ def filename
380
+ model.foo + File.extname(super)
381
+ end
382
+ end
383
+
384
+ @doc.image = stub_file('old.jpeg')
385
+ @doc.foo = "test"
386
+ @doc.save.should be_true
387
+ File.exists?(public_path('uploads/test.jpeg')).should be_true
388
+ @doc.image.read.should == "this is stuff"
389
+ end
390
+
391
+ it "should not remove file if old file had the same dynamic path" do
392
+ @doc.image = stub_file('test.jpeg')
393
+ @doc.save.should be_true
394
+ File.exists?(public_path('uploads/test.jpeg')).should be_true
395
+ end
396
+
397
+ it "should remove old file if old file had a different dynamic path" do
398
+ @doc.foo = "new"
399
+ @doc.image = stub_file('test.jpeg')
400
+ @doc.save.should be_true
401
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
402
+ File.exists?(public_path('uploads/test.jpeg')).should be_false
403
+ end
404
+ end
405
+
406
+ describe 'with embedded documents' do
407
+
408
+ before do
409
+ @embedded_doc_class = define_mongo_class('MongoLocation') do
410
+ include Mongoid::Document
411
+ mount_uploader :image, @uploader
412
+ embedded_in :mongo_user
413
+ end
414
+
415
+ @class.class_eval do
416
+ embeds_many :mongo_locations
417
+ end
418
+
419
+ @doc = @class.new
420
+ @embedded_doc = @doc.mongo_locations.build
421
+ @embedded_doc.image = stub_file('old.jpeg')
422
+ @embedded_doc.save.should be_true
423
+ end
424
+
425
+ it "should remove old file if old file had a different path" do
426
+ @embedded_doc.image = stub_file('new.jpeg')
427
+ @embedded_doc.save.should be_true
428
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
429
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
430
+ end
431
+
432
+ it "should not remove old file if old file had a different path but config is false" do
433
+ @embedded_doc.image.stub!(:remove_previously_stored_files_after_update).and_return(false)
434
+ @embedded_doc.image = stub_file('new.jpeg')
435
+ @embedded_doc.save.should be_true
436
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
437
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
438
+ end
439
+
440
+ it "should not remove file if old file had the same path" do
441
+ @embedded_doc.image = stub_file('old.jpeg')
442
+ @embedded_doc.save.should be_true
443
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
444
+ end
445
+
446
+ it "should not remove file if validations fail on save" do
447
+ @embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
448
+ @embedded_doc.image = stub_file('new.jpeg')
449
+ @embedded_doc.save.should be_false
450
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
451
+ end
452
+
453
+ describe 'with double embedded documents' do
454
+
455
+ before do
456
+ @double_embedded_doc_class = define_mongo_class('MongoItem') do
457
+ include Mongoid::Document
458
+ mount_uploader :image, @uploader
459
+ embedded_in :mongo_location
460
+ end
461
+
462
+ @embedded_doc_class.class_eval do
463
+ embeds_many :mongo_items
464
+ end
465
+
466
+ @doc = @class.new
467
+ @embedded_doc = @doc.mongo_locations.build
468
+ @embedded_doc.image = stub_file('old.jpeg')
469
+ @embedded_doc.save.should be_true
470
+
471
+ @double_embedded_doc = @embedded_doc.mongo_items.build
472
+ @double_embedded_doc.image = stub_file('old.jpeg')
473
+ @double_embedded_doc.save.should be_true
474
+ end
475
+
476
+ it "should remove old file if old file had a different path" do
477
+ @double_embedded_doc.image = stub_file('new.jpeg')
478
+ @double_embedded_doc.save.should be_true
479
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
480
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
481
+ end
482
+
483
+ it "should not remove old file if old file had a different path but config is false" do
484
+ @double_embedded_doc.image.stub!(:remove_previously_stored_files_after_update).and_return(false)
485
+ @double_embedded_doc.image = stub_file('new.jpeg')
486
+ @double_embedded_doc.save.should be_true
487
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
488
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
489
+ end
490
+
491
+ it "should not remove file if old file had the same path" do
492
+ @double_embedded_doc.image = stub_file('old.jpeg')
493
+ @double_embedded_doc.save.should be_true
494
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
495
+ end
496
+
497
+ it "should not remove file if validations fail on save" do
498
+ @double_embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
499
+ @double_embedded_doc.image = stub_file('new.jpeg')
500
+ @double_embedded_doc.save.should be_false
501
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
502
+ end
503
+
504
+ end
505
+
506
+ end
507
+ end
508
+
509
+ describe '#mount_uploader removing old files with versions' do
510
+
511
+ before do
512
+ @uploader = Class.new(MongoUploader)
513
+ @uploader.version :thumb
514
+ @class = reset_mongo_class(@uploader)
515
+ @doc = @class.new
516
+ @doc.image = stub_file('old.jpeg')
517
+ @doc.save.should be_true
518
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
519
+ File.exists?(public_path('uploads/thumb_old.jpeg')).should be_true
520
+ end
521
+
522
+ after do
523
+ FileUtils.rm_rf(file_path("uploads"))
524
+ end
525
+
526
+ it "should remove old file if old file had a different path" do
527
+ @doc.image = stub_file('new.jpeg')
528
+ @doc.save.should be_true
529
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
530
+ File.exists?(public_path('uploads/thumb_new.jpeg')).should be_true
531
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
532
+ File.exists?(public_path('uploads/thumb_old.jpeg')).should be_false
533
+ end
534
+
535
+ it "should not remove file if old file had the same path" do
536
+ @doc.image = stub_file('old.jpeg')
537
+ @doc.save.should be_true
538
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
539
+ File.exists?(public_path('uploads/thumb_old.jpeg')).should be_true
540
+ end
541
+ end
542
+
543
+ describe '#mount_uploader removing old files with multiple uploaders' do
544
+
545
+ before do
546
+ @uploader = Class.new(MongoUploader)
547
+ @class = reset_mongo_class(@uploader)
548
+ @uploader1 = Class.new(CarrierWave::Uploader::Base)
549
+ @class.mount_uploader(:textfile, @uploader1)
550
+ @doc = @class.new
551
+ @doc.image = stub_file('old.jpeg')
552
+ @doc.textfile = stub_file('old.txt')
553
+ @doc.save.should be_true
554
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
555
+ File.exists?(public_path('uploads/old.txt')).should be_true
556
+ end
557
+
558
+ after do
559
+ FileUtils.rm_rf(file_path("uploads"))
560
+ end
561
+
562
+ it "should remove old file1 and file2 if old file1 and file2 had a different paths" do
563
+ @doc.image = stub_file('new.jpeg')
564
+ @doc.textfile = stub_file('new.txt')
565
+ @doc.save.should be_true
566
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
567
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
568
+ File.exists?(public_path('uploads/new.txt')).should be_true
569
+ File.exists?(public_path('uploads/old.txt')).should be_false
570
+ end
571
+
572
+ it "should remove old file1 but not file2 if old file1 had a different path but old file2 has the same path" do
573
+ @doc.image = stub_file('new.jpeg')
574
+ @doc.textfile = stub_file('old.txt')
575
+ @doc.save.should be_true
576
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
577
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
578
+ File.exists?(public_path('uploads/old.txt')).should be_true
579
+ end
580
+
581
+ it "should not remove file1 or file2 if file1 and file2 have the same paths" do
582
+ @doc.image = stub_file('old.jpeg')
583
+ @doc.textfile = stub_file('old.txt')
584
+ @doc.save.should be_true
585
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
586
+ File.exists?(public_path('uploads/old.txt')).should be_true
587
+ end
588
+ end
589
+
590
+ describe '#mount_uploader removing old files with with mount_on' do
591
+
592
+ before do
593
+ @class = reset_mongo_class
594
+ @uploader1 = Class.new(CarrierWave::Uploader::Base)
595
+ @class.mount_uploader(:avatar, @uploader1, :mount_on => :another_image)
596
+ @doc = @class.new
597
+ @doc.avatar = stub_file('old.jpeg')
598
+ @doc.save.should be_true
599
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
600
+ end
601
+
602
+ after do
603
+ FileUtils.rm_rf(file_path("uploads"))
604
+ end
605
+
606
+ it "should remove old file if old file had a different path" do
607
+ @doc.avatar = stub_file('new.jpeg')
608
+ @doc.save.should be_true
609
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
610
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
611
+ end
612
+
613
+ it "should not remove file if old file had the same path" do
614
+ @doc.avatar = stub_file('old.jpeg')
615
+ @doc.save.should be_true
616
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
617
+ end
618
+ end
286
619
 
287
620
  end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,13 @@ require 'tempfile'
6
6
  require 'carrierwave'
7
7
  require 'carrierwave/mongoid'
8
8
 
9
+ Mongoid.configure do |config|
10
+ logger = Logger.new('log/test.log')
11
+ config.logger = logger
12
+ config.master = Mongo::Connection.new('localhost', 27017,
13
+ :logger => logger).db('carrierwave_test')
14
+ end
15
+
9
16
  def file_path( *paths )
10
17
  File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
11
18
  end
@@ -30,12 +37,9 @@ module CarrierWave
30
37
  t = Tempfile.new(filename)
31
38
  FileUtils.copy_file(file_path(filename), t.path)
32
39
 
33
- # This is stupid, but for some reason rspec won't play nice...
34
- eval <<-EOF
35
- def t.original_filename; '#{fake_name || filename}'; end
36
- def t.content_type; '#{mime_type}'; end
37
- def t.local_path; path; end
38
- EOF
40
+ t.stub!(:local_path => "",
41
+ :original_filename => filename || fake_name,
42
+ :content_type => mime_type)
39
43
 
40
44
  return t
41
45
  end
@@ -100,9 +100,12 @@ describe CarrierWave::Storage::GridFS do
100
100
  describe "#recreate_versions!" do
101
101
  before do
102
102
  @uploader_class = Class.new(CarrierWave::Uploader::Base)
103
- @uploader_class.class_eval do
103
+ @uploader_class.class_eval{
104
+ include CarrierWave::MiniMagick
104
105
  storage :grid_fs
105
- end
106
+
107
+ process :resize_to_fit => [10, 10]
108
+ }
106
109
 
107
110
  @versioned = @uploader_class.new
108
111
  @versioned.stub!(:grid_fs_connection).and_return(@database)
@@ -122,6 +125,33 @@ describe CarrierWave::Storage::GridFS do
122
125
  @versioned.should be_present
123
126
  end
124
127
  end
128
+
129
+
130
+ describe "resize_to_fill" do
131
+ before do
132
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
133
+ @uploader_class.class_eval{
134
+ include CarrierWave::MiniMagick
135
+ storage :grid_fs
136
+ }
137
+
138
+ @versioned = @uploader_class.new
139
+ @versioned.stub!(:grid_fs_connection).and_return(@database)
140
+
141
+ @versioned.store! File.open(file_path('portrait.jpg'))
142
+ end
143
+
144
+ after do
145
+ FileUtils.rm_rf(public_path)
146
+ end
147
+
148
+ it "resizes the file with out error" do
149
+ lambda {
150
+ @versioned.resize_to_fill(200, 200)
151
+ }.should_not raise_error
152
+
153
+ end
154
+ end
125
155
  end
126
156
 
127
157
  context "when setting a connection manually" do
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-mongoid
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 25
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 0
9
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Jonas Nicklas
@@ -15,7 +16,7 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-08-12 00:00:00 +02:00
19
+ date: 2011-08-12 00:00:00 -05:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
@@ -26,9 +27,12 @@ dependencies:
26
27
  requirements:
27
28
  - - ">="
28
29
  - !ruby/object:Gem::Version
30
+ hash: 7
29
31
  segments:
30
32
  - 0
31
- version: "0"
33
+ - 5
34
+ - 6
35
+ version: 0.5.6
32
36
  type: :runtime
33
37
  version_requirements: *id001
34
38
  - !ruby/object:Gem::Dependency
@@ -37,11 +41,13 @@ dependencies:
37
41
  requirement: &id002 !ruby/object:Gem::Requirement
38
42
  none: false
39
43
  requirements:
40
- - - ">="
44
+ - - ~>
41
45
  - !ruby/object:Gem::Version
46
+ hash: 1
42
47
  segments:
43
- - 0
44
- version: "0"
48
+ - 2
49
+ - 1
50
+ version: "2.1"
45
51
  type: :runtime
46
52
  version_requirements: *id002
47
53
  - !ruby/object:Gem::Dependency
@@ -52,10 +58,11 @@ dependencies:
52
58
  requirements:
53
59
  - - ~>
54
60
  - !ruby/object:Gem::Version
61
+ hash: 15
55
62
  segments:
56
63
  - 2
57
- - 0
58
- version: "2.0"
64
+ - 6
65
+ version: "2.6"
59
66
  type: :development
60
67
  version_requirements: *id003
61
68
  - !ruby/object:Gem::Dependency
@@ -64,28 +71,44 @@ dependencies:
64
71
  requirement: &id004 !ruby/object:Gem::Requirement
65
72
  none: false
66
73
  requirements:
67
- - - ">"
74
+ - - ~>
68
75
  - !ruby/object:Gem::Version
76
+ hash: 9
69
77
  segments:
70
78
  - 1
71
79
  - 3
72
- - 0
73
- version: 1.3.0
80
+ version: "1.3"
74
81
  type: :development
75
82
  version_requirements: *id004
76
83
  - !ruby/object:Gem::Dependency
77
- name: sqlite3
84
+ name: rake
78
85
  prerelease: false
79
86
  requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ hash: 25
92
+ segments:
93
+ - 0
94
+ - 9
95
+ version: "0.9"
96
+ type: :development
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ name: mini_magick
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
80
102
  none: false
81
103
  requirements:
82
104
  - - ">="
83
105
  - !ruby/object:Gem::Version
106
+ hash: 3
84
107
  segments:
85
108
  - 0
86
109
  version: "0"
87
110
  type: :development
88
- version_requirements: *id005
111
+ version_requirements: *id006
89
112
  description: Mongoid support for CarrierWave
90
113
  email:
91
114
  - jonas.nicklas@gmail.com
@@ -98,12 +121,17 @@ extra_rdoc_files: []
98
121
  files:
99
122
  - .gitignore
100
123
  - Gemfile
101
- - README.rdoc
124
+ - README.md
102
125
  - Rakefile
103
126
  - carrierwave-mongoid.gemspec
104
127
  - lib/carrierwave/mongoid.rb
105
128
  - lib/carrierwave/mongoid/version.rb
106
129
  - lib/carrierwave/storage/grid_fs.rb
130
+ - log/.gitkeep
131
+ - spec/fixtures/new.jpeg
132
+ - spec/fixtures/new.txt
133
+ - spec/fixtures/old.jpeg
134
+ - spec/fixtures/old.txt
107
135
  - spec/fixtures/portrait.jpg
108
136
  - spec/fixtures/test.jpeg
109
137
  - spec/fixtures/test.jpg
@@ -124,6 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
152
  requirements:
125
153
  - - ">="
126
154
  - !ruby/object:Gem::Version
155
+ hash: 3
127
156
  segments:
128
157
  - 0
129
158
  version: "0"
@@ -132,17 +161,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
161
  requirements:
133
162
  - - ">="
134
163
  - !ruby/object:Gem::Version
164
+ hash: 3
135
165
  segments:
136
166
  - 0
137
167
  version: "0"
138
168
  requirements: []
139
169
 
140
170
  rubyforge_project: carrierwave-mongoid
141
- rubygems_version: 1.3.7
171
+ rubygems_version: 1.4.2
142
172
  signing_key:
143
173
  specification_version: 3
144
174
  summary: Mongoid support for CarrierWave
145
175
  test_files:
176
+ - spec/fixtures/new.jpeg
177
+ - spec/fixtures/new.txt
178
+ - spec/fixtures/old.jpeg
179
+ - spec/fixtures/old.txt
146
180
  - spec/fixtures/portrait.jpg
147
181
  - spec/fixtures/test.jpeg
148
182
  - spec/fixtures/test.jpg
data/README.rdoc DELETED
@@ -1,53 +0,0 @@
1
- = CarrierWave for Mongoid
2
-
3
- This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the
4
- CarrierWave documentation for more detailed usage instructions.
5
-
6
- Install it like this:
7
-
8
- gem install carrierwave-mongoid
9
-
10
- Use it like this:
11
-
12
- require 'carrierwave/mongoid'
13
-
14
- Make sure to disable auto_validation on the mounted column.
15
-
16
- Using bundler:
17
-
18
- gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
19
-
20
- This used to be part of CarrierWave but has been extracted.
21
-
22
- == Using MongoDB's GridFS store
23
-
24
- You'll need to configure the database and host to use:
25
-
26
- CarrierWave.configure do |config|
27
- config.grid_fs_database = 'my_mongo_database'
28
- config.grid_fs_host = 'mongo.example.com'
29
- end
30
-
31
- The defaults are 'carrierwave' and 'localhost'.
32
-
33
- And then in your uploader, set the storage to <code>:grid_fs</code>:
34
-
35
- class AvatarUploader < CarrierWave::Uploader::Base
36
- storage :grid_fs
37
- end
38
-
39
- Since GridFS doesn't make the files available via HTTP, you'll need to stream
40
- them yourself. In Rails for example, you could use the +send_data+ method. You
41
- can tell CarrierWave the URL you will serve your images from, allowing it to
42
- generate the correct URL, by setting eg:
43
-
44
- CarrierWave.configure do |config|
45
- config.grid_fs_access_url = "/image/show"
46
- end
47
-
48
- == Known issues/ limitations
49
-
50
- If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
51
- You must explicitly call save on embedded documents in order to save their attached files.
52
- You can read more about this {here}[https://github.com/jnicklas/carrierwave/issues#issue/81]
53
-