durran-carrierwave 0.3.2.3 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/Generators +1 -1
  2. data/History.txt +39 -2
  3. data/Manifest.txt +19 -5
  4. data/README.rdoc +180 -55
  5. data/Rakefile +11 -4
  6. data/features/grid_fs_storage.feature +32 -0
  7. data/features/step_definitions/general_steps.rb +6 -1
  8. data/features/support/activerecord.rb +1 -1
  9. data/features/support/env.rb +3 -16
  10. data/lib/carrierwave.rb +19 -74
  11. data/lib/carrierwave/compatibility/paperclip.rb +2 -2
  12. data/lib/carrierwave/core_ext/inheritable_attributes.rb +3 -3
  13. data/lib/carrierwave/mount.rb +36 -27
  14. data/lib/carrierwave/orm/activerecord.rb +3 -3
  15. data/lib/carrierwave/orm/datamapper.rb +2 -2
  16. data/lib/carrierwave/orm/mongoid.rb +23 -0
  17. data/lib/carrierwave/orm/mongomapper.rb +1 -1
  18. data/lib/carrierwave/orm/sequel.rb +4 -16
  19. data/lib/carrierwave/processing/image_science.rb +54 -25
  20. data/lib/carrierwave/processing/mini_magick.rb +269 -0
  21. data/lib/carrierwave/processing/rmagick.rb +4 -6
  22. data/lib/carrierwave/sanitized_file.rb +7 -6
  23. data/lib/carrierwave/storage/abstract.rb +0 -2
  24. data/lib/carrierwave/storage/file.rb +3 -5
  25. data/lib/carrierwave/storage/grid_fs.rb +92 -0
  26. data/lib/carrierwave/storage/right_s3.rb +183 -0
  27. data/lib/carrierwave/storage/s3.rb +37 -69
  28. data/lib/carrierwave/test/matchers.rb +22 -8
  29. data/lib/carrierwave/uploader.rb +2 -2
  30. data/lib/carrierwave/uploader/cache.rb +21 -18
  31. data/lib/carrierwave/uploader/configuration.rb +122 -0
  32. data/lib/carrierwave/uploader/default_url.rb +19 -0
  33. data/lib/carrierwave/uploader/processing.rb +4 -2
  34. data/lib/carrierwave/uploader/remove.rb +0 -1
  35. data/lib/carrierwave/uploader/store.rb +1 -68
  36. data/lib/carrierwave/uploader/url.rb +1 -1
  37. data/lib/carrierwave/uploader/versions.rb +3 -4
  38. data/{lib/generators → merb_generators}/uploader_generator.rb +0 -0
  39. data/rails_generators/uploader/templates/uploader.rb +4 -4
  40. data/spec/compatibility/paperclip_spec.rb +11 -2
  41. data/spec/fixtures/landscape.jpg +0 -0
  42. data/spec/fixtures/portrait.jpg +0 -0
  43. data/spec/mount_spec.rb +0 -25
  44. data/spec/orm/datamapper_spec.rb +55 -48
  45. data/spec/orm/mongoid_spec.rb +206 -0
  46. data/spec/orm/mongomapper_spec.rb +19 -1
  47. data/spec/orm/sequel_spec.rb +3 -12
  48. data/spec/processing/image_science_spec.rb +56 -0
  49. data/spec/processing/mini_magick_spec.rb +76 -0
  50. data/spec/processing/rmagick_spec.rb +68 -0
  51. data/spec/sanitized_file_spec.rb +84 -74
  52. data/spec/spec_helper.rb +1 -3
  53. data/spec/storage/grid_fs_spec.rb +78 -0
  54. data/spec/storage/right_s3_spec.rb +75 -0
  55. data/spec/storage/s3_spec.rb +83 -0
  56. data/spec/uploader/cache_spec.rb +1 -13
  57. data/spec/uploader/configuration_spec.rb +105 -0
  58. data/spec/uploader/{default_path_spec.rb → default_url_spec.rb} +22 -5
  59. data/spec/uploader/paths_spec.rb +1 -1
  60. data/spec/uploader/processing_spec.rb +11 -0
  61. data/spec/uploader/store_spec.rb +21 -47
  62. data/spec/uploader/versions_spec.rb +0 -8
  63. metadata +105 -17
  64. data/LICENSE +0 -8
  65. data/carrierwave.gemspec +0 -57
  66. data/lib/carrierwave/uploader/default_path.rb +0 -23
  67. data/lib/carrierwave/uploader/paths.rb +0 -27
