andrewtimberlake-carrierwave 0.3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. data/Generators +4 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +314 -0
  4. data/Rakefile +29 -0
  5. data/lib/carrierwave.rb +140 -0
  6. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  7. data/lib/carrierwave/core_ext/blank.rb +46 -0
  8. data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
  9. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  10. data/lib/carrierwave/mount.rb +332 -0
  11. data/lib/carrierwave/orm/activerecord.rb +73 -0
  12. data/lib/carrierwave/orm/datamapper.rb +27 -0
  13. data/lib/carrierwave/orm/sequel.rb +57 -0
  14. data/lib/carrierwave/processing/image_science.rb +72 -0
  15. data/lib/carrierwave/processing/rmagick.rb +286 -0
  16. data/lib/carrierwave/sanitized_file.rb +272 -0
  17. data/lib/carrierwave/storage/abstract.rb +32 -0
  18. data/lib/carrierwave/storage/file.rb +50 -0
  19. data/lib/carrierwave/storage/s3.rb +215 -0
  20. data/lib/carrierwave/test/matchers.rb +114 -0
  21. data/lib/carrierwave/uploader.rb +43 -0
  22. data/lib/carrierwave/uploader/cache.rb +116 -0
  23. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  24. data/lib/carrierwave/uploader/default_path.rb +23 -0
  25. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  26. data/lib/carrierwave/uploader/mountable.rb +39 -0
  27. data/lib/carrierwave/uploader/paths.rb +27 -0
  28. data/lib/carrierwave/uploader/processing.rb +81 -0
  29. data/lib/carrierwave/uploader/proxy.rb +62 -0
  30. data/lib/carrierwave/uploader/remove.rb +23 -0
  31. data/lib/carrierwave/uploader/store.rb +156 -0
  32. data/lib/carrierwave/uploader/url.rb +24 -0
  33. data/lib/carrierwave/uploader/versions.rb +147 -0
  34. data/lib/generators/uploader_generator.rb +22 -0
  35. data/rails_generators/uploader/USAGE +2 -0
  36. data/rails_generators/uploader/templates/uploader.rb +47 -0
  37. data/rails_generators/uploader/uploader_generator.rb +21 -0
  38. data/spec/compatibility/paperclip_spec.rb +43 -0
  39. data/spec/fixtures/bork.txt +1 -0
  40. data/spec/fixtures/test.jpeg +1 -0
  41. data/spec/fixtures/test.jpg +1 -0
  42. data/spec/mount_spec.rb +517 -0
  43. data/spec/orm/activerecord_spec.rb +271 -0
  44. data/spec/orm/datamapper_spec.rb +161 -0
  45. data/spec/orm/sequel_spec.rb +192 -0
  46. data/spec/sanitized_file_spec.rb +612 -0
  47. data/spec/spec_helper.rb +99 -0
  48. data/spec/uploader/cache_spec.rb +196 -0
  49. data/spec/uploader/default_path_spec.rb +68 -0
  50. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  51. data/spec/uploader/mountable_spec.rb +33 -0
  52. data/spec/uploader/paths_spec.rb +22 -0
  53. data/spec/uploader/processing_spec.rb +62 -0
  54. data/spec/uploader/proxy_spec.rb +54 -0
  55. data/spec/uploader/remove_spec.rb +70 -0
  56. data/spec/uploader/store_spec.rb +274 -0
  57. data/spec/uploader/url_spec.rb +87 -0
  58. data/spec/uploader/versions_spec.rb +306 -0
  59. metadata +127 -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,161 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ require 'carrierwave/orm/datamapper'
