jnicklas-carrierwave 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ require 'spec/rake/spectask'
9
9
  require 'cucumber/rake/task'
10
10
 
11
11
  NAME = "carrierwave"
12
- GEM_VERSION = "0.2.4"
12
+ GEM_VERSION = "0.3.0"
13
13
  AUTHOR = "Jonas Nicklas"
14
14
  EMAIL = "jonas.nicklas@gmail.com"
15
15
  HOMEPAGE = "http://www.example.com"
@@ -102,3 +102,5 @@ end
102
102
 
103
103
  desc 'Default: run unit tests.'
104
104
  task :default => 'spec'
105
+
106
+ task :superspec => [:spec, :features]
@@ -75,6 +75,8 @@ module CarrierWave
75
75
  # [image_integrity_error] Returns an error object if the last file to be assigned caused an integrty error
76
76
  # [image_processing_error] Returns an error object if the last file to be assigned caused a processing error
77
77
  #
78
+ # [write_image_identifier] Uses the write_uploader method to set the identifier.
79
+ #
78
80
  # === Parameters
79
81
  #
80
82
  # [column (Symbol)] the attribute to mount this uploader on
@@ -125,7 +127,19 @@ module CarrierWave
125
127
 
126
128
  include CarrierWave::Mount::Extension
127
129
 
130
+ # Make sure to write over accessors directly defined on the class.
131
+ # Simply super to the included module below.
128
132
  class_eval <<-RUBY, __FILE__, __LINE__+1
133
+ def #{column}; super; end
134
+ def #{column}=(new_file); super; end
135
+ RUBY
136
+
137
+ # Mixing this in as a Module instead of class_evaling directly, so we
138
+ # can maintain the ability to super to any of these methods from within
139
+ # the class.
140
+ mod = Module.new
141
+ include mod
142
+ mod.class_eval <<-RUBY, __FILE__, __LINE__+1
129
143
 
130
144
  def #{column}
131
145
  _mounter(:#{column}).uploader
@@ -186,6 +200,11 @@ module CarrierWave
186
200
  def #{column}_processing_error
187
201
  _mounter(:#{column}).processing_error
188
202
  end
203
+
204
+ def write_#{column}_identifier
205
+ _mounter(:#{column}).write_identifier
206
+ end
207
+
189
208
  RUBY
190
209
 
191
210
  end
@@ -225,11 +244,23 @@ module CarrierWave
225
244
  @options = record.class.uploader_options[column]
226
245
  end
227
246
 
247
+ def write_identifier
248
+ if remove?
249
+ record.write_uploader(serialization_column, '')
250
+ else
251
+ record.write_uploader(serialization_column, uploader.identifier)
252
+ end
253
+ end
254
+
255
+ def identifier
256
+ record.read_uploader(serialization_column)
257
+ end
258
+
228
259
  def uploader
229
260
  @uploader ||= record.class.uploaders[column].new(record, column)
230
- if @uploader.blank?
231
- identifier = record.read_uploader(serialization_column)
232
- @uploader.retrieve_from_store!(identifier) unless identifier.blank?
261
+
262
+ if @uploader.blank? and not identifier.blank?
263
+ @uploader.retrieve_from_store!(identifier)
233
264
  end
234
265
  return @uploader
235
266
  end
@@ -259,10 +290,8 @@ module CarrierWave
259
290
  unless uploader.blank?
260
291
  if remove?
261
292
  uploader.remove!
262
- record.write_uploader(serialization_column, '')
263
293
  else
264
294
  uploader.store!
265
- record.write_uploader(serialization_column, uploader.identifier)
266
295
  end
267
296
  end
268
297
  end
@@ -17,13 +17,12 @@ module CarrierWave
17
17
  validates_integrity_of column if uploader_options[column.to_sym][:validate_integrity]
18
18
  validates_processing_of column if uploader_options[column.to_sym][:validate_processing]
19
19
 
20
- after_create do |record|
20
+ after_save do |record|
21
21
  record.send("store_#{column}!")
22
- record.save
23
22
  end
24
23
 
25
- before_update do |record|
26
- record.send("store_#{column}!")
24
+ before_save do |record|
25
+ record.send("write_#{column}_identifier")
27
26
  end
28
27
 
29
28
  after_destroy do |record|
@@ -14,9 +14,17 @@ module CarrierWave
14
14
  alias_method :read_uploader, :attribute_get
15
15
  alias_method :write_uploader, :attribute_set
16
16
 
17
- before :save do
17
+ after :save do
18
18
  send("store_#{column}!")
19
19
  end
20
+
21
+ before :save do
22
+ send("write_#{column}_identifier")
23
+ end
24
+
25
+ after :destroy do
26
+ send("remove_#{column}!")
27
+ end
20
28
  end
21
29
 
22
30
  end # DataMapper
@@ -11,10 +11,14 @@ module CarrierWave
11
11
  alias_method :read_uploader, :[]
12
12
  alias_method :write_uploader, :[]=
13
13
 
14
- after_create do
14
+ after_save do
15
15
  send("store_#{column}!")
16
16
  end
17
17
 
18
+ before_save do
19
+ send("write_#{column}_identifier")
20
+ end
21
+
18
22
  before_destroy do
19
23
  send("remove_#{column}!")
20
24
  end
@@ -63,6 +63,37 @@ module CarrierWave
63
63
  #
64
64
  module RMagick
65
65
 
66
+ def self.included(base)
67
+ super
68
+ base.extend(ClassMethods)
69
+ end
70
+
71
+ module ClassMethods
72
+ def convert(format)
73
+ process :resize_to_limit => format
74
+ end
75
+
76
+ def resize_to_limit(width, height)
77
+ process :resize_to_limit => [width, height]
78
+ end
79
+
80
+ def resize_to_fit(width, height)
81
+ process :resize_to_fit => [width, height]
82
+ end
83
+
84
+ def resize_to_fill(width, height)
85
+ process :resize_to_fill => [width, height]
86
+ end
87
+
88
+ def resize_and_pad(width, height)
89
+ process :resize_to_fit => [width, height]
90
+ end
91
+
92
+ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::CenterGravity)
93
+ process :resize_and_pad => [width, height, background, gravity]
94
+ end
95
+ end
96
+
66
97
  ##
67
98
  # Changes the image encoding format to the given format
68
99
  #
@@ -3,107 +3,27 @@ module CarrierWave
3
3
 
4
4
  ##
5
5
  # This file serves mostly as a specification for Storage engines. There is no requirement
6
- # that storage engines must be a subclass of this class. However, any storage engine must
7
- # conform to the following interface:
8
- #
9
- # The storage engine must respond to store!, taking an uploader object and a
10
- # CarrierWave::SanitizedFile as parameters. This method should do something to store
11
- # the given file, and then return an object.
12
- #
13
- # The storage engine must respond to retrieve!, taking an uploader object and an identifier
14
- # as parameters. This method should do retrieve and then return an object.
15
- #
16
- # The objects returned by store! and retrieve! both *must* respond to +identifier+, taking
17
- # no arguments. Identifier is a string that uniquely identifies this file and can be used
18
- # to retrieve it later.
6
+ # that storage engines must be a subclass of this class.
19
7
  #
20
8
  class Abstract
21
9
 
22
- # Do something to destroy the file
23
- #
24
- # === Parameters
25
- #
26
- # [uploader (CarrierWave::Uploader)] an uploader object
27
- # [identifier (String)] uniquely identifies the file
28
- #
29
- # === Returns
30
- #
31
- # [bool] True if file was remove or false
32
- #
33
- def self.destroy!(uploader, identifier)
34
- false
10
+ attr_reader :uploader
11
+
12
+ def initialize(uploader)
13
+ @uploader = uploader
35
14
  end
36
15
 
37
- ##
38
- # Do setup specific for this storage engine
39
- #
40
16
  def self.setup!; end
41
-
42
- ##
43
- # Do something to store the file
44
- #
45
- # === Parameters
46
- #
47
- # [uploader (CarrierWave::Uploader)] an uploader object
48
- # [file (CarrierWave::SanitizedFile)] the file to store
49
- #
50
- # === Returns
51
- #
52
- # [#identifier] an object
53
- #
54
- def self.store!(uploader, file)
55
- self.new
56
- end
57
17
 
58
- # Do something to retrieve the file
59
- #
60
- # === Parameters
61
- #
62
- # [uploader (CarrierWave::Uploader)] an uploader object
63
- # [identifier (String)] uniquely identifies the file
64
- #
65
- # === Returns
66
- #
67
- # [#identifier] an object
68
- #
69
- def self.retrieve!(uploader, identifier)
70
- self.new
18
+ def identifier
19
+ uploader.filename
71
20
  end
72
21
 
73
- ##
74
- # Should return a String that uniquely identifies this file and can be used to retrieve it from
75
- # the same storage engine later on.
76
- #
77
- # This is OPTIONAL
78
- #
79
- # === Returns
80
- #
81
- # [String] path to the file
82
- #
83
- def identifier; end
84
-
85
- ##
86
- # Should return the url where the file is publically accessible. If this is not set, then
87
- # it is assumed that the url is the path relative to the public directory.
88
- #
89
- # This is OPTIONAL
90
- #
91
- # === Returns
92
- #
93
- # [String] file's url
94
- #
95
- def url; end
22
+ def store!
23
+ end
96
24
 
97
- ##
98
- # Should return the path where the file is corrently located. This is OPTIONAL.
99
- #
100
- # This is OPTIONAL
101
- #
102
- # === Returns
103
- #
104
- # [String] path to the file
105
- #
106
- def path; end
25
+ def retrieve!
26
+ end
107
27
 
108
28
  end # Abstract
109
29
  end # Storage
@@ -8,23 +8,18 @@ module CarrierWave
8
8
  #
9
9
  class File < Abstract
10
10
 
11
- def initialize(uploader)
12
- @uploader = uploader
13
- end
14
-
15
11
  ##
16
12
  # Move the file to the uploader's store path.
17
13
  #
18
14
  # === Parameters
19
15
  #
20
- # [uploader (CarrierWave::Uploader)] an uploader object
21
16
  # [file (CarrierWave::SanitizedFile)] the file to store
22
17
  #
23
18
  # === Returns
24
19
  #
25
20
  # [CarrierWave::SanitizedFile] a sanitized file
26
21
  #
27
- def self.store!(uploader, file)
22
+ def store!(file)
28
23
  path = ::File.join(uploader.store_path)
29
24
  path = ::File.expand_path(path, uploader.public)
30
25
  file.move_to(path, CarrierWave.config[:permissions])
@@ -36,14 +31,13 @@ module CarrierWave
36
31
  #
37
32
  # === Parameters
38
33
  #
39
- # [uploader (CarrierWave::Uploader)] an uploader object
40
34
  # [identifier (String)] the filename of the file
41
35
  #
42
36
  # === Returns
43
37
  #
44
38
  # [CarrierWave::SanitizedFile] a sanitized file
45
39
  #
46
- def self.retrieve!(uploader, identifier)
40
+ def retrieve!(identifier)
47
41
  path = ::File.join(uploader.store_path(identifier))
48
42
  path = ::File.expand_path(path, uploader.public)
49
43
  CarrierWave::SanitizedFile.new(path)
@@ -28,32 +28,86 @@ module CarrierWave
28
28
  # The default is :public_read, it should work in most cases.
29
29
  #
30
30
  class S3 < Abstract
31
-
32
- def initialize(path, identifier)
33
- @path = path
34
- @identifier = identifier
35
- end
36
-
37
- ##
38
- # Connect to Amazon S3
39
- #
40
- def self.setup!
41
- require 'aws/s3'
42
- AWS::S3::Base.establish_connection!(
43
- :access_key_id => CarrierWave.config[:s3][:access_key_id],
44
- :secret_access_key => CarrierWave.config[:s3][:secret_access_key]
45
- )
31
+
32
+ class File
33
+
34
+ def initialize(path, identifier)
35
+ @path = path
36
+ @identifier = identifier
37
+ end
38
+
39
+ ##
40
+ # Returns the current path of the file on S3
41
+ #
42
+ # === Returns
43
+ #
44
+ # [String] A path
45
+ #
46
+ def path
47
+ @path
48
+ end
49
+
50
+ ##
51
+ # Returns the filename on S3
52
+ #
53
+ # === Returns
54
+ #
55
+ # [String] path to the file
56
+ #
57
+ def identifier
58
+ @identifier
59
+ end
60
+
61
+ ##
62
+ # Reads the contents of the file from S3
63
+ #
64
+ # === Returns
65
+ #
66
+ # [String] contents of the file
67
+ #
68
+ def read
69
+ AWS::S3::S3Object.value @path, bucket
70
+ end
71
+
72
+ ##
73
+ # Remove the file from Amazon S3
74
+ #
75
+ def delete
76
+ AWS::S3::S3Object.delete @path, bucket
77
+ end
78
+
79
+ ##
80
+ # Returns the url on Amazon's S3 service
81
+ #
82
+ # === Returns
83
+ #
84
+ # [String] file's url
85
+ #
86
+ def url
87
+ ["http://s3.amazonaws.com", bucket, @path].compact.join('/')
88
+ end
89
+
90
+ private
91
+
92
+ def bucket
93
+ CarrierWave::Storage::S3.bucket
94
+ end
95
+
96
+ def access
97
+ CarrierWave::Storage::S3.access
98
+ end
99
+
46
100
  end
47
-
101
+
48
102
  ##
49
103
  # === Returns
50
104
  #
51
105
  # [String] the bucket set in the config options
52
- #
106
+ #
53
107
  def self.bucket
54
108
  CarrierWave.config[:s3][:bucket]
55
109
  end
56
-
110
+
57
111
  ##
58
112
  # === Returns
59
113
  #
@@ -62,91 +116,49 @@ module CarrierWave
62
116
  def self.access
63
117
  CarrierWave.config[:s3][:access]
64
118
  end
65
-
119
+
120
+ ##
121
+ # Connect to Amazon S3
122
+ #
123
+ def self.setup!
124
+ require 'aws/s3'
125
+ AWS::S3::Base.establish_connection!(
126
+ :access_key_id => CarrierWave.config[:s3][:access_key_id],
127
+ :secret_access_key => CarrierWave.config[:s3][:secret_access_key]
128
+ )
129
+ end
130
+
66
131
  ##
67
132
  # Store the file on S3
68
133
  #
69
134
  # === Parameters
70
135
  #
71
- # [uploader (CarrierWave::Uploader)] an uploader object
72
- # [file (CarrierWave::SanitizedFile)] the file to store
136
+ # [file (CarrierWave::Storage::S3::File)] the file to store
73
137
  #
74
138
  # === Returns
75
139
  #
76
140
  # [CarrierWave::Storage::S3] the stored file
77
141
  #
78
- def self.store!(uploader, file)
79
- AWS::S3::S3Object.store(::File.join(uploader.store_path), file.read, bucket, :access => access)
80
- self.new(uploader.store_dir, uploader.filename)
142
+ def store!(file)
143
+ AWS::S3::S3Object.store(::File.join(uploader.store_path), file.read, self.class.bucket, :access => self.class.access)
144
+ CarrierWave::Storage::S3::File.new(uploader.store_dir, uploader.filename)
81
145
  end
82
-
146
+
83
147
  # Do something to retrieve the file
84
148
  #
85
149
  # @param [CarrierWave::Uploader] uploader an uploader object
86
150
  # @param [String] identifier uniquely identifies the file
87
151
  #
88
- # [uploader (CarrierWave::Uploader)] an uploader object
89
152
  # [identifier (String)] uniquely identifies the file
90
153
  #
91
154
  # === Returns
92
155
  #
93
- # [CarrierWave::Storage::S3] the stored file
94
- #
95
- def self.retrieve!(uploader, identifier)
96
- self.new(uploader.store_path(identifier), identifier)
97
- end
98
-
99
- ##
100
- # Returns the current path of the file on S3
101
- #
102
- # === Returns
103
- #
104
- # [String] A path
105
- #
106
- def path
107
- @path
108
- end
109
-
110
- ##
111
- # Returns the filename on S3
112
- #
113
- # === Returns
114
- #
115
- # [String] path to the file
156
+ # [CarrierWave::Storage::S3::File] the stored file
116
157
  #
117
- def identifier
118
- @identifier
158
+ def retrieve!(identifier)
159
+ CarrierWave::Storage::S3::File.new(uploader.store_path(identifier), identifier)
119
160
  end
120
161
 
121
- ##
122
- # Reads the contents of the file from S3
123
- #
124
- # === Returns
125
- #
126
- # [String] contents of the file
127
- #
128
- def read
129
- AWS::S3::S3Object.value @path, self.class.bucket
130
- end
131
-
132
- ##
133
- # Remove the file from Amazon S3
134
- #
135
- def delete
136
- AWS::S3::S3Object.delete @path, self.class.bucket
137
- end
138
-
139
- ##
140
- # Returns the url on Amazon's S3 service
141
- #
142
- # === Returns
143
- #
144
- # [String] file's url
145
- #
146
- def url
147
- ["http://s3.amazonaws.com", self.class.bucket, @path].compact.join('/')
148
- end
149
-
150
162
  end # S3
151
163
  end # Storage
152
- end # CarrierWave
164
+ end # CarrierWave
@@ -30,7 +30,7 @@ module CarrierWave
30
30
  # [String] uniquely identifies a file
31
31
  #
32
32
  def identifier
33
- file.identifier if file.respond_to?(:identifier)
33
+ storage.identifier if storage.respond_to?(:identifier)
34
34
  end
35
35
 
36
36
  ##
@@ -120,7 +120,7 @@ module CarrierWave
120
120
  cache!(new_file) if new_file
121
121
  if @file and @cache_id
122
122
  with_callbacks(:store, new_file) do
123
- @file = storage.store!(self, @file)
123
+ @file = storage.store!(@file)
124
124
  @cache_id = nil
125
125
  end
126
126
  end
@@ -135,7 +135,7 @@ module CarrierWave
135
135
  #
136
136
  def retrieve_from_store!(identifier)
137
137
  with_callbacks(:retrieve_from_store, identifier) do
138
- @file = storage.retrieve!(self, identifier)
138
+ @file = storage.retrieve!(identifier)
139
139
  end
140
140
  end
141
141
 
@@ -146,7 +146,7 @@ module CarrierWave
146
146
  end
147
147
 
148
148
  def storage
149
- self.class.storage
149
+ @storage ||= self.class.storage.new(self)
150
150
  end
151
151
 
152
152
  end # Store
data/spec/mount_spec.rb CHANGED
@@ -19,21 +19,19 @@ describe CarrierWave::Mount do
19
19
  end
20
20
 
21
21
  it "should maintain the ability to super" do
22
- pending "I can't make this work with datamapper" do
23
- @class.class_eval do
24
- def image_uploader
25
- super
26
- end
27
-
28
- def image=(val)
29
- super
30
- end
22
+ @class.class_eval do
23
+ def image_uploader
24
+ super
31
25
  end
32
26
 
33
- @instance.image_uploader.should be_an_instance_of(@uploader)
34
- @instance.image = stub_file('test.jpg')
35
- @instance.image.should be_an_instance_of(@uploader)
27
+ def image=(val)
28
+ super
29
+ end
36
30
  end
31
+
32
+ @instance.image_uploader.should be_an_instance_of(@uploader)
33
+ @instance.image = stub_file('test.jpg')
34
+ @instance.image.should be_an_instance_of(@uploader)
37
35
  end
38
36
 
39
37
  describe '#image_uploader' do
@@ -74,12 +72,12 @@ describe CarrierWave::Mount do
74
72
  end
75
73
 
76
74
  it "should retrieve a file from the storage if a value is stored in the database" do
77
- @instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
75
+ @instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
78
76
  @instance.image.should be_an_instance_of(@uploader)
79
77
  end
80
78
 
81
79
  it "should set the path to the store dir" do
82
- @instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
80
+ @instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
83
81
  @instance.image.current_path.should == public_path('uploads/test.jpg')
84
82
  end
85
83
 
@@ -161,7 +159,7 @@ describe CarrierWave::Mount do
161
159
  end
162
160
 
163
161
  it "should get the url from a retrieved file" do
164
- @instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
162
+ @instance.should_receive(:read_uploader).at_least(:once).with(:image).and_return('test.jpg')
165
163
  @instance.image_url.should == '/uploads/test.jpg'
166
164
  end
167
165
 
@@ -250,14 +248,7 @@ describe CarrierWave::Mount do
250
248
  @instance.image.current_path.should == public_path('uploads/test.jpg')
251
249
  end
252
250
 
253
- it "write to the column" do
254
- @instance.should_receive(:write_uploader).with(:image, "test.jpg")
255
- @instance.image = stub_file('test.jpg')
256
- @instance.store_image!
257
- end
258
-
259
251
  it "should remove an uploaded file when remove_image? returns true" do
260
- @instance.should_receive(:write_uploader).with(:image, "")
261
252
  @instance.image = stub_file('test.jpg')
262
253
  path = @instance.image.current_path
263
254
  @instance.remove_image = true
@@ -371,6 +362,22 @@ describe CarrierWave::Mount do
371
362
  end
372
363
  end
373
364
 
365
+ describe '#write_image_identifier' do
366
+ it "should write to the column" do
367
+ @instance.should_receive(:write_uploader).with(:image, "test.jpg")
368
+ @instance.image = stub_file('test.jpg')
369
+ @instance.write_image_identifier
370
+ end
371
+
372
+ it "should remove from the column when remove_image is true" do
373
+ @instance.image = stub_file('test.jpg')
374
+ @instance.store_image!
375
+ @instance.remove_image = true
376
+ @instance.should_receive(:write_uploader).with(:image, "")
377
+ @instance.write_image_identifier
378
+ end
379
+ end
380
+
374
381
  end
375
382
 
376
383
  describe '#mount_uploader with a block' do
@@ -473,20 +480,18 @@ describe CarrierWave::Mount do
473
480
  end
474
481
 
475
482
  describe '#image' do
476
-
477
483
  it "should retrieve a file from the storage if a value is stored in the database" do
478
- @instance.should_receive(:read_uploader).with(:monkey).twice.and_return('test.jpg')
484
+ @instance.should_receive(:read_uploader).at_least(:once).with(:monkey).twice.and_return('test.jpg')
479
485
  @instance.image.should be_an_instance_of(@uploader)
480
486
  @instance.image.current_path.should == public_path('uploads/test.jpg')
481
487
  end
482
-
483
488
  end
484
489
 
485
- describe '#store_image!' do
490
+ describe '#write_image_identifier' do
486
491
  it "should write to the given column" do
487
492
  @instance.should_receive(:write_uploader).with(:monkey, "test.jpg")
488
493
  @instance.image = stub_file('test.jpg')
489
- @instance.store_image!
494
+ @instance.write_image_identifier
490
495
  end
491
496
 
492
497
  it "should remove from the given column when remove_image is true" do
@@ -494,7 +499,7 @@ describe CarrierWave::Mount do
494
499
  @instance.store_image!
495
500
  @instance.remove_image = true
496
501
  @instance.should_receive(:write_uploader).with(:monkey, "")
497
- @instance.store_image!
502
+ @instance.write_image_identifier
498
503
  end
499
504
  end
500
505
  end
@@ -101,14 +101,14 @@ describe CarrierWave::DataMapper do
101
101
  @event.image.current_path.should == public_path('uploads/test.jpeg')
102
102
  end
103
103
 
104
- it "should do nothing when a validation fails" do
105
- pending "how do we test with and without dm-validations?"
106
- @class.validate { |r| r.errors.add :textfile, "FAIL!" }
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/tmp')}/
111
- end
104
+ # it "should do nothing when a validation fails" do
105
+ # pending "how do we test with and without dm-validations?"
106
+ # @class.validate { |r| r.errors.add :textfile, "FAIL!" }
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/tmp')}/
111
+ # end
112
112
 
113
113
  it "should assign the filename to the database" do
114
114
  @event.image = stub_file('test.jpeg')
@@ -117,14 +117,14 @@ describe CarrierWave::DataMapper do
117
117
  @event.attribute_get(:image).should == 'test.jpeg'
118
118
  end
119
119
 
120
- it "should assign the filename before validation" do
121
- pending "how do we test with and without dm-validations?"
122
- @class.validate { |r| r.errors.add_to_base "FAIL!" if r[:image].nil? }
123
- @event.image = stub_file('test.jpeg')
124
- @event.save
125
- @event.reload
126
- @event.attribute_get(:image).should == 'test.jpeg'
127
- end
120
+ # it "should assign the filename before validation" do
121
+ # pending "how do we test with and without dm-validations?"
122
+ # @class.validate { |r| r.errors.add_to_base "FAIL!" if r[:image].nil? }
123
+ # @event.image = stub_file('test.jpeg')
124
+ # @event.save
125
+ # @event.reload
126
+ # @event.attribute_get(:image).should == 'test.jpeg'
127
+ # end
128
128
 
129
129
  it "should remove the image if remove_image? returns true" do
130
130
  @event.image = stub_file('test.jpeg')
@@ -137,5 +137,23 @@ describe CarrierWave::DataMapper do
137
137
  end
138
138
 
139
139
  end
140
+
141
+ describe '#destroy' do
142
+
143
+ it "should do nothing when no file has been assigned" do
144
+ @event.destroy
145
+ end
146
+
147
+ it "should remove the file from the filesystem" do
148
+ @event.image = stub_file('test.jpeg')
149
+ @event.save.should be_true
150
+ File.exist?(public_path('uploads/test.jpeg')).should be_true
151
+ @event.image.should be_an_instance_of(@uploader)
152
+ @event.image.current_path.should == public_path('uploads/test.jpeg')
153
+ @event.destroy
154
+ File.exist?(public_path('uploads/test.jpeg')).should be_false
155
+ end
156
+
157
+ end
140
158
 
141
159
  end
@@ -136,24 +136,20 @@ describe CarrierWave::Sequel do
136
136
  end
137
137
 
138
138
  it "should assign the filename to the database" do
139
- pending "Sequel support is currently broken" do
140
- @event.image = stub_file('test.jpeg')
141
- @event.save.should be_true
142
- @event.reload
143
- @event[:image].should == 'test.jpeg'
144
- end
139
+ @event.image = stub_file('test.jpeg')
140
+ @event.save.should be_true
141
+ @event.reload
142
+ @event[:image].should == 'test.jpeg'
145
143
  end
146
144
 
147
145
  it "should remove the image if remove_image? returns true" do
148
- pending "Sequel support is currently broken" do
149
- @event.image = stub_file('test.jpeg')
150
- @event.save
151
- @event.remove_image = true
152
- @event.save
153
- @event.reload
154
- @event.image.should be_blank
155
- @event[:image].should == ''
156
- end
146
+ @event.image = stub_file('test.jpeg')
147
+ @event.save
148
+ @event.remove_image = true
149
+ @event.save
150
+ @event.reload
151
+ @event.image.should be_blank
152
+ @event[:image].should == ''
157
153
  end
158
154
  end
159
155
 
@@ -178,12 +174,10 @@ describe CarrierWave::Sequel do
178
174
  end
179
175
 
180
176
  it "should assign an overridden filename to the database" do
181
- pending "Sequel support is currently broken" do
182
- @event.image = stub_file('test.jpeg')
183
- @event.save.should be_true
184
- @event.reload
185
- @event[:image].should == 'jonas.jpeg'
186
- end
177
+ @event.image = stub_file('test.jpeg')
178
+ @event.save.should be_true
179
+ @event.reload
180
+ @event[:image].should == 'jonas.jpeg'
187
181
  end
188
182
 
189
183
  end
@@ -21,7 +21,10 @@ describe CarrierWave::Uploader do
21
21
  @stored_file.stub!(:identifier).and_return('this-is-me')
22
22
  @stored_file.stub!(:delete)
23
23
 
24
- @uploader_class.storage.stub!(:store!).and_return(@stored_file)
24
+ @storage = mock('a storage engine')
25
+ @storage.stub!(:store!).and_return(@stored_file)
26
+
27
+ @uploader_class.storage.stub!(:new).and_return(@storage)
25
28
  @uploader.store!(@file)
26
29
  end
27
30
 
@@ -67,9 +67,12 @@ describe CarrierWave::Uploader do
67
67
  @stored_file = mock('a stored file')
68
68
  @stored_file.stub!(:path).and_return('/path/to/somewhere')
69
69
  @stored_file.stub!(:url).and_return('http://www.example.com')
70
- @stored_file.stub!(:identifier).and_return('this-is-me')
71
70
 
72
- @uploader_class.storage.stub!(:store!).and_return(@stored_file)
71
+ @storage = mock('a storage engine')
72
+ @storage.stub!(:store!).and_return(@stored_file)
73
+ @storage.stub!(:identifier).and_return('this-is-me')
74
+
75
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
73
76
  end
74
77
 
75
78
  it "should set the current path" do
@@ -105,7 +108,7 @@ describe CarrierWave::Uploader do
105
108
 
106
109
  it "should instruct the storage engine to store the file" do
107
110
  @uploader.cache!(@file)
108
- @uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
111
+ @storage.should_receive(:store!).with(@uploader.file).and_return(:monkey)
109
112
  @uploader.store!
110
113
  end
111
114
 
@@ -126,7 +129,7 @@ describe CarrierWave::Uploader do
126
129
 
127
130
  it "should not re-store a retrieved file" do
128
131
  @stored_file = mock('a stored file')
129
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
132
+ @storage.stub!(:retrieve!).and_return(@stored_file)
130
133
 
131
134
  @uploader_class.storage.should_not_receive(:store!)
132
135
  @uploader.retrieve_from_store!('monkey.txt')
@@ -139,9 +142,12 @@ describe CarrierWave::Uploader do
139
142
  @stored_file = mock('a stored file')
140
143
  @stored_file.stub!(:path).and_return('/path/to/somewhere')
141
144
  @stored_file.stub!(:url).and_return('http://www.example.com')
142
- @stored_file.stub!(:identifier).and_return('this-is-me')
143
145
 
144
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
146
+ @storage = mock('a storage engine')
147
+ @storage.stub!(:retrieve!).and_return(@stored_file)
148
+ @storage.stub!(:identifier).and_return('this-is-me')
149
+
150
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
145
151
  end
146
152
 
147
153
  it "should set the current path" do
@@ -165,7 +171,7 @@ describe CarrierWave::Uploader do
165
171
  end
166
172
 
167
173
  it "should instruct the storage engine to retrieve the file and store the result" do
168
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
174
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
169
175
  @uploader.retrieve_from_store!('monkey.txt')
170
176
  @uploader.file.should == @stored_file
171
177
  end
@@ -194,7 +200,10 @@ describe CarrierWave::Uploader do
194
200
  @stored_file.stub!(:path).and_return('/path/to/somewhere')
195
201
  @stored_file.stub!(:url).and_return('http://www.example.com')
196
202
 
197
- @uploader_class.storage.stub!(:store!).and_return(@stored_file)
203
+ @storage = mock('a storage engine')
204
+ @storage.stub!(:store!).and_return(@stored_file)
205
+
206
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
198
207
  end
199
208
 
200
209
  after do
@@ -230,7 +239,10 @@ describe CarrierWave::Uploader do
230
239
  @stored_file.stub!(:path).and_return('/path/to/somewhere')
231
240
  @stored_file.stub!(:url).and_return('http://www.example.com')
232
241
 
233
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
242
+ @storage = mock('a storage engine')
243
+ @storage.stub!(:retrieve!).and_return(@stored_file)
244
+
245
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
234
246
  end
235
247
 
236
248
  it "should set the current path" do
@@ -244,7 +256,7 @@ describe CarrierWave::Uploader do
244
256
  end
245
257
 
246
258
  it "should pass the identifier to the storage engine" do
247
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
259
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
248
260
  @uploader.retrieve_from_store!('monkey.txt')
249
261
  @uploader.file.should == @stored_file
250
262
  end
@@ -21,12 +21,12 @@ describe CarrierWave::Uploader do
21
21
  end
22
22
 
23
23
  it "should raise ArgumentError when version doesn't exist" do
24
- lambda { @uploader.url(:thumb) }.should raise_error ArgumentError
24
+ lambda { @uploader.url(:thumb) }.should raise_error(ArgumentError)
25
25
  end
26
26
 
27
27
  it "should not raise ArgumentError when versions version exists" do
28
28
  @uploader_class.version(:thumb)
29
- lambda { @uploader.url(:thumb) }.should_not raise_error ArgumentError
29
+ lambda { @uploader.url(:thumb) }.should_not raise_error(ArgumentError)
30
30
  end
31
31
 
32
32
  it "should get the directory relative to public, prepending a slash" do
@@ -157,8 +157,14 @@ describe CarrierWave::Uploader do
157
157
  @thumb_stored_file.stub!(:path).and_return('/path/to/somewhere/thumb')
158
158
  @thumb_stored_file.stub!(:url).and_return('http://www.example.com/thumb')
159
159
 
160
- @uploader_class.storage.stub!(:store!).and_return(@base_stored_file)
161
- @uploader_class.version(:thumb).storage.stub!(:store!).and_return(@thumb_stored_file)
160
+ @storage = mock('a storage engine')
161
+ @storage.stub!(:store!).and_return(@base_stored_file)
162
+
163
+ @thumb_storage = mock('a storage engine for thumbnails')
164
+ @thumb_storage.stub!(:store!).and_return(@thumb_stored_file)
165
+
166
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
167
+ @uploader_class.version(:thumb).storage.stub!(:new).with(@uploader.thumb).and_return(@thumb_storage)
162
168
  end
163
169
 
164
170
  after do
@@ -186,8 +192,8 @@ describe CarrierWave::Uploader do
186
192
 
187
193
  it "should instruct the storage engine to store the file and its version" do
188
194
  @uploader.cache!(@file)
189
- @uploader_class.storage.should_receive(:store!).with(@uploader, @uploader.file).and_return(:monkey)
190
- @uploader_class.version(:thumb).storage.should_receive(:store!).with(@uploader.thumb, @uploader.thumb.file).and_return(:gorilla)
195
+ @storage.should_receive(:store!).with(@uploader.file).and_return(:monkey)
196
+ @thumb_storage.should_receive(:store!).with(@uploader.thumb.file).and_return(:gorilla)
191
197
  @uploader.store!
192
198
  end
193
199
 
@@ -203,8 +209,14 @@ describe CarrierWave::Uploader do
203
209
  @base_stored_file = mock('a stored file')
204
210
  @thumb_stored_file = mock('a thumb version of a stored file')
205
211
 
206
- @uploader_class.storage.stub!(:store!).and_return(@base_stored_file)
207
- @uploader_class.version(:thumb).storage.stub!(:store!).and_return(@thumb_stored_file)
212
+ @storage = mock('a storage engine')
213
+ @storage.stub!(:store!).and_return(@base_stored_file)
214
+
215
+ @thumb_storage = mock('a storage engine for thumbnails')
216
+ @thumb_storage.stub!(:store!).and_return(@thumb_stored_file)
217
+
218
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
219
+ @uploader_class.version(:thumb).storage.stub!(:new).with(@uploader.thumb).and_return(@thumb_storage)
208
220
 
209
221
  @base_stored_file.stub!(:delete)
210
222
  @thumb_stored_file.stub!(:delete)
@@ -239,27 +251,47 @@ describe CarrierWave::Uploader do
239
251
 
240
252
  describe '#retrieve_from_store!' do
241
253
  before do
242
- @stored_file = mock('a stored file')
243
- @stored_file.stub!(:path).and_return('/path/to/somewhere')
244
- @stored_file.stub!(:url).and_return('http://www.example.com')
254
+ @uploader_class.storage = mock_storage('base')
255
+ @uploader_class.version(:thumb).storage = mock_storage('thumb')
256
+
257
+ @file = File.open(file_path('test.jpg'))
258
+
259
+ @base_stored_file = mock('a stored file')
260
+ @base_stored_file.stub!(:path).and_return('/path/to/somewhere')
261
+ @base_stored_file.stub!(:url).and_return('http://www.example.com')
262
+
263
+ @thumb_stored_file = mock('a thumb version of a stored file')
264
+ @thumb_stored_file.stub!(:path).and_return('/path/to/somewhere/thumb')
265
+ @thumb_stored_file.stub!(:url).and_return('http://www.example.com/thumb')
266
+
267
+ @storage = mock('a storage engine')
268
+ @storage.stub!(:retrieve!).and_return(@base_stored_file)
269
+
270
+ @thumb_storage = mock('a storage engine for thumbnails')
271
+ @thumb_storage.stub!(:retrieve!).and_return(@thumb_stored_file)
245
272
 
246
- @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
273
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
274
+ @uploader_class.version(:thumb).storage.stub!(:new).with(@uploader.thumb).and_return(@thumb_storage)
247
275
  end
248
276
 
249
277
  it "should set the current path" do
250
278
  @uploader.retrieve_from_store!('monkey.txt')
251
279
  @uploader.current_path.should == '/path/to/somewhere'
280
+ @uploader.thumb.current_path.should == '/path/to/somewhere/thumb'
252
281
  end
253
282
 
254
283
  it "should set the url" do
255
284
  @uploader.retrieve_from_store!('monkey.txt')
256
285
  @uploader.url.should == 'http://www.example.com'
286
+ @uploader.thumb.url.should == 'http://www.example.com/thumb'
257
287
  end
258
288
 
259
289
  it "should pass the identifier to the storage engine" do
260
- @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
290
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@base_stored_file)
291
+ @thumb_storage.should_receive(:retrieve!).with('monkey.txt').and_return(@thumb_stored_file)
261
292
  @uploader.retrieve_from_store!('monkey.txt')
262
- @uploader.file.should == @stored_file
293
+ @uploader.file.should == @base_stored_file
294
+ @uploader.thumb.file.should == @thumb_stored_file
263
295
  end
264
296
 
265
297
  it "should not set the filename" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jnicklas-carrierwave
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-11 00:00:00 -07:00
12
+ date: 2009-06-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15