samlown-carrierwave 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/Generators +4 -0
  2. data/History.txt +125 -0
  3. data/Manifest.txt +110 -0
  4. data/README.rdoc +524 -0
  5. data/Rakefile +39 -0
  6. data/carrierwave.gemspec +85 -0
  7. data/cucumber.yml +2 -0
  8. data/features/caching.feature +28 -0
  9. data/features/download.feature +20 -0
  10. data/features/file_storage.feature +37 -0
  11. data/features/file_storage_overridden_filename.feature +38 -0
  12. data/features/file_storage_overridden_store_dir.feature +38 -0
  13. data/features/file_storage_reversing_processor.feature +43 -0
  14. data/features/fixtures/bork.txt +1 -0
  15. data/features/fixtures/monkey.txt +1 -0
  16. data/features/grid_fs_storage.feature +32 -0
  17. data/features/mount_activerecord.feature +46 -0
  18. data/features/mount_datamapper.feature +46 -0
  19. data/features/step_definitions/activerecord_steps.rb +22 -0
  20. data/features/step_definitions/caching_steps.rb +14 -0
  21. data/features/step_definitions/datamapper_steps.rb +29 -0
  22. data/features/step_definitions/download_steps.rb +4 -0
  23. data/features/step_definitions/file_steps.rb +53 -0
  24. data/features/step_definitions/general_steps.rb +85 -0
  25. data/features/step_definitions/mount_steps.rb +19 -0
  26. data/features/step_definitions/store_steps.rb +18 -0
  27. data/features/support/activerecord.rb +30 -0
  28. data/features/support/datamapper.rb +7 -0
  29. data/features/support/env.rb +22 -0
  30. data/features/versions_basics.feature +50 -0
  31. data/features/versions_nested_versions.feature +70 -0
  32. data/features/versions_overridden_filename.feature +51 -0
  33. data/features/versions_overriden_store_dir.feature +41 -0
  34. data/lib/carrierwave.rb +98 -0
  35. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  36. data/lib/carrierwave/core_ext/blank.rb +46 -0
  37. data/lib/carrierwave/core_ext/file.rb +11 -0
  38. data/lib/carrierwave/core_ext/inheritable_attributes.rb +108 -0
  39. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  40. data/lib/carrierwave/mount.rb +359 -0
  41. data/lib/carrierwave/orm/activerecord.rb +73 -0
  42. data/lib/carrierwave/orm/datamapper.rb +27 -0
  43. data/lib/carrierwave/orm/mongoid.rb +23 -0
  44. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  45. data/lib/carrierwave/orm/sequel.rb +45 -0
  46. data/lib/carrierwave/processing/image_science.rb +101 -0
  47. data/lib/carrierwave/processing/mini_magick.rb +265 -0
  48. data/lib/carrierwave/processing/rmagick.rb +282 -0
  49. data/lib/carrierwave/sanitized_file.rb +273 -0
  50. data/lib/carrierwave/storage/abstract.rb +30 -0
  51. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  52. data/lib/carrierwave/storage/file.rb +48 -0
  53. data/lib/carrierwave/storage/grid_fs.rb +97 -0
  54. data/lib/carrierwave/storage/right_s3.rb +3 -0
  55. data/lib/carrierwave/storage/s3.rb +206 -0
  56. data/lib/carrierwave/test/matchers.rb +128 -0
  57. data/lib/carrierwave/uploader.rb +44 -0
  58. data/lib/carrierwave/uploader/cache.rb +145 -0
  59. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  60. data/lib/carrierwave/uploader/configuration.rb +132 -0
  61. data/lib/carrierwave/uploader/default_url.rb +19 -0
  62. data/lib/carrierwave/uploader/download.rb +59 -0
  63. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  64. data/lib/carrierwave/uploader/mountable.rb +39 -0
  65. data/lib/carrierwave/uploader/processing.rb +83 -0
  66. data/lib/carrierwave/uploader/proxy.rb +62 -0
  67. data/lib/carrierwave/uploader/remove.rb +22 -0
  68. data/lib/carrierwave/uploader/store.rb +89 -0
  69. data/lib/carrierwave/uploader/url.rb +33 -0
  70. data/lib/carrierwave/uploader/versions.rb +146 -0
  71. data/merb_generators/uploader_generator.rb +22 -0
  72. data/rails_generators/uploader/USAGE +2 -0
  73. data/rails_generators/uploader/templates/uploader.rb +47 -0
  74. data/rails_generators/uploader/uploader_generator.rb +21 -0
  75. data/script/console +10 -0
  76. data/script/destroy +14 -0
  77. data/script/generate +14 -0
  78. data/spec/compatibility/paperclip_spec.rb +52 -0
  79. data/spec/fixtures/bork.txt +1 -0
  80. data/spec/fixtures/landscape.jpg +0 -0
  81. data/spec/fixtures/portrait.jpg +0 -0
  82. data/spec/fixtures/test.jpeg +1 -0
  83. data/spec/fixtures/test.jpg +1 -0
  84. data/spec/mount_spec.rb +538 -0
  85. data/spec/orm/activerecord_spec.rb +271 -0
  86. data/spec/orm/datamapper_spec.rb +168 -0
  87. data/spec/orm/mongoid_spec.rb +202 -0
  88. data/spec/orm/mongomapper_spec.rb +202 -0
  89. data/spec/orm/sequel_spec.rb +183 -0
  90. data/spec/processing/image_science_spec.rb +56 -0
  91. data/spec/processing/mini_magick_spec.rb +76 -0
  92. data/spec/processing/rmagick_spec.rb +75 -0
  93. data/spec/sanitized_file_spec.rb +623 -0
  94. data/spec/spec_helper.rb +92 -0
  95. data/spec/storage/cloudfiles_spec.rb +78 -0
  96. data/spec/storage/grid_fs_spec.rb +83 -0
  97. data/spec/storage/s3_spec.rb +118 -0
  98. data/spec/uploader/cache_spec.rb +209 -0
  99. data/spec/uploader/configuration_spec.rb +105 -0
  100. data/spec/uploader/default_url_spec.rb +85 -0
  101. data/spec/uploader/download_spec.rb +75 -0
  102. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  103. data/spec/uploader/mountable_spec.rb +33 -0
  104. data/spec/uploader/paths_spec.rb +22 -0
  105. data/spec/uploader/processing_spec.rb +73 -0
  106. data/spec/uploader/proxy_spec.rb +54 -0
  107. data/spec/uploader/remove_spec.rb +70 -0
  108. data/spec/uploader/store_spec.rb +264 -0
  109. data/spec/uploader/url_spec.rb +102 -0
  110. data/spec/uploader/versions_spec.rb +298 -0
  111. metadata +433 -0
@@ -0,0 +1,271 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ require 'carrierwave/orm/activerecord'
6
+
7
+ # change this if sqlite is unavailable
8
+ dbconfig = {
9
+ :adapter => 'sqlite3',
10
+ :database => ':memory:'
11
+ }
12
+
13
+ ActiveRecord::Base.establish_connection(dbconfig)
14
+ ActiveRecord::Migration.verbose = false
15
+
16
+ class TestMigration < ActiveRecord::Migration
17
+ def self.up
18
+ create_table :events, :force => true do |t|
19
+ t.column :image, :string
20
+ t.column :textfile, :string
21
+ t.column :foo, :string
22
+ end
23
+ end
24
+
25
+ def self.down
26
+ drop_table :events
27
+ end
28
+ end
29
+
30
+ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
31
+ $arclass = 0
32
+
33
+ describe CarrierWave::ActiveRecord do
34
+
35
+ describe '.mount_uploader' do
36
+
37
+ before(:all) { TestMigration.up }
38
+ after(:all) { TestMigration.down }
39
+ after { Event.delete_all }
40
+
41
+ before do
42
+ # My god, what a horrible, horrible solution, but AR validations don't work
43
+ # unless the class has a name. This is the best I could come up with :S
44
+ $arclass += 1
45
+ eval <<-RUBY
46
+ class Event#{$arclass} < Event; end
47
+ @class = Event#{$arclass}
48
+ RUBY
49
+ @class.table_name = "events"
50
+ @uploader = Class.new(CarrierWave::Uploader::Base)
51
+ @class.mount_uploader(:image, @uploader)
52
+ @event = @class.new
53
+ end
54
+
55
+ describe '#image' do
56
+
57
+ it "should return blank uploader when nothing has been assigned" do
58
+ @event.image.should be_blank
59
+ end
60
+
61
+ it "should return blank uploader when an empty string has been assigned" do
62
+ @event[:image] = ''
63
+ @event.save
64
+ @event.reload
65
+ @event.image.should be_blank
66
+ end
67
+
68
+ it "should retrieve a file from the storage if a value is stored in the database" do
69
+ @event[:image] = 'test.jpeg'
70
+ @event.save
71
+ @event.reload
72
+ @event.image.should be_an_instance_of(@uploader)
73
+ end
74
+
75
+ it "should set the path to the store dir" do
76
+ @event[:image] = 'test.jpeg'
77
+ @event.save
78
+ @event.reload
79
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
80
+ end
81
+
82
+ end
83
+
84
+ describe '#image=' do
85
+
86
+ it "should cache a file" do
87
+ @event.image = stub_file('test.jpeg')
88
+ @event.image.should be_an_instance_of(@uploader)
89
+ end
90
+
91
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
92
+ @event[:image].should be_nil
93
+ end
94
+
95
+ it "should copy a file into into the cache directory" do
96
+ @event.image = stub_file('test.jpeg')
97
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
98
+ end
99
+
100
+ it "should do nothing when nil is assigned" do
101
+ @event.image = nil
102
+ @event.image.should be_blank
103
+ end
104
+
105
+ it "should do nothing when an empty string is assigned" do
106
+ @event.image = ''
107
+ @event.image.should be_blank
108
+ end
109
+
110
+ it "should make the record invalid when an integrity error occurs" do
111
+ @uploader.class_eval do
112
+ def extension_white_list
113
+ %(txt)
114
+ end
115
+ end
116
+ @event.image = stub_file('test.jpg')
117
+ @event.should_not be_valid
118
+ end
119
+
120
+ it "should make the record invalid when a processing error occurs" do
121
+ @uploader.class_eval do
122
+ process :monkey
123
+ def monkey
124
+ raise CarrierWave::ProcessingError, "Ohh noez!"
125
+ end
126
+ end
127
+ @event.image = stub_file('test.jpg')
128
+ @event.should_not be_valid
129
+ end
130
+
131
+ end
132
+
133
+ describe '#save' do
134
+
135
+ it "should do nothing when no file has been assigned" do
136
+ @event.save.should be_true
137
+ @event.image.should be_blank
138
+ end
139
+
140
+ it "should copy the file to the upload directory when a file has been assigned" do
141
+ @event.image = stub_file('test.jpeg')
142
+ @event.save.should be_true
143
+ @event.image.should be_an_instance_of(@uploader)
144
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
145
+ end
146
+
147
+ it "should do nothing when a validation fails" do
148
+ @class.validate { |r| r.errors.add :textfile, "FAIL!" }
149
+ @event.image = stub_file('test.jpeg')
150
+ @event.save.should be_false
151
+ @event.image.should be_an_instance_of(@uploader)
152
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
153
+ end
154
+
155
+ it "should assign the filename to the database" do
156
+ @event.image = stub_file('test.jpeg')
157
+ @event.save.should be_true
158
+ @event.reload
159
+ @event[:image].should == 'test.jpeg'
160
+ end
161
+
162
+ it "should preserve the image when nothing is assigned" do
163
+ @event.image = stub_file('test.jpeg')
164
+ @event.save.should be_true
165
+ @event = @class.find(@event.id)
166
+ @event.foo = "bar"
167
+ @event.save.should be_true
168
+ @event[:image].should == 'test.jpeg'
169
+ end
170
+
171
+ it "should remove the image if remove_image? returns true" do
172
+ @event.image = stub_file('test.jpeg')
173
+ @event.save!
174
+ @event.remove_image = true
175
+ @event.save!
176
+ @event.reload
177
+ @event.image.should be_blank
178
+ @event[:image].should == ''
179
+ end
180
+
181
+ end
182
+
183
+ describe '#destroy' do
184
+
185
+ it "should do nothing when no file has been assigned" do
186
+ @event.save.should be_true
187
+ @event.destroy
188
+ end
189
+
190
+ it "should remove the file from the filesystem" do
191
+ @event.image = stub_file('test.jpeg')
192
+ @event.save.should be_true
193
+ @event.image.should be_an_instance_of(@uploader)
194
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
195
+ @event.destroy
196
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
197
+ end
198
+
199
+ end
200
+
201
+ describe 'with overriddent filename' do
202
+
203
+ describe '#save' do
204
+
205
+ before do
206
+ @uploader.class_eval do
207
+ def filename
208
+ model.name + File.extname(super)
209
+ end
210
+ end
211
+ @event.stub!(:name).and_return('jonas')
212
+ end
213
+
214
+ it "should copy the file to the upload directory when a file has been assigned" do
215
+ @event.image = stub_file('test.jpeg')
216
+ @event.save.should be_true
217
+ @event.image.should be_an_instance_of(@uploader)
218
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
219
+ end
220
+
221
+ it "should assign an overridden filename to the database" do
222
+ @event.image = stub_file('test.jpeg')
223
+ @event.save.should be_true
224
+ @event.reload
225
+ @event[:image].should == 'jonas.jpeg'
226
+ end
227
+
228
+ end
229
+
230
+ end
231
+
232
+ describe 'with validates_presence_of' do
233
+
234
+ before do
235
+ @class.validates_presence_of :image
236
+ @event.stub!(:name).and_return('jonas')
237
+ end
238
+
239
+ it "should be valid if a file has been cached" do
240
+ @event.image = stub_file('test.jpeg')
241
+ @event.should be_valid
242
+ end
243
+
244
+ it "should not be valid if a file has not been cached" do
245
+ @event.should_not be_valid
246
+ end
247
+
248
+ end
249
+
250
+ describe 'with validates_size_of' do
251
+
252
+ before do
253
+ @class.validates_size_of :image, :maximum => 40
254
+ @event.stub!(:name).and_return('jonas')
255
+ end
256
+
257
+ it "should be valid if a file has been cached that matches the size criteria" do
258
+ @event.image = stub_file('test.jpeg')
259
+ @event.should be_valid
260
+ end
261
+
262
+ it "should not be valid if a file has been cached that does not match the size criteria" do
263
+ @event.image = stub_file('bork.txt')
264
+ @event.should_not be_valid
265
+ end
266
+
267
+ end
268
+
269
+ end
270
+
271
+ end
@@ -0,0 +1,168 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ require 'dm-core'
6
+ require 'dm-validations'
7
+ require 'carrierwave/orm/datamapper'
8
+
9
+ DataMapper.setup(:default, 'sqlite3::memory:')
10
+
11
+ describe CarrierWave::DataMapper do
12
+
13
+ before do
14
+ uploader = Class.new(CarrierWave::Uploader::Base)
15
+
16
+ @class = Class.new
17
+ @class.class_eval do
18
+ include DataMapper::Resource
19
+
20
+ storage_names[:default] = 'events'
21
+
22
+ property :id, DataMapper::Types::Serial
23
+
24
+ # auto validation off currently for inferred type
25
+ # validation. issue is that validations are done on
26
+ # the value returned by model#property_name which is overloaded
27
+ # by carrierwave. pull request in @dm-more to remedy this.
28
+ property :image, String, :auto_validation => false
29
+
30
+ mount_uploader :image, uploader
31
+ end
32
+
33
+ @class.auto_migrate!
34
+
35
+ @uploader = uploader
36
+
37
+ @event = @class.new
38
+ end
39
+
40
+ describe '#image' do
41
+
42
+ it "should return blank uploader when nothing has been assigned" do
43
+ @event.image.should be_blank
44
+ end
45
+
46
+ it "should return blank uploader when an empty string has been assigned" do
47
+ repository(:default).adapter.execute("INSERT INTO events (image) VALUES ('')")
48
+ @event = @class.first
49
+
50
+ @event.image.should be_blank
51
+ end
52
+
53
+ it "should retrieve a file from the storage if a value is stored in the database" do
54
+ repository(:default).adapter.execute("INSERT INTO events (image) VALUES ('test.jpg')")
55
+ @event = @class.first
56
+
57
+ @event.save
58
+ @event.reload
59
+ @event.image.should be_an_instance_of(@uploader)
60
+ end
61
+
62
+ it "should set the path to the store dir" do
63
+ @event.attribute_set(:image, 'test.jpeg')
64
+ @event.save
65
+ @event.reload
66
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
67
+ end
68
+
69
+ end
70
+
71
+ describe '#image=' do
72
+
73
+ it "should cache a file" do
74
+ @event.image = stub_file('test.jpeg')
75
+ @event.image.should be_an_instance_of(@uploader)
76
+ end
77
+
78
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
79
+ @event.attribute_get(:image).should be_nil
80
+ end
81
+
82
+ it "should copy a file into into the cache directory" do
83
+ @event.image = stub_file('test.jpeg')
84
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
85
+ end
86
+
87
+ it "should do nothing when nil is assigned" do
88
+ @event.image = nil
89
+ @event.image.should be_blank
90
+ end
91
+
92
+ it "should do nothing when an empty string is assigned" do
93
+ @event.image = ''
94
+ @event.image.should be_blank
95
+ end
96
+
97
+ end
98
+
99
+ describe '#save' do
100
+
101
+ it "should do nothing when no file has been assigned" do
102
+ @event.save
103
+ @event.image.should be_blank
104
+ end
105
+
106
+ it "should copy the file to the upload directory when a file has been assigned" do
107
+ @event.image = stub_file('test.jpeg')
108
+ @event.save
109
+ @event.image.should be_an_instance_of(@uploader)
110
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
111
+ end
112
+
113
+ it "should assign the filename to the database" do
114
+ @event.image = stub_file('test.jpeg')
115
+ @event.save
116
+ @event.reload
117
+ @event.attribute_get(:image).should == 'test.jpeg'
118
+ end
119
+
120
+ it "should remove the image if remove_image? returns true" do
121
+ @event.image = stub_file('test.jpeg')
122
+ @event.save
123
+ @event.remove_image = true
124
+ @event.save
125
+ @event.reload
126
+ @event.image.should be_blank
127
+ @event.attribute_get(:image).should == ''
128
+ end
129
+
130
+ describe "with validations" do
131
+ it "should do nothing when a validation fails" do
132
+ @class.validates_with_block(:textfile) { [false, "FAIL!"] }
133
+ @event.image = stub_file('test.jpeg')
134
+ @event.save
135
+ @event.image.should be_an_instance_of(@uploader)
136
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
137
+ end
138
+
139
+ it "should assign the filename before validation" do
140
+ @class.validates_with_block(:image) { [false, "FAIL!"] if self.image.nil? }
141
+ @event.image = stub_file('test.jpeg')
142
+ @event.save
143
+ @event.reload
144
+ @event.attribute_get(:image).should == 'test.jpeg'
145
+ end
146
+ end
147
+
148
+ end
149
+
150
+ describe '#destroy' do
151
+
152
+ it "should do nothing when no file has been assigned" do
153
+ @event.destroy
154
+ end
155
+
156
+ it "should remove the file from the filesystem" do
157
+ @event.image = stub_file('test.jpeg')
158
+ @event.save.should be_true
159
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
160
+ @event.image.should be_an_instance_of(@uploader)
161
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
162
+ @event.destroy
163
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
164
+ end
165
+
166
+ end
167
+
168
+ end
@@ -0,0 +1,202 @@
1
+ # encoding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ require 'carrierwave/orm/mongoid'
5
+
6
+ connection = Mongo::Connection.new
7
+ Mongoid.database = connection.db("carrierwave_test")
8
+
9
+ MongoidUploader = Class.new(CarrierWave::Uploader::Base)
10
+ MongoidUser = Class.new
11
+ MongoidUser.class_eval do
12
+ include Mongoid::Document
13
+ store_in :users
14
+ mount_uploader :image, MongoidUploader
15
+ end
16
+
17
+ describe CarrierWave::Mongoid do
18
+
19
+ after do
20
+ MongoidUser.collection.drop
21
+ end
22
+
23
+ describe '#image' do
24
+
25
+ context "when nothing is assigned" do
26
+
27
+ before do
28
+ @document = MongoidUser.new
29
+ end
30
+
31
+ it "returns a blank uploader" do
32
+ @document.image.should be_blank
33
+ end
34
+
35
+ end
36
+
37
+ context "when an empty string is assigned" do
38
+
39
+ before do
40
+ @document = MongoidUser.new(:image_filename => "")
41
+ @document.save
42
+ end
43
+
44
+ it "returns a blank uploader" do
45
+ @saved_doc = MongoidUser.first
46
+ @saved_doc.image.should be_blank
47
+ end
48
+
49
+ end
50
+
51
+ context "when a filename is saved in the database" do
52
+
53
+ before do
54
+ @document = MongoidUser.new(:image_filename => "test.jpg")
55
+ @document.save
56
+ @doc = MongoidUser.first
57
+ end
58
+
59
+ it "returns an uploader" do
60
+ @doc.image.should be_an_instance_of(MongoidUploader)
61
+ end
62
+
63
+ it "sets the path to the store directory" do
64
+ @doc.image.current_path.should == public_path('uploads/test.jpg')
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ describe '#image=' do
72
+
73
+ before do
74
+ @doc = MongoidUser.new
75
+ end
76
+
77
+ context "when nil is assigned" do
78
+
79
+ it "does not set the value" do
80
+ @doc.image = nil
81
+ @doc.image.should be_blank
82
+ end
83
+
84
+ end
85
+
86
+ context "when an empty string is assigned" do
87
+
88
+ it "does not set the value" do
89
+ @doc.image = ''
90
+ @doc.image.should be_blank
91
+ end
92
+
93
+ end
94
+
95
+ context "when a file is assigned" do
96
+
97
+ it "should cache a file" do
98
+ @doc.image = stub_file('test.jpeg')
99
+ @doc.image.should be_an_instance_of(MongoidUploader)
100
+ end
101
+
102
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
103
+ @doc.image_filename.should be_nil
104
+ end
105
+
106
+ it "should copy a file into into the cache directory" do
107
+ @doc.image = stub_file('test.jpeg')
108
+ @doc.image.current_path.should =~ /^#{public_path('uploads\/tmp')}/
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ describe "#save" do
116
+
117
+ before do
118
+ @doc = MongoidUser.new
119
+ end
120
+
121
+ context "when no file is assigned" do
122
+
123
+ it "image is blank" do
124
+ @doc.save
125
+ @doc.image.should be_blank
126
+ end
127
+
128
+ end
129
+
130
+ context "when a file is assigned" do
131
+
132
+ it "copies the file to the upload directory" do
133
+ @doc.image = stub_file('test.jpg')
134
+ @doc.save
135
+ @doc.image.should be_an_instance_of(MongoidUploader)
136
+ @doc.image.current_path.should == public_path('uploads/test.jpg')
137
+ end
138
+
139
+ it "saves the filename in the database" do
140
+ @doc.image = stub_file('test.jpg')
141
+ @doc.save
142
+ @doc.image_filename.should == 'test.jpg'
143
+ end
144
+
145
+ context "when remove_image? is true" do
146
+
147
+ it "removes the image" do
148
+ @doc.image = stub_file('test.jpeg')
149
+ @doc.save
150
+ @doc.remove_image = true
151
+ @doc.save
152
+ @doc.image.should be_blank
153
+ @doc.image_filename.should == ''
154
+ end
155
+
156
+ end
157
+
158
+ end
159
+
160
+ end
161
+
162
+ describe '#destroy' do
163
+
164
+ before do
165
+ @doc = MongoidUser.new
166
+ end
167
+
168
+ describe "when file assigned" do
169
+
170
+ it "removes the file from the filesystem" do
171
+ @doc.image = stub_file('test.jpeg')
172
+ @doc.save.should be_true
173
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
174
+ @doc.image.should be_an_instance_of(MongoidUploader)
175
+ @doc.image.current_path.should == public_path('uploads/test.jpeg')
176
+ @doc.destroy
177
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
178
+ end
179
+
180
+ end
181
+
182
+ describe "when file is not assigned" do
183
+
184
+ it "deletes the instance of MongoidUser after save" do
185
+ @doc.save
186
+ MongoidUser.count.should eql(1)
187
+ @doc.destroy
188
+ end
189
+
190
+ it "deletes the instance of MongoidUser after save and then re-looking up the instance" do
191
+ @doc.save
192
+ MongoidUser.count.should eql(1)
193
+ @doc = MongoidUser.first
194
+ @doc.destroy
195
+ end
196
+
197
+ end
198
+
199
+ end
200
+
201
+
202
+ end