jnicklas-carrierwave 0.3.2.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/History.txt +68 -0
  2. data/Manifest.txt +89 -0
  3. data/README.rdoc +32 -4
  4. data/Rakefile +5 -2
  5. data/carrierwave.gemspec +63 -0
  6. data/cucumber.yml +2 -0
  7. data/features/caching.feature +28 -0
  8. data/features/file_storage.feature +37 -0
  9. data/features/file_storage_overridden_filename.feature +38 -0
  10. data/features/file_storage_overridden_store_dir.feature +38 -0
  11. data/features/file_storage_reversing_processor.feature +43 -0
  12. data/features/fixtures/bork.txt +1 -0
  13. data/features/fixtures/monkey.txt +1 -0
  14. data/features/mount_activerecord.feature +46 -0
  15. data/features/mount_datamapper.feature +46 -0
  16. data/features/step_definitions/activerecord_steps.rb +22 -0
  17. data/features/step_definitions/caching_steps.rb +14 -0
  18. data/features/step_definitions/datamapper_steps.rb +29 -0
  19. data/features/step_definitions/file_steps.rb +42 -0
  20. data/features/step_definitions/general_steps.rb +80 -0
  21. data/features/step_definitions/mount_steps.rb +19 -0
  22. data/features/step_definitions/store_steps.rb +18 -0
  23. data/features/support/activerecord.rb +30 -0
  24. data/features/support/datamapper.rb +7 -0
  25. data/features/support/env.rb +35 -0
  26. data/features/versions_basics.feature +50 -0
  27. data/features/versions_nested_versions.feature +70 -0
  28. data/features/versions_overridden_filename.feature +51 -0
  29. data/features/versions_overriden_store_dir.feature +41 -0
  30. data/lib/carrierwave.rb +6 -1
  31. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  32. data/script/console +10 -0
  33. data/script/destroy +14 -0
  34. data/script/generate +14 -0
  35. data/spec/orm/mongomapper_spec.rb +184 -0
  36. metadata +153 -33
  37. data/LICENSE +0 -20
