carrierwave-rails3 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/README.rdoc +527 -0
  2. data/lib/carrierwave.rb +103 -0
  3. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  4. data/lib/carrierwave/core_ext/file.rb +11 -0
  5. data/lib/carrierwave/mount.rb +359 -0
  6. data/lib/carrierwave/orm/activerecord.rb +75 -0
  7. data/lib/carrierwave/orm/datamapper.rb +27 -0
  8. data/lib/carrierwave/orm/mongoid.rb +23 -0
  9. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  10. data/lib/carrierwave/orm/sequel.rb +45 -0
  11. data/lib/carrierwave/processing/image_science.rb +116 -0
  12. data/lib/carrierwave/processing/mini_magick.rb +261 -0
  13. data/lib/carrierwave/processing/rmagick.rb +278 -0
  14. data/lib/carrierwave/sanitized_file.rb +273 -0
  15. data/lib/carrierwave/storage/abstract.rb +30 -0
  16. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  17. data/lib/carrierwave/storage/file.rb +48 -0
  18. data/lib/carrierwave/storage/grid_fs.rb +104 -0
  19. data/lib/carrierwave/storage/right_s3.rb +3 -0
  20. data/lib/carrierwave/storage/s3.rb +206 -0
  21. data/lib/carrierwave/test/matchers.rb +164 -0
  22. data/lib/carrierwave/uploader.rb +44 -0
  23. data/lib/carrierwave/uploader/cache.rb +146 -0
  24. data/lib/carrierwave/uploader/callbacks.rb +41 -0
  25. data/lib/carrierwave/uploader/configuration.rb +134 -0
  26. data/lib/carrierwave/uploader/default_url.rb +19 -0
  27. data/lib/carrierwave/uploader/download.rb +60 -0
  28. data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
  29. data/lib/carrierwave/uploader/mountable.rb +39 -0
  30. data/lib/carrierwave/uploader/processing.rb +84 -0
  31. data/lib/carrierwave/uploader/proxy.rb +62 -0
  32. data/lib/carrierwave/uploader/remove.rb +23 -0
  33. data/lib/carrierwave/uploader/store.rb +90 -0
  34. data/lib/carrierwave/uploader/url.rb +33 -0
  35. data/lib/carrierwave/uploader/versions.rb +147 -0
  36. data/lib/generators/templates/uploader.rb +47 -0
  37. data/lib/generators/uploader_generator.rb +13 -0
  38. data/spec/compatibility/paperclip_spec.rb +52 -0
  39. data/spec/mount_spec.rb +538 -0
  40. data/spec/orm/activerecord_spec.rb +271 -0
  41. data/spec/orm/datamapper_spec.rb +168 -0
  42. data/spec/orm/mongoid_spec.rb +202 -0
  43. data/spec/orm/mongomapper_spec.rb +202 -0
  44. data/spec/orm/sequel_spec.rb +183 -0
  45. data/spec/processing/image_science_spec.rb +56 -0
  46. data/spec/processing/mini_magick_spec.rb +76 -0
  47. data/spec/processing/rmagick_spec.rb +75 -0
  48. data/spec/sanitized_file_spec.rb +623 -0
  49. data/spec/spec_helper.rb +92 -0
  50. data/spec/storage/cloudfiles_spec.rb +78 -0
  51. data/spec/storage/grid_fs_spec.rb +86 -0
  52. data/spec/storage/s3_spec.rb +118 -0
  53. data/spec/uploader/cache_spec.rb +209 -0
  54. data/spec/uploader/callback_spec.rb +24 -0
  55. data/spec/uploader/configuration_spec.rb +105 -0
  56. data/spec/uploader/default_url_spec.rb +85 -0
  57. data/spec/uploader/download_spec.rb +75 -0
  58. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  59. data/spec/uploader/mountable_spec.rb +33 -0
  60. data/spec/uploader/paths_spec.rb +22 -0
  61. data/spec/uploader/processing_spec.rb +73 -0
  62. data/spec/uploader/proxy_spec.rb +54 -0
  63. data/spec/uploader/remove_spec.rb +70 -0
  64. data/spec/uploader/store_spec.rb +264 -0
  65. data/spec/uploader/url_spec.rb +102 -0
  66. data/spec/uploader/versions_spec.rb +298 -0
  67. metadata +128 -0