@@ -4,6 +4,15 @@ require File.dirname(__FILE__) + '/../spec_helper'
4
4
 
5
5
  require 'carrierwave/orm/activerecord'
6
6
 
7
+ module Rails
8
+ def self.root
9
+ File.expand_path(File.join('..'), File.dirname(__FILE__))
10
+ end
11
+ def self.env
12
+ "test"
13
+ end
14
+ end unless defined?(Rails)
15
+
7
16
  describe CarrierWave::Compatibility::Paperclip do
8
17
 
9
18
  before do
@@ -21,12 +30,12 @@ describe CarrierWave::Compatibility::Paperclip do
21
30
 
22
31
  describe '#store_path' do
23
32
  it "should mimics paperclip default" do
24
- @uploader.store_path("monkey.png").should == CarrierWave.config[:root] + "/public/system/monkeys/23/original/monkey.png"
33
+ @uploader.store_path("monkey.png").should == CarrierWave::Uploader::Base.root + "/system/monkeys/23/original/monkey.png"
25
34
  end
26
35
 
27
36
  it "should interpolate the root path" do
28
37
  @uploader.stub!(:paperclip_path).and_return(":rails_root/foo/bar")
29
- @uploader.store_path("monkey.png").should == CarrierWave.config[:root] + "/foo/bar"
38
+ @uploader.store_path("monkey.png").should == Rails.root + "/foo/bar"
30
39
  end
31
40
 
32
41
  it "should interpolate the attachment" do
@@ -31,7 +31,6 @@ describe CarrierWave::Mount do
31
31
  end
32
32
  end
33
33
 
34
- @instance.image_uploader.should be_an_instance_of(@uploader)
35
34
  @instance.image = stub_file('test.jpg')
36
35
  @instance.image.should be_an_instance_of(@uploader)
37
36
  end
@@ -39,34 +38,10 @@ describe CarrierWave::Mount do
39
38
  it "should inherit uploaders to subclasses" do
40
39
  @subclass = Class.new(@class)
41
40
  @subclass_instance = @subclass.new
42
- @subclass_instance.image_uploader.should be_an_instance_of(@uploader)
43
41
  @subclass_instance.image = stub_file('test.jpg')
44
42
  @subclass_instance.image.should be_an_instance_of(@uploader)
45
43
  end
46
44
 
47
- describe '#image_uploader' do
48
- it "should return the uploader" do
49
- @instance.image_uploader.should be_an_instance_of(@uploader)
50
- end
51
- end
52
-
53
- describe '#image_uploader=' do
54
- it "should set the uploader" do
55
- @my_uploader = @uploader.new
56
- @instance.image_uploader = @my_uploader
57
- @instance.image_uploader.should == @my_uploader
58
- end
59
-
60
- it "should use the set uploader" do
61
- @my_uploader = @uploader.new
62
- @my_uploader.store!(stub_file('test.jpg'))
63
- @instance.image_uploader = @my_uploader
64
- @instance.image_uploader.should == @my_uploader
65
- @instance.image.should == @my_uploader
66
- @instance.image.current_path.should == public_path('uploads/test.jpg')
67
- end
68
- end
69
-
70
45
  describe '#image' do
71
46
 
72
47
  it "should return a blank uploader when nothing has been assigned" do
@@ -2,47 +2,54 @@
2
2
 
3
3
  require File.dirname(__FILE__) + '/../spec_helper'
4
4
 
5
+ require 'dm-core'
6
+ require 'dm-validations'
5
7
  require 'carrierwave/orm/datamapper'
6
8
 
7
9
  DataMapper.setup(:default, 'sqlite3::memory:')
8
10
 
9
11
  describe CarrierWave::DataMapper do
10
-
12
+
11
13
  before do
12
14
  uploader = Class.new(CarrierWave::Uploader::Base)
13
-
15
+
14
16
  @class = Class.new
15
17
  @class.class_eval do
16
18
  include DataMapper::Resource
17
19
 
18
20
  storage_names[:default] = 'events'
19
-
20
- property :id, Integer, :key => true
21
- property :image, String
22
-
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
+
23
30
  mount_uploader :image, uploader
24
31
  end
25
-
32
+
26
33
  @class.auto_migrate!
27
-
34
+
28
35
  @uploader = uploader
29
-
36
+
30
37
  @event = @class.new
31
38
  end
32
-
39
+
33
40
  describe '#image' do
34
-
41
+
35
42
  it "should return blank uploader when nothing has been assigned" do
36
43
  @event.image.should be_blank
37
44
  end
38
-
45
+
39
46
  it "should return blank uploader when an empty string has been assigned" do
40
47
  repository(:default).adapter.query("INSERT INTO events (image) VALUES ('')")
41
48
  @event = @class.first
42
-
49
+
43
50
  @event.image.should be_blank
44
51
  end
45
-
52
+
46
53
  it "should retrieve a file from the storage if a value is stored in the database" do
47
54
  repository(:default).adapter.query("INSERT INTO events (image) VALUES ('test.jpg')")
48
55
  @event = @class.first
@@ -51,82 +58,64 @@ describe CarrierWave::DataMapper do
51
58
  @event.reload
52
59
  @event.image.should be_an_instance_of(@uploader)
53
60
  end
54
-
61
+
55
62
  it "should set the path to the store dir" do
56
63
  @event.attribute_set(:image, 'test.jpeg')
57
64
  @event.save
58
65
  @event.reload
59
66
  @event.image.current_path.should == public_path('uploads/test.jpeg')
60
67
  end
61
-
68
+
62
69
  end
63
-
70
+
64
71
  describe '#image=' do
65
-
72
+
66
73
  it "should cache a file" do
67
74
  @event.image = stub_file('test.jpeg')
68
75
  @event.image.should be_an_instance_of(@uploader)
69
76
  end
70
-
77
+
71
78
  it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
72
79
  @event.attribute_get(:image).should be_nil
73
80
  end
74
-
81
+
75
82
  it "should copy a file into into the cache directory" do
76
83
  @event.image = stub_file('test.jpeg')
77
84
  @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
78
85
  end
79
-
86
+
80
87
  it "should do nothing when nil is assigned" do
81
88
  @event.image = nil
82
89
  @event.image.should be_blank
83
90
  end
84
-
91
+
85
92
  it "should do nothing when an empty string is assigned" do
86
93
  @event.image = ''
87
94
  @event.image.should be_blank
88
95
  end
89
-
96
+
90
97
  end
91
-
98
+
92
99
  describe '#save' do
93
-
100
+
94
101
  it "should do nothing when no file has been assigned" do
95
102
  @event.save
96
103
  @event.image.should be_blank
97
104
  end
98
-
105
+
99
106
  it "should copy the file to the upload directory when a file has been assigned" do
100
107
  @event.image = stub_file('test.jpeg')
101
108
  @event.save
102
109
  @event.image.should be_an_instance_of(@uploader)
103
110
  @event.image.current_path.should == public_path('uploads/test.jpeg')
104
111
  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
-
112
+
115
113
  it "should assign the filename to the database" do
116
114
  @event.image = stub_file('test.jpeg')
117
115
  @event.save
118
116
  @event.reload
119
117
  @event.attribute_get(:image).should == 'test.jpeg'
120
118
  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
119
 
131
120
  it "should remove the image if remove_image? returns true" do
132
121
  @event.image = stub_file('test.jpeg')
@@ -138,14 +127,32 @@ describe CarrierWave::DataMapper do
138
127
  @event.attribute_get(:image).should == ''
139
128
  end
140
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
+
141
148
  end
142
149
 
143
150
  describe '#destroy' do
144
-
151
+
145
152
  it "should do nothing when no file has been assigned" do
146
153
  @event.destroy
147
154
  end
148
-
155
+
149
156
  it "should remove the file from the filesystem" do
150
157
  @event.image = stub_file('test.jpeg')
151
158
  @event.save.should be_true
@@ -157,5 +164,5 @@ describe CarrierWave::DataMapper do
157
164
  end
158
165
 
159
166
  end
160
-
167
+
161
168
  end
@@ -0,0 +1,206 @@
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
+ describe CarrierWave::Mongoid do
10
+
11
+ before do
12
+ uploader = Class.new(CarrierWave::Uploader::Base)
13
+
14
+ @class = Class.new
15
+ @class.class_eval do
16
+ include Mongoid::Document
17
+ mount_uploader :image, uploader
18
+ end
19
+
20
+ @uploader = uploader
21
+ end
22
+
23
+ after do
24
+ @class.collection.drop
25
+ end
26
+
27
+ describe '#image' do
28
+
29
+ context "when nothing is assigned" do
30
+
31
+ before do
32
+ @document = @class.new
33
+ end
34
+
35
+ it "returns a blank uploader" do
36
+ @document.image.should be_blank
37
+ end
38
+
39
+ end
40
+
41
+ context "when an empty string is assigned" do
42
+
43
+ before do
44
+ @document = @class.new(:image_filename => "")
45
+ @document.save
46
+ end
47
+
48
+ it "returns a blank uploader" do
49
+ @saved_doc = @class.find(:first)
50
+ @saved_doc.image.should be_blank
51
+ end
52
+
53
+ end
54
+
55
+ context "when a filename is saved in the database" do
56
+
57
+ before do
58
+ @document = @class.new(:image_filename => "test.jpg")
59
+ @document.save
60
+ @doc = @class.find(:first)
61
+ end
62
+
63
+ it "returns an uploader" do
64
+ @doc.image.should be_an_instance_of(@uploader)
65
+ end
66
+
67
+ it "sets the path to the store directory" do
68
+ @doc.image.current_path.should == public_path('uploads/test.jpg')
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+
75
+ describe '#image=' do
76
+
77
+ before do
78
+ @doc = @class.new
79
+ end
80
+
81
+ context "when nil is assigned" do
82
+
83
+ it "does not set the value" do
84
+ @doc.image = nil
85
+ @doc.image.should be_blank
86
+ end
87
+
88
+ end
89
+
90
+ context "when an empty string is assigned" do
91
+
92
+ it "does not set the value" do
93
+ @doc.image = ''
94
+ @doc.image.should be_blank
95
+ end
96
+
97
+ end
98
+
99
+ context "when a file is assigned" do
100
+
101
+ it "should cache a file" do
102
+ @doc.image = stub_file('test.jpeg')
103
+ @doc.image.should be_an_instance_of(@uploader)
104
+ end
105
+
106
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
107
+ @doc.image_filename.should be_nil
108
+ end
109
+
110
+ it "should copy a file into into the cache directory" do
111
+ @doc.image = stub_file('test.jpeg')
112
+ @doc.image.current_path.should =~ /^#{public_path('uploads\/tmp')}/
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ describe "#save" do
120
+
121
+ before do
122
+ @doc = @class.new
123
+ end
124
+
125
+ context "when no file is assigned" do
126
+
127
+ it "image is blank" do
128
+ @doc.save
129
+ @doc.image.should be_blank
130
+ end
131
+
132
+ end
133
+
134
+ context "when a file is assigned" do
135
+
136
+ it "copies the file to the upload directory" do
137
+ @doc.image = stub_file('test.jpg')
138
+ @doc.save
139
+ @doc.image.should be_an_instance_of(@uploader)
140
+ @doc.image.current_path.should == public_path('uploads/test.jpg')
141
+ end
142
+
143
+ it "saves the filename in the database" do
144
+ @doc.image = stub_file('test.jpg')
145
+ @doc.save
146
+ @doc.image_filename.should == 'test.jpg'
147
+ end
148
+
149
+ context "when remove_image? is true" do
150
+
151
+ it "removes the image" do
152
+ @doc.image = stub_file('test.jpeg')
153
+ @doc.save
154
+ @doc.remove_image = true
155
+ @doc.save
156
+ @doc.image.should be_blank
157
+ @doc.image_filename.should == ''
158
+ end
159
+
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+
166
+ describe '#destroy' do
167
+
168
+ before do
169
+ @doc = @class.new
170
+ end
171
+
172
+ describe "when file assigned" do
173
+
174
+ it "removes the file from the filesystem" do
175
+ @doc.image = stub_file('test.jpeg')
176
+ @doc.save.should be_true
177
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
178
+ @doc.image.should be_an_instance_of(@uploader)
179
+ @doc.image.current_path.should == public_path('uploads/test.jpeg')
180
+ @doc.destroy
181
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
182
+ end
183
+
184
+ end
185
+
186
+ describe "when file is not assigned" do
187
+
188
+ it "deletes the instance of @class after save" do
189
+ @doc.save
190
+ @class.count.should eql(1)
191
+ @doc.destroy
192
+ end
193
+
194
+ it "deletes the instance of @class after save and then re-looking up the instance" do
195
+ @doc.save
196
+ @class.count.should eql(1)
197
+ @doc = @class.first
198
+ @doc.destroy
199
+ end
200
+
201
+ end
202
+
203
+ end
204
+
205
+
206
+ end