@@ -0,0 +1,50 @@
1
+ Feature: uploader with file storage and versions
2
+ In order to be awesome
3
+ As a developer using CarrierWave
4
+ I want to upload files to the filesystem
5
+
6
+ Background:
7
+ Given an uploader class that uses the 'file' storage
8
+ And that the uploader class has a version named 'thumb'
9
+ And an instance of that class
10
+
11
+ Scenario: store a file
12
+ When I store the file 'fixtures/bork.txt'
13
+ Then there should be a file at 'public/uploads/bork.txt'
14
+ Then there should be a file at 'public/uploads/thumb_bork.txt'
15
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
16
+ And the file at 'public/uploads/thumb_bork.txt' should be identical to the file at 'fixtures/bork.txt'
17
+ And the uploader should have the url '/uploads/bork.txt'
18
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
19
+
20
+ Scenario: cache a file and then store it
21
+ When I cache the file 'fixtures/bork.txt'
22
+ Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
23
+ Then there should be a file called 'thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
24
+ And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
25
+ And there should not be a file at 'public/uploads/bork.txt'
26
+ And there should not be a file at 'public/uploads/thumb_bork.txt'
27
+ When I store the file
28
+ Then there should be a file at 'public/uploads/bork.txt'
29
+ And there should be a file at 'public/uploads/thumb_bork.txt'
30
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
31
+ And the file at 'public/uploads/thumb_bork.txt' should be identical to the file at 'fixtures/bork.txt'
32
+ And the uploader should have the url '/uploads/bork.txt'
33
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
34
+
35
+ Scenario: retrieving a file from cache then storing
36
+ Given the file 'fixtures/bork.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/bork.txt'
37
+ Given the file 'fixtures/monkey.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/thumb_bork.txt'
38
+ When I retrieve the cache name '20090212-2343-8336-0348/bork.txt' from the cache
39
+ And I store the file
40
+ Then there should be a file at 'public/uploads/bork.txt'
41
+ Then there should be a file at 'public/uploads/thumb_bork.txt'
42
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
43
+ And the file at 'public/uploads/thumb_bork.txt' should be identical to the file at 'fixtures/monkey.txt'
44
+
45
+ Scenario: retrieving a file from store
46
+ Given the file 'fixtures/bork.txt' is stored at 'public/uploads/bork.txt'
47
+ Given the file 'fixtures/monkey.txt' is stored at 'public/uploads/thumb_bork.txt'
48
+ When I retrieve the file 'bork.txt' from the store
49
+ Then the uploader should have the url '/uploads/bork.txt'
50
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
@@ -0,0 +1,70 @@
1
+ Feature: uploader with nested versions
2
+ In order to optimize performance for processing
3
+ As a developer using CarrierWave
4
+ I want to set nested versions
5
+
6
+ Background:
7
+ Given an uploader class that uses the 'file' storage
8
+ And that the uploader class has a version named 'thumb'
9
+ And yo dawg, I put a version called 'mini' in your version called 'thumb'
10
+ And yo dawg, I put a version called 'micro' in your version called 'thumb'
11
+ And an instance of that class
12
+
13
+ Scenario: store a file
14
+ When I store the file 'fixtures/bork.txt'
15
+ Then there should be a file at 'public/uploads/bork.txt'
16
+ Then there should be a file at 'public/uploads/thumb_bork.txt'
17
+ Then there should be a file at 'public/uploads/thumb_mini_bork.txt'
18
+ Then there should be a file at 'public/uploads/thumb_micro_bork.txt'
19
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
20
+ And the file at 'public/uploads/thumb_bork.txt' should be identical to the file at 'fixtures/bork.txt'
21
+ And the file at 'public/uploads/thumb_mini_bork.txt' should be identical to the file at 'fixtures/bork.txt'
22
+ And the file at 'public/uploads/thumb_micro_bork.txt' should be identical to the file at 'fixtures/bork.txt'
23
+ And the uploader should have the url '/uploads/bork.txt'
24
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
25
+ And the uploader's nested version 'mini' nested in 'thumb' should have the url '/uploads/thumb_mini_bork.txt'
26
+ And the uploader's nested version 'micro' nested in 'thumb' should have the url '/uploads/thumb_micro_bork.txt'
27
+
28
+ Scenario: cache a file and then store it
29
+ When I cache the file 'fixtures/bork.txt'
30
+ Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
31
+ Then there should be a file called 'thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
32
+ And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
33
+ And there should not be a file at 'public/uploads/bork.txt'
34
+ And there should not be a file at 'public/uploads/thumb_bork.txt'
35
+ When I store the file
36
+ Then there should be a file at 'public/uploads/bork.txt'
37
+ And there should be a file at 'public/uploads/thumb_bork.txt'
38
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
39
+ And the file at 'public/uploads/thumb_bork.txt' should be identical to the file at 'fixtures/bork.txt'
40
+ And the uploader should have the url '/uploads/bork.txt'
41
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
42
+ And the uploader's nested version 'mini' nested in 'thumb' should have the url '/uploads/thumb_mini_bork.txt'
43
+ And the uploader's nested version 'micro' nested in 'thumb' should have the url '/uploads/thumb_micro_bork.txt'
44
+
45
+ Scenario: retrieving a file from cache then storing
46
+ Given the file 'fixtures/bork.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/bork.txt'
47
+ Given the file 'fixtures/monkey.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/thumb_bork.txt'
48
+ Given the file 'fixtures/bork.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/thumb_mini_bork.txt'
49
+ Given the file 'fixtures/monkey.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/thumb_micro_bork.txt'
50
+ When I retrieve the cache name '20090212-2343-8336-0348/bork.txt' from the cache
51
+ And I store the file
52
+ Then there should be a file at 'public/uploads/bork.txt'
53
+ Then there should be a file at 'public/uploads/thumb_bork.txt'
54
+ Then there should be a file at 'public/uploads/thumb_mini_bork.txt'
55
+ Then there should be a file at 'public/uploads/thumb_micro_bork.txt'
56
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
57
+ And the file at 'public/uploads/thumb_bork.txt' should be identical to the file at 'fixtures/monkey.txt'
58
+ And the file at 'public/uploads/thumb_mini_bork.txt' should be identical to the file at 'fixtures/bork.txt'
59
+ And the file at 'public/uploads/thumb_micro_bork.txt' should be identical to the file at 'fixtures/monkey.txt'
60
+
61
+ Scenario: retrieving a file from store
62
+ Given the file 'fixtures/bork.txt' is stored at 'public/uploads/bork.txt'
63
+ Given the file 'fixtures/monkey.txt' is stored at 'public/uploads/thumb_bork.txt'
64
+ Given the file 'fixtures/monkey.txt' is stored at 'public/uploads/thumb_mini_bork.txt'
65
+ Given the file 'fixtures/monkey.txt' is stored at 'public/uploads/thumb_micro_bork.txt'
66
+ When I retrieve the file 'bork.txt' from the store
67
+ Then the uploader should have the url '/uploads/bork.txt'
68
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
69
+ And the uploader's nested version 'mini' nested in 'thumb' should have the url '/uploads/thumb_mini_bork.txt'
70
+ And the uploader's nested version 'micro' nested in 'thumb' should have the url '/uploads/thumb_micro_bork.txt'
@@ -0,0 +1,51 @@
1
+ Feature: uploader with file storage and overriden filename
2
+ In order to customize the filaname of uploaded files
3
+ As a developer using CarrierWave
4
+ I want to upload files to the filesystem with an overriden filename and different verions
5
+
6
+ Background:
7
+ Given an uploader class that uses the 'file' storage
8
+ And that the uploader class has a version named 'thumb'
9
+ And that the uploader has the filename overridden to 'grark.png'
10
+ And an instance of that class
11
+
12
+ Scenario: store a file
13
+ When I store the file 'fixtures/bork.txt'
14
+ Then there should be a file at 'public/uploads/grark.png'
15
+ Then there should be a file at 'public/uploads/thumb_grark.png'
16
+ And the file at 'public/uploads/grark.png' should be identical to the file at 'fixtures/bork.txt'
17
+ And the file at 'public/uploads/thumb_grark.png' should be identical to the file at 'fixtures/bork.txt'
18
+ And the uploader should have the url '/uploads/grark.png'
19
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_grark.png'
20
+
21
+ Scenario: cache a file and then store it
22
+ When I cache the file 'fixtures/bork.txt'
23
+ Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
24
+ Then there should be a file called 'thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
25
+ And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
26
+ And there should not be a file at 'public/uploads/grark.png'
27
+ And there should not be a file at 'public/uploads/thumb_grark.png'
28
+ When I store the file
29
+ Then there should be a file at 'public/uploads/grark.png'
30
+ And there should be a file at 'public/uploads/thumb_grark.png'
31
+ And the file at 'public/uploads/grark.png' should be identical to the file at 'fixtures/bork.txt'
32
+ And the file at 'public/uploads/thumb_grark.png' should be identical to the file at 'fixtures/bork.txt'
33
+ And the uploader should have the url '/uploads/grark.png'
34
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_grark.png'
35
+
36
+ Scenario: retrieving a file from cache then storing
37
+ Given the file 'fixtures/bork.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/bork.txt'
38
+ Given the file 'fixtures/monkey.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/thumb_bork.txt'
39
+ When I retrieve the cache name '20090212-2343-8336-0348/bork.txt' from the cache
40
+ And I store the file
41
+ Then there should be a file at 'public/uploads/grark.png'
42
+ Then there should be a file at 'public/uploads/thumb_grark.png'
43
+ And the file at 'public/uploads/grark.png' should be identical to the file at 'fixtures/bork.txt'
44
+ And the file at 'public/uploads/thumb_grark.png' should be identical to the file at 'fixtures/monkey.txt'
45
+
46
+ Scenario: retrieving a file from store
47
+ Given the file 'fixtures/bork.txt' is stored at 'public/uploads/bork.txt'
48
+ Given the file 'fixtures/monkey.txt' is stored at 'public/uploads/thumb_bork.txt'
49
+ When I retrieve the file 'bork.txt' from the store
50
+ Then the uploader should have the url '/uploads/bork.txt'
51
+ And the uploader's version 'thumb' should have the url '/uploads/thumb_bork.txt'
@@ -0,0 +1,41 @@
1
+ Feature: uploader with file storage and versions with overridden store dir
2
+ In order to be awesome
3
+ As a developer using CarrierWave
4
+ I want to upload files to the filesystem
5
+
6
+ Background:
7
+ Given an uploader class that uses the 'file' storage
8
+ And that the uploader class has a version named 'thumb'
9
+ And that the version 'thumb' has the store_dir overridden to 'public/monkey/llama'
10
+ And an instance of that class
11
+
12
+ Scenario: store a file
13
+ When I store the file 'fixtures/bork.txt'
14
+ Then there should be a file at 'public/uploads/bork.txt'
15
+ Then there should be a file at 'public/monkey/llama/thumb_bork.txt'
16
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
17
+ And the file at 'public/monkey/llama/thumb_bork.txt' should be identical to the file at 'fixtures/bork.txt'
18
+
19
+ Scenario: cache a file and then store it
20
+ When I cache the file 'fixtures/bork.txt'
21
+ Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
22
+ Then there should be a file called 'thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
23
+ And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
24
+ And the file called 'thumb_bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
25
+ And there should not be a file at 'public/uploads/bork.txt'
26
+ And there should not be a file at 'public/monkey/llama/thumb_bork.txt'
27
+ When I store the file
28
+ Then there should be a file at 'public/uploads/bork.txt'
29
+ Then there should be a file at 'public/monkey/llama/thumb_bork.txt'
30
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
31
+ And the file at 'public/monkey/llama/thumb_bork.txt' should be identical to the file at 'fixtures/bork.txt'
32
+
33
+ Scenario: retrieving a file from cache then storing
34
+ Given the file 'fixtures/bork.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/bork.txt'
35
+ Given the file 'fixtures/monkey.txt' is cached file at 'public/uploads/tmp/20090212-2343-8336-0348/thumb_bork.txt'
36
+ When I retrieve the cache name '20090212-2343-8336-0348/bork.txt' from the cache
37
+ And I store the file
38
+ Then there should be a file at 'public/uploads/bork.txt'
39
+ Then there should be a file at 'public/monkey/llama/thumb_bork.txt'
40
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
41
+ And the file at 'public/monkey/llama/thumb_bork.txt' should be identical to the file at 'fixtures/monkey.txt'
@@ -7,7 +7,7 @@ require 'carrierwave/core_ext/inheritable_attributes'
7
7
 