@@ -0,0 +1,202 @@
1
+ # encoding: utf-8
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ require 'carrierwave/orm/mongomapper'
5
+
6
+ MongoMapper.database = "carrierwave_test"
7
+
8
+ describe CarrierWave::MongoMapper do
9
+
10
+ before do
11
+ uploader = Class.new(CarrierWave::Uploader::Base)
12
+
13
+ @class = Class.new
14
+ @class.class_eval do
15
+ include MongoMapper::Document
16
+ mount_uploader :image, uploader
17
+ end
18
+
19
+ @uploader = uploader
20
+ end
21
+
22
+ describe '#image' do
23
+
24
+ context "when nothing is assigned" do
25
+
26
+ before do
27
+ @document = @class.new
28
+ end
29
+
30
+ it "returns a blank uploader" do
31
+ @document.image.should be_blank
32
+ end
33
+
34
+ end
35
+
36
+ context "when an empty string is assigned" do
37
+
38
+ before do
39
+ @document = @class.new(:image_filename => "")
40
+ @document.save
41
+ end
42
+
43
+ it "returns a blank uploader" do
44
+ @saved_doc = @class.first
45
+ @saved_doc.image.should be_blank
46
+ end
47
+
48
+ end
49
+
50
+ context "when a filename is saved in the database" do
51
+
52
+ before do
53
+ @document = @class.new(:image_filename => "test.jpg")
54
+ @document.save
55
+ @doc = @class.first
56
+ end
57
+
58
+ it "returns an uploader" do
59
+ @doc.image.should be_an_instance_of(@uploader)
60
+ end
61
+
62
+ it "sets the path to the store directory" do
63
+ @doc.image.current_path.should == public_path('uploads/test.jpg')
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe '#image=' do
71
+
72
+ before do
73
+ @doc = @class.new
74
+ end
75
+
76
+ context "when nil is assigned" do
77
+
78
+ it "does not set the value" do
79
+ @doc.image = nil
80
+ @doc.image.should be_blank
81
+ end
82
+
83
+ end
84
+
85
+ context "when an empty string is assigned" do
86
+
87
+ it "does not set the value" do
88
+ @doc.image = ''
89
+ @doc.image.should be_blank
90
+ end
91
+
92
+ end
93
+
94
+ context "when a file is assigned" do
95
+
96
+ it "should cache a file" do
97
+ @doc.image = stub_file('test.jpeg')
98
+ @doc.image.should be_an_instance_of(@uploader)
99
+ end
100
+
101
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
102
+ @doc[:image_filename].should be_nil
103
+ end
104
+
105
+ it "should copy a file into into the cache directory" do
106
+ @doc.image = stub_file('test.jpeg')
107
+ @doc.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+
114
+ describe "#save" do
115
+
116
+ before do
117
+ @doc = @class.new
118
+ end
119
+
120
+ context "when no file is assigned" do
121
+
122
+ it "image is blank" do
123
+ @doc.save
124
+ @doc.image.should be_blank
125
+ end
126
+
127
+ end
128
+
129
+ context "when a file is assigned" do
130
+
131
+ it "copies the file to the upload directory" do
132
+ @doc.image = stub_file('test.jpg')
133
+ @doc.save
134
+ @doc.image.should be_an_instance_of(@uploader)
135
+ @doc.image.current_path.should == public_path('uploads/test.jpg')
136
+ end
137
+
138
+ it "saves the filename in the database" do
139
+ @doc.image = stub_file('test.jpg')
140
+ @doc.save
141
+ @doc[:image_filename].should == 'test.jpg'
142
+ end
143
+
144
+ context "when remove_image? is true" do
145
+
146
+ it "removes the image" do
147
+ @doc.image = stub_file('test.jpeg')
148
+ @doc.save
149
+ @doc.remove_image = true
150
+ @doc.save
151
+ @doc.image.should be_blank
152
+ @doc[:image_filename].should == ''
153
+ end
154
+
155
+ end
156
+
157
+ end
158
+
159
+ end
160
+
161
+ describe '#destroy' do
162
+
163
+ before do
164
+ @doc = @class.new
165
+ end
166
+
167
+ describe "when file assigned" do
168
+
169
+ it "removes the file from the filesystem" do
170
+ @doc.image = stub_file('test.jpeg')
171
+ @doc.save.should be_true
172
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
173
+ @doc.image.should be_an_instance_of(@uploader)
174
+ @doc.image.current_path.should == public_path('uploads/test.jpeg')
175
+ @doc.destroy
176
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
177
+ end
178
+
179
+ end
180
+
181
+ describe "when file is not assigned" do
182
+
183
+ it "deletes the instance of @class after save" do
184
+ @doc.save
185
+ @class.count.should eql(1)
186
+ @doc.destroy
187
+ end
188
+
189
+ it "deletes the instance of @class after save and then re-looking up the instance" do
190
+ # this fails with TypeError in 'CarrierWave::MongoMapper#destroy when file is not assigned deletes the instance of @class' can't modify frozen object
191
+ @doc.save
192
+ @class.count.should eql(1)
193
+ @doc = @class.first
194
+ @doc.destroy
195
+ end
196
+
197
+ end
198
+
199
+ end
200
+
201
+
202
+ end
@@ -0,0 +1,183 @@
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
+ @class.class_eval do
115
+ def validate
116
+ errors.add(:image, 'FAIL!')
117
+ end
118
+ end
119
+ # Turn off raising the exceptions on save
120
+ @event.raise_on_save_failure = false
121
+ end
122
+
123
+ it "should do nothing when a validation fails" do
124
+ @event.image = stub_file('test.jpeg')
125
+ @event.should_not be_valid
126
+ @event.save
127
+ @event.should be_new
128
+ @event.image.should be_an_instance_of(@uploader)
129
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
130
+ end
131
+ end
132
+
133
+ it "should assign the filename to the database" do
134
+ @event.image = stub_file('test.jpeg')
135
+ @event.save.should be_true
136
+ @event.reload
137
+ @event[:image].should == 'test.jpeg'
138
+ end
139
+
140
+ it "should remove the image if remove_image? returns true" do
141
+ @event.image = stub_file('test.jpeg')
142
+ @event.save
143
+ @event.remove_image = true
144
+ @event.save
145
+ @event.reload
146
+ @event.image.should be_blank
147
+ @event[:image].should == ''
148
+ end
149
+ end
150
+
151
+ describe 'with overriddent filename' do
152
+
153
+ describe '#save' do
154
+
155
+ before do
156
+ @uploader.class_eval do
157
+ def filename
158
+ model.name + File.extname(super)
159
+ end
160
+ end
161
+ @event.stub!(:name).and_return('jonas')
162
+ end
163
+
164
+ it "should copy the file to the upload directory when a file has been assigned" do
165
+ @event.image = stub_file('test.jpeg')
166
+ @event.save.should be_true
167
+ @event.image.should be_an_instance_of(@uploader)
168
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
169
+ end
170
+
171
+ it "should assign an overridden filename to the database" do
172
+ @event.image = stub_file('test.jpeg')
173
+ @event.save.should be_true
174
+ @event.reload
175
+ @event[:image].should == 'jonas.jpeg'
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::ImageScience do
6
+
7
+ before do
8
+ @klass = Class.new do
9
+ include CarrierWave::ImageScience
10
+ end
11
+ @instance = @klass.new
12
+ FileUtils.cp(file_path('landscape.jpg'), file_path('landscape_copy.jpg'))
13
+ @instance.stub(:current_path).and_return(file_path('landscape_copy.jpg'))
14
+ end
15
+
16
+ after do
17
+ FileUtils.rm(file_path('landscape_copy.jpg'))
18
+ end
19
+
20
+ describe '#resize_to_fill' do
21
+ it "should resize the image to exactly the given dimensions" do
22
+ @instance.resize_to_fill(200, 200)
23
+ @instance.should have_dimensions(200, 200)
24
+ end
25
+
26
+ it "should scale up the image if it smaller than the given dimensions" do
27
+ @instance.resize_to_fill(1000, 1000)
28
+ @instance.should have_dimensions(1000, 1000)
29
+ end
30
+ end
31
+
32
+ describe '#resize_to_fit' do
33
+ it "should resize the image to fit within the given dimensions" do
34
+ @instance.resize_to_fit(200, 200)
35
+ @instance.should have_dimensions(200, 150)
36
+ end
37
+
38
+ it "should scale up the image if it smaller than the given dimensions" do
39
+ @instance.resize_to_fit(1000, 1000)
40
+ @instance.should have_dimensions(1000, 750)
41
+ end
42
+ end
43
+
44
+ describe '#resize_to_limit' do
45
+ it "should resize the image to fit within the given dimensions" do
46
+ @instance.resize_to_limit(200, 200)
47
+ @instance.should have_dimensions(200, 150)
48
+ end
49
+
50
+ it "should not scale up the image if it smaller than the given dimensions" do
51
+ @instance.resize_to_limit(1000, 1000)
52
+ @instance.should have_dimensions(640, 480)
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::MiniMagick do
6
+
7
+ before do
8
+ @klass = Class.new do
9
+ include CarrierWave::MiniMagick
10
+ end
11
+ @instance = @klass.new
12
+ FileUtils.cp(file_path('landscape.jpg'), file_path('landscape_copy.jpg'))
13
+ @instance.stub(:current_path).and_return(file_path('landscape_copy.jpg'))
14
+ end
15
+
16
+ after do
17
+ FileUtils.rm(file_path('landscape_copy.jpg'))
18
+ end
19
+
20
+ describe "#convert" do
21
+ it "should convert from one format to another" do
22
+ @instance.convert('png')
23
+ img = ::MiniMagick::Image.from_file(@instance.current_path)
24
+ img['format'].should =~ /PNG/
25
+ end
26
+ end
27
+
28
+ describe '#resize_to_fill' do
29
+ it "should resize the image to exactly the given dimensions" do
30
+ @instance.resize_to_fill(200, 200)
31
+ @instance.should have_dimensions(200, 200)
32
+ end
33
+
34
+ it "should scale up the image if it smaller than the given dimensions" do
35
+ @instance.resize_to_fill(1000, 1000)
36
+ @instance.should have_dimensions(1000, 1000)
37
+ end
38
+ end
39
+
40
+ describe '#resize_and_pad' do
41
+ it "should resize the image to exactly the given dimensions" do
42
+ @instance.resize_and_pad(200, 200)
43
+ @instance.should have_dimensions(200, 200)
44
+ end
45
+
46
+ it "should scale up the image if it smaller than the given dimensions" do
47
+ @instance.resize_and_pad(1000, 1000)
48
+ @instance.should have_dimensions(1000, 1000)
49
+ end
50
+ end
51
+
52
+ describe '#resize_to_fit' do
53
+ it "should resize the image to fit within the given dimensions" do
54
+ @instance.resize_to_fit(200, 200)
55
+ @instance.should have_dimensions(200, 150)
56
+ end
57
+
58
+ it "should scale up the image if it smaller than the given dimensions" do
59
+ @instance.resize_to_fit(1000, 1000)
60
+ @instance.should have_dimensions(1000, 750)
61
+ end
62
+ end
63
+
64
+ describe '#resize_to_limit' do
65
+ it "should resize the image to fit within the given dimensions" do
66
+ @instance.resize_to_limit(200, 200)
67
+ @instance.should have_dimensions(200, 150)
68
+ end
69
+
70
+ it "should not scale up the image if it smaller than the given dimensions" do
71
+ @instance.resize_to_limit(1000, 1000)
72
+ @instance.should have_dimensions(640, 480)
73
+ end
74
+ end
75
+
76
+ end