carrierwave 0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of carrierwave might be problematic. Click here for more details.

@@ -0,0 +1,20 @@
1
+ module Merb
2
+ module Generators
3
+ class UploaderGenerator < NamedGenerator
4
+
5
+ def self.source_root
6
+ File.join(File.dirname(__FILE__), '..', '..', 'rails_generators', 'uploader', 'templates')
7
+ end
8
+
9
+ first_argument :name, :required => true, :desc => "The name of this uploader"
10
+
11
+ template :uploader do |t|
12
+ t.source = 'uploader.rb'
13
+ t.destination = "app/uploaders/#{file_name}_uploader.rb"
14
+ end
15
+ end
16
+
17
+ add :uploader, UploaderGenerator
18
+
19
+ end
20
+ end
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Generates a skeleton for a new uploader.
@@ -0,0 +1,33 @@
1
+ class <%= class_name %>Uploader < CarrierWave::Uploader
2
+
3
+ # Include RMagick or ImageScience support
4
+ # include CarrierWave::RMagick
5
+ # include CarrierWave::ImageScience
6
+
7
+ # Choose what kind of storage to use for this uploader
8
+ storage :file
9
+ # storage :s3
10
+
11
+ # Process files as they are uploaded.
12
+ # process :scale => [200, 300]
13
+ #
14
+ # def scale(width, height)
15
+ # # do something
16
+ # end
17
+
18
+ # Create different versions of your uploaded files
19
+ # version :thumb do
20
+ # process :scale => [50, 50]
21
+ # end
22
+
23
+ # Override the filename of the uploaded files
24
+ # def filename
25
+ # "something.jpg"
26
+ # end
27
+
28
+ # Override the directory where uploaded files will be stored
29
+ # def store_dir
30
+ # "something"
31
+ # end
32
+
33
+ end
@@ -0,0 +1,19 @@
1
+ class UploaderGenerator < Rails::Generator::NamedBase
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.directory 'app/uploaders'
6
+ m.template 'uploader.rb', "app/uploaders/#{name.underscore}_uploader.rb"
7
+ end
8
+ end
9
+
10
+ def class_name
11
+ name.camelize
12
+ end
13
+
14
+ protected
15
+
16
+ def banner
17
+ "Usage: #{$0} uploader UploaderName"
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ bork bork bork
@@ -0,0 +1 @@
1
+ this is stuff
@@ -0,0 +1 @@
1
+ this is stuff
@@ -0,0 +1,181 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe CarrierWave::Mount do
4
+
5
+ include SanitizedFileSpecHelper
6
+
7
+ after do
8
+ FileUtils.rm_rf(public_path)
9
+ end
10
+
11
+ describe '.mount_uploader' do
12
+
13
+ before do
14
+ @class = Class.new
15
+ @class.send(:extend, CarrierWave::Mount)
16
+
17
+ @uploader = Class.new(CarrierWave::Uploader)
18
+
19
+ @class.mount_uploader(:image, @uploader)
20
+ @instance = @class.new
21
+ end
22
+
23
+ describe '#image' do
24
+
25
+ it "should return nil when nothing has been assigned" do
26
+ @instance.should_receive(:read_uploader).with(:image).and_return(nil)
27
+ @instance.image.should be_nil
28
+ end
29
+
30
+ it "should return nil when an empty string has been assigned" do
31
+ @instance.should_receive(:read_uploader).with(:image).and_return('')
32
+ @instance.image.should be_nil
33
+ end
34
+
35
+ it "should retrieve a file from the storage if a value is stored in the database" do
36
+ @instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
37
+ @instance.image.should be_an_instance_of(@uploader)
38
+ end
39
+
40
+ it "should set the path to the store dir" do
41
+ @instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
42
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
43
+ end
44
+
45
+ end
46
+
47
+ describe '#image=' do
48
+
49
+ it "should cache a file" do
50
+ @instance.image = stub_file('test.jpg')
51
+ @instance.image.should be_an_instance_of(@uploader)
52
+ end
53
+
54
+ it "should copy a file into into the cache directory" do
55
+ @instance.image = stub_file('test.jpg')
56
+ @instance.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
57
+ end
58
+
59
+ it "should do nothing when nil is assigned" do
60
+ @instance.should_not_receive(:write_uploader)
61
+ @instance.image = nil
62
+ end
63
+
64
+ it "should do nothing when an empty string is assigned" do
65
+ @instance.should_not_receive(:write_uploader)
66
+ @instance.image = ''
67
+ end
68
+
69
+ end
70
+
71
+ describe '#image_cache' do
72
+
73
+ before do
74
+ @instance.stub!(:write_uploader)
75
+ @instance.stub!(:read_uploader).and_return(nil)
76
+ end
77
+
78
+ it "should return nil when nothing has been assigned" do
79
+ @instance.image_cache.should be_nil
80
+ end
81
+
82
+ it "should be nil when a file has been stored" do
83
+ @instance.image = stub_file('test.jpg')
84
+ @instance.image.store!
85
+ @instance.image_cache.should be_nil
86
+ end
87
+
88
+ it "should be the cache name when a file has been cached" do
89
+ @instance.image = stub_file('test.jpg')
90
+ @instance.image_cache.should =~ %r(^[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}/test\.jpg$)
91
+ end
92
+
93
+ end
94
+
95
+ describe '#image_cache=' do
96
+
97
+ before do
98
+ @instance.stub!(:write_uploader)
99
+ @instance.stub!(:read_uploader).and_return(nil)
100
+ CarrierWave::SanitizedFile.new(file_path('test.jpg')).copy_to(public_path('uploads/tmp/19990512-1202-123-1234/test.jpg'))
101
+ end
102
+
103
+ it "should do nothing when nil is assigned" do
104
+ @instance.image_cache = nil
105
+ @instance.image.should be_nil
106
+ end
107
+
108
+ it "should do nothing when an empty string is assigned" do
109
+ @instance.image_cache = ''
110
+ @instance.image.should be_nil
111
+ end
112
+
113
+ it "retrieve from cache when a cache name is assigned" do
114
+ @instance.image_cache = '19990512-1202-123-1234/test.jpg'
115
+ @instance.image.current_path.should == public_path('uploads/tmp/19990512-1202-123-1234/test.jpg')
116
+ end
117
+
118
+ it "should not write over a previously assigned file" do
119
+ @instance.image = stub_file('test.jpg')
120
+ @instance.image_cache = '19990512-1202-123-1234/monkey.jpg'
121
+ @instance.image.current_path.should =~ /test.jpg$/
122
+ end
123
+ end
124
+
125
+ describe '#store_image!' do
126
+
127
+ before do
128
+ @instance.stub!(:write_uploader)
129
+ @instance.stub!(:read_uploader).and_return(nil)
130
+ end
131
+
132
+ it "should do nothing when no file has been uploaded" do
133
+ @instance.store_image!
134
+ @instance.image.should be_nil
135
+ end
136
+
137
+ it "store an assigned file" do
138
+ @instance.image = stub_file('test.jpg')
139
+ @instance.store_image!
140
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ describe '#mount_uploader with a block' do
147
+
148
+ before do
149
+ @class = Class.new
150
+ @class.send(:extend, CarrierWave::Mount)
151
+ @class.mount_uploader(:image) do
152
+ def monkey
153
+ 'blah'
154
+ end
155
+ end
156
+ @instance = @class.new
157
+ end
158
+
159
+ describe '#image' do
160
+
161
+ before do
162
+ @instance.stub!(:read_uploader).and_return('test.jpg')
163
+ end
164
+
165
+ it "should return an instance of a subclass of CarrierWave::Uploader" do
166
+ @instance.image.should be_a(CarrierWave::Uploader)
167
+ end
168
+
169
+ it "should set the path to the store dir" do
170
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
171
+ end
172
+
173
+ it "should apply any custom modifications" do
174
+ @instance.image.monkey.should == "blah"
175
+ end
176
+
177
+ end
178
+
179
+ end
180
+
181
+ end
@@ -0,0 +1,168 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require 'carrierwave/orm/activerecord'
4
+
5
+ # change this if sqlite is unavailable
6
+ dbconfig = {
7
+ :adapter => 'sqlite3',
8
+ :database => ':memory:'
9
+ }
10
+
11
+ ActiveRecord::Base.establish_connection(dbconfig)
12
+ ActiveRecord::Migration.verbose = false
13
+
14
+ class TestMigration < ActiveRecord::Migration
15
+ def self.up
16
+ create_table :events, :force => true do |t|
17
+ t.column :image, :string
18
+ t.column :textfile, :string
19
+ end
20
+ end
21
+
22
+ def self.down
23
+ drop_table :events
24
+ end
25
+ end
26
+
27
+ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
28
+
29
+ describe CarrierWave::ActiveRecord do
30
+
31
+ include SanitizedFileSpecHelper
32
+
33
+ describe '.mount_uploader' do
34
+
35
+ before(:all) { TestMigration.up }
36
+ after(:all) { TestMigration.down }
37
+ after { Event.delete_all }
38
+
39
+ before do
40
+ @class = Class.new(ActiveRecord::Base)
41
+ @class.table_name = "events"
42
+ @uploader = Class.new(CarrierWave::Uploader)
43
+ @class.mount_uploader(:image, @uploader)
44
+ @event = @class.new
45
+ end
46
+
47
+ describe '#image' do
48
+
49
+ it "should return nil when nothing has been assigned" do
50
+ @event.image.should be_nil
51
+ end
52
+
53
+ it "should return nil when an empty string has been assigned" do
54
+ @event[:image] = ''
55
+ @event.save
56
+ @event.reload
57
+ @event.image.should be_nil
58
+ end
59
+
60
+ it "should retrieve a file from the storage if a value is stored in the database" do
61
+ @event[:image] = 'test.jpeg'
62
+ @event.save
63
+ @event.reload
64
+ @event.image.should be_an_instance_of(@uploader)
65
+ end
66
+
67
+ it "should set the path to the store dir" do
68
+ @event[:image] = 'test.jpeg'
69
+ @event.save
70
+ @event.reload
71
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
72
+ end
73
+
74
+ end
75
+
76
+ describe '#image=' do
77
+
78
+ it "should cache a file" do
79
+ @event.image = stub_file('test.jpeg')
80
+ @event.image.should be_an_instance_of(@uploader)
81
+ end
82
+
83
+ it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
84
+ @event[:image].should be_nil
85
+ end
86
+
87
+ it "should copy a file into into the cache directory" do
88
+ @event.image = stub_file('test.jpeg')
89
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
90
+ end
91
+
92
+ it "should do nothing when nil is assigned" do
93
+ @event.image = nil
94
+ @event.image.should be_nil
95
+ end
96
+
97
+ it "should do nothing when an empty string is assigned" do
98
+ @event.image = ''
99
+ @event.image.should be_nil
100
+ end
101
+
102
+ end
103
+
104
+ describe '#save' do
105
+
106
+ it "should do nothing when no file has been assigned" do
107
+ @event.save.should be_true
108
+ @event.image.should be_nil
109
+ end
110
+
111
+ it "should copy the file to the upload directory when a file has been assigned" do
112
+ @event.image = stub_file('test.jpeg')
113
+ @event.save.should be_true
114
+ @event.image.should be_an_instance_of(@uploader)
115
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
116
+ end
117
+
118
+ it "should do nothing when a validation fails" do
119
+ @class.validate { |r| r.errors.add :textfile, "FAIL!" }
120
+ @event.image = stub_file('test.jpeg')
121
+ @event.save.should be_false
122
+ @event.image.should be_an_instance_of(@uploader)
123
+ @event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
124
+ end
125
+
126
+ it "should assign the filename to the database" do
127
+ @event.image = stub_file('test.jpeg')
128
+ @event.save.should be_true
129
+ @event.reload
130
+ @event[:image].should == 'test.jpeg'
131
+ end
132
+
133
+ end
134
+
135
+ describe 'with overriddent filename' do
136
+
137
+ describe '#save' do
138
+
139
+ before do
140
+ @uploader.class_eval do
141
+ def filename
142
+ model.name + File.extname(super)
143
+ end
144
+ end
145
+ @event.stub!(:name).and_return('jonas')
146
+ end
147
+
148
+ it "should copy the file to the upload directory when a file has been assigned" do
149
+ @event.image = stub_file('test.jpeg')
150
+ @event.save.should be_true
151
+ @event.image.should be_an_instance_of(@uploader)
152
+ @event.image.current_path.should == public_path('uploads/jonas.jpeg')
153
+ end
154
+
155
+ it "should assign an overridden 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 == 'jonas.jpeg'
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+
168
+ end
@@ -0,0 +1,133 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require 'carrierwave/orm/datamapper'
4
+
5
+ DataMapper.setup(:default, 'sqlite3::memory:')
6
+
7
+ describe CarrierWave::DataMapper do
8
+
9
+ include SanitizedFileSpecHelper
10
+
11
+ before do
12
+ uploader = Class.new(CarrierWave::Uploader)
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 nil when nothing has been assigned" do
36
+ @event.image.should be_nil
37
+ end
38
+
39
+ it "should return nil 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_nil
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_nil
83
+ end
84
+
85
+ it "should do nothing when an empty string is assigned" do
86
+ @event.image = ''
87
+ @event.image.should be_nil
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_nil
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
+ end
132
+
133
+ end