8
8
  module CarrierWave
9
9
 
10
- VERSION = "0.3.2"
10
+ VERSION = "0.3.3"
11
11
 
12
12
  class << self
13
13
  attr_accessor :config, :logger
@@ -138,3 +138,8 @@ elsif defined?(Sinatra)
138
138
  CarrierWave.config[:public] = Sinatra::Application.public
139
139
 
140
140
  end
141
+
142
+ # MongoMapper is framework agnostic so we could need this in any environment.
143
+ if defined?(MongoMapper)
144
+ require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongomapper")
145
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'mongomapper'
3
+
4
+ module CarrierWave
5
+ module MongoMapper
6
+ include CarrierWave::Mount
7
+ ##
8
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
9
+ #
10
+ def mount_uploader(column, uploader, options={}, &block)
11
+ # We need to set the mount_on column (or key in MongoMapper's case)
12
+ # since MongoMapper will attempt to set the filename on
13
+ # the uploader instead of the file on a Document's initialization.
14
+ options[:mount_on] ||= "#{column}_filename"
15
+ key options[:mount_on]
16
+
17
+ super
18
+ alias_method :read_uploader, :[]
19
+ alias_method :write_uploader, :[]=
20
+ after_save "store_#{column}!".to_sym
21
+ before_save "write_#{column}_identifier".to_sym
22
+ after_destroy "remove_#{column}!".to_sym
23
+ end
24
+ end # MongoMapper
25
+ end # CarrierWave
26
+
27
+ MongoMapper::Document::ClassMethods.send(:include, CarrierWave::MongoMapper)
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/carrierwave.rb'}"
9
+ puts "Loading carrierwave gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,184 @@
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.find(: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.find(: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
+ context "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
+ end
182
+
183
+
184
+ end