6
+
7
+ DataMapper.setup(:default, 'sqlite3::memory:')
8
+
9
+ describe CarrierWave::DataMapper do
10
+
11
+ before do
12
+ uploader = Class.new(CarrierWave::Uploader::Base)
13
+
14
+ @class = Class.new
15
+ @class.class_eval do
16
+ include DataMapper::Resource
17
+
18
+ storage_names[:default] = 'events'
19
+
20
+ property :id, Integer, :key => true
21
+ property :image, String
22
+
23
+ mount_uploader :image, uploader
24
+ end
25
+
26
+ @class.auto_migrate!
27
+
28
+ @uploader = uploader
29
+
30
+ @event = @class.new
31
+ end
32
+
33
+ describe '#image' do
34
+
35
+ it "should return blank uploader when nothing has been assigned" do
36
+ @event.image.should be_blank
37
+ end
38
+
39
+ it "should return blank uploader when an empty string has been assigned" do
40
+ repository(:default).adapter.query("INSERT INTO events (image) VALUES ('')")
41
+ @event = @class.first
42
+
43
+ @event.image.should be_blank
44
+ end
45
+
46
+ it "should retrieve a file from the storage if a value is stored in the database" do
47
+ repository(:default).adapter.query("INSERT INTO events (image) VALUES ('test.jpg')")
48
+ @event = @class.first
49
+
50
+ @event.save
51
+ @event.reload
52
+ @event.image.should be_an_instance_of(@uploader)
53
+ end
54
+
55
+ it "should set the path to the store dir" do
56
+ @event.attribute_set(:image, 'test.jpeg')
57
+ @event.save
58
+ @event.reload
59
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
60
+ end
61
+
62
+ end
63
+
64
+ describe '#image=' do
65
+
66
+ it "should cache a file" do
67
+ @event.image = stub_file('test.jpeg')
68
+ @event.image.should be_an_instance_of(@uploader)
69
+ end
70
+
71
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
72
+ @event.attribute_get(:image).should be_nil
73
+ end
74
+
75
+ it "should copy a file into into the cache directory" do
76
+ @event.image = stub_file('test.jpeg')
77
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
78
+ end
79
+
80
+ it "should do nothing when nil is assigned" do
81
+ @event.image = nil
82
+ @event.image.should be_blank
83
+ end
84
+
85
+ it "should do nothing when an empty string is assigned" do
86
+ @event.image = ''
87
+ @event.image.should be_blank
88
+ end
89
+
90
+ end
91
+
92
+ describe '#save' do
93
+
94
+ it "should do nothing when no file has been assigned" do
95
+ @event.save
96
+ @event.image.should be_blank
97
+ end
98
+
99
+ it "should copy the file to the upload directory when a file has been assigned" do
100
+ @event.image = stub_file('test.jpeg')
101
+ @event.save
102
+ @event.image.should be_an_instance_of(@uploader)
103
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
104
+ end
105
+
106
+ # it "should do nothing when a validation fails" do
107
+ # pending "how do we test with and without dm-validations?"
108
+ # @class.validate { |r| r.errors.add :textfile, "FAIL!" }
109
+ # @event.image = stub_file('test.jpeg')
110
+ # @event.save
111
+ # @event.image.should be_an_instance_of(@uploader)
112
+ # @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
113
+ # end
114
+
115
+ it "should assign the filename to the database" do
116
+ @event.image = stub_file('test.jpeg')
117
+ @event.save
118
+ @event.reload
119
+ @event.attribute_get(:image).should == 'test.jpeg'
120
+ end
121
+
122
+ # it "should assign the filename before validation" do
123
+ # pending "how do we test with and without dm-validations?"
124
+ # @class.validate { |r| r.errors.add_to_base "FAIL!" if r[:image].nil? }
125
+ # @event.image = stub_file('test.jpeg')
126
+ # @event.save
127
+ # @event.reload
128
+ # @event.attribute_get(:image).should == 'test.jpeg'
129
+ # end
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_blank
138
+ @event.attribute_get(:image).should == ''
139
+ end
140
+
141
+ end
142
+
143
+ describe '#destroy' do
144
+
145
+ it "should do nothing when no file has been assigned" do
146
+ @event.destroy
147
+ end
148
+
149
+ it "should remove the file from the filesystem" do
150
+ @event.image = stub_file('test.jpeg')
151
+ @event.save.should be_true
152
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
153
+ @event.image.should be_an_instance_of(@uploader)
154
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
155
+ @event.destroy
156
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
157
+ end
158
+
159
+ end
160
+
161
+ end
@@ -0,0 +1,192 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ require 'carrierwave/orm/sequel'
6
+
7
+ DB = Sequel.sqlite
8
+
9
+ describe CarrierWave::Sequel do
10
+
11
+ def setup_variables_for_class(klass)
12
+ uploader = Class.new(CarrierWave::Uploader::Base)
13
+ klass.mount_uploader(:image, uploader)
14
+ model = klass.new
15
+ [klass, uploader, model]
16
+ end
17
+
18
+ describe '.mount_uploader' do
19
+
20
+ before(:all) do
21
+ DB.create_table :events do
22
+ primary_key :id
23
+ column :image, :string
24
+ column :textfile, :string
25
+ end
26
+ end
27
+
28
+ after(:all) do
29
+ DB.drop_table :events
30
+ end
31
+
32
+ before(:each) do
33
+ @class = Class.new(Sequel::Model)
34
+ @class.set_dataset :events
35
+ @class, @uploader, @event = setup_variables_for_class(@class)
36
+ end
37
+
38
+ after(:each) { @class.delete }
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
+ @event[:image] = ''
48
+ @event.save
49
+ @event.reload
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
+ @event[:image] = 'test.jpeg'
55
+ @event.save
56
+ @event.reload
57
+ @event.image.should be_an_instance_of(@uploader)
58
+ end
59
+
60
+ it "should set the path to the store dir" do
61
+ @event[:image] = 'test.jpeg'
62
+ @event.save
63
+ @event.reload
64
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
65
+ end
66
+
67
+ end
68
+
69
+ describe '#image=' do
70
+
71
+ it "should cache a file" do
72
+ @event.image = stub_file('test.jpeg')
73
+ @event.image.should be_an_instance_of(@uploader)
74
+ end
75
+
76
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
77
+ @event[:image].should be_nil
78
+ end
79
+
80
+ it "should copy a file into into the cache directory" do
81
+ @event.image = stub_file('test.jpeg')
82
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
83
+ end
84
+
85
+ it "should do nothing when nil is assigned" do
86
+ @event.image = nil
87
+ @event.image.should be_blank
88
+ end
89
+
90
+ it "should do nothing when an empty string is assigned" do
91
+ @event.image = ''
92
+ @event.image.should be_blank
93
+ end
94
+
95
+ end
96
+
97
+ describe '#save' do
98
+
99
+ it "should do nothing when no file has been assigned" do
100
+ @event.save.should be_true
101
+ @event.image.should be_blank
102
+ end
103
+
104
+ it "should copy the file to the upload directory when a file has been assigned" do
105
+ @event.image = stub_file('test.jpeg')
106
+ @event.save.should be_true
107
+ @event.image.should be_an_instance_of(@uploader)
108
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
109
+ end
110
+
111
+ describe 'with validation' do
112
+
113
+ before do
114
+ # Add validations
115
+ if CarrierWave::Sequel.new_sequel?
116
+ @class.class_eval do
117
+ def validate
118
+ errors.add(:image, 'FAIL!')
119
+ end
120
+ end
121
+ else
122
+ @class.class_eval do
123
+ validates_each(:image) do |o,a,v|
124
+ o.errors.add(a, 'FAIL!')
125
+ end
126
+ end
127
+ end
128
+ # Turn off raising the exceptions on save
129
+ @event.raise_on_save_failure = false
130
+ end
131
+
132
+ it "should do nothing when a validation fails" do
133
+ @event.image = stub_file('test.jpeg')
134
+ @event.should_not be_valid
135
+ @event.save
136
+ @event.should be_new
137
+ @event.image.should be_an_instance_of(@uploader)
138
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
139
+ end
140
+ end
141
+
142
+ it "should assign the filename to the database" do
143
+ @event.image = stub_file('test.jpeg')
144
+ @event.save.should be_true
145
+ @event.reload
146
+ @event[:image].should == 'test.jpeg'
147
+ end
148
+
149
+ it "should remove the image if remove_image? returns true" do
150
+ @event.image = stub_file('test.jpeg')
151
+ @event.save
152
+ @event.remove_image = true
153
+ @event.save
154
+ @event.reload
155
+ @event.image.should be_blank
156
+ @event[:image].should == ''
157
+ end
158
+ end
159
+
160
+ describe 'with overriddent filename' do
161
+
162
+ describe '#save' do
163
+
164
+ before do
165
+ @uploader.class_eval do
166
+ def filename
167
+ model.name + File.extname(super)
168
+ end
169
+ end
170
+ @event.stub!(:name).and_return('jonas')
171
+ end
172
+
173
+ it "should copy the file to the upload directory when a file has been assigned" do
174
+ @event.image = stub_file('test.jpeg')
175
+ @event.save.should be_true
176
+ @event.image.should be_an_instance_of(@uploader)
177
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
178
+ end
179
+
180
+ it "should assign an overridden filename to the database" do
181
+ @event.image = stub_file('test.jpeg')
182
+ @event.save.should be_true
183
+ @event.reload
184
+ @event[:image].should == 'jonas.jpeg'
185
+ end
186
+
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+ end