dsturnbull-carrierwave 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/Generators +4 -0
  2. data/History.txt +118 -0
  3. data/Manifest.txt +107 -0
  4. data/README.rdoc +502 -0
  5. data/Rakefile +38 -0
  6. data/cucumber.yml +2 -0
  7. data/features/caching.feature +28 -0
  8. data/features/download.feature +20 -0
  9. data/features/file_storage.feature +37 -0
  10. data/features/file_storage_overridden_filename.feature +38 -0
  11. data/features/file_storage_overridden_store_dir.feature +38 -0
  12. data/features/file_storage_reversing_processor.feature +43 -0
  13. data/features/fixtures/bork.txt +1 -0
  14. data/features/fixtures/monkey.txt +1 -0
  15. data/features/grid_fs_storage.feature +32 -0
  16. data/features/mount_activerecord.feature +46 -0
  17. data/features/mount_datamapper.feature +46 -0
  18. data/features/step_definitions/activerecord_steps.rb +22 -0
  19. data/features/step_definitions/caching_steps.rb +14 -0
  20. data/features/step_definitions/datamapper_steps.rb +29 -0
  21. data/features/step_definitions/download_steps.rb +4 -0
  22. data/features/step_definitions/file_steps.rb +53 -0
  23. data/features/step_definitions/general_steps.rb +85 -0
  24. data/features/step_definitions/mount_steps.rb +19 -0
  25. data/features/step_definitions/store_steps.rb +18 -0
  26. data/features/support/activerecord.rb +30 -0
  27. data/features/support/datamapper.rb +7 -0
  28. data/features/support/env.rb +22 -0
  29. data/features/versions_basics.feature +50 -0
  30. data/features/versions_nested_versions.feature +70 -0
  31. data/features/versions_overridden_filename.feature +51 -0
  32. data/features/versions_overriden_store_dir.feature +41 -0
  33. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  34. data/lib/carrierwave/core_ext/blank.rb +46 -0
  35. data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
  36. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  37. data/lib/carrierwave/mount.rb +359 -0
  38. data/lib/carrierwave/orm/activerecord.rb +73 -0
  39. data/lib/carrierwave/orm/datamapper.rb +27 -0
  40. data/lib/carrierwave/orm/mongoid.rb +23 -0
  41. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  42. data/lib/carrierwave/orm/sequel.rb +45 -0
  43. data/lib/carrierwave/processing/image_science.rb +101 -0
  44. data/lib/carrierwave/processing/mini_magick.rb +269 -0
  45. data/lib/carrierwave/processing/rmagick.rb +282 -0
  46. data/lib/carrierwave/sanitized_file.rb +268 -0
  47. data/lib/carrierwave/storage/abstract.rb +30 -0
  48. data/lib/carrierwave/storage/file.rb +48 -0
  49. data/lib/carrierwave/storage/grid_fs.rb +96 -0
  50. data/lib/carrierwave/storage/right_s3.rb +170 -0
  51. data/lib/carrierwave/storage/s3.rb +199 -0
  52. data/lib/carrierwave/test/matchers.rb +128 -0
  53. data/lib/carrierwave/uploader/cache.rb +145 -0
  54. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  55. data/lib/carrierwave/uploader/configuration.rb +122 -0
  56. data/lib/carrierwave/uploader/default_url.rb +19 -0
  57. data/lib/carrierwave/uploader/download.rb +59 -0
  58. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  59. data/lib/carrierwave/uploader/mountable.rb +39 -0
  60. data/lib/carrierwave/uploader/processing.rb +83 -0
  61. data/lib/carrierwave/uploader/proxy.rb +62 -0
  62. data/lib/carrierwave/uploader/remove.rb +22 -0
  63. data/lib/carrierwave/uploader/store.rb +89 -0
  64. data/lib/carrierwave/uploader/url.rb +24 -0
  65. data/lib/carrierwave/uploader/versions.rb +146 -0
  66. data/lib/carrierwave/uploader.rb +44 -0
  67. data/lib/carrierwave.rb +96 -0
  68. data/merb_generators/uploader_generator.rb +22 -0
  69. data/rails_generators/uploader/USAGE +2 -0
  70. data/rails_generators/uploader/templates/uploader.rb +47 -0
  71. data/rails_generators/uploader/uploader_generator.rb +21 -0
  72. data/script/console +10 -0
  73. data/script/destroy +14 -0
  74. data/script/generate +14 -0
  75. data/spec/compatibility/paperclip_spec.rb +52 -0
  76. data/spec/fixtures/bork.txt +1 -0
  77. data/spec/fixtures/landscape.jpg +0 -0
  78. data/spec/fixtures/portrait.jpg +0 -0
  79. data/spec/fixtures/test.jpeg +1 -0
  80. data/spec/fixtures/test.jpg +1 -0
  81. data/spec/mount_spec.rb +538 -0
  82. data/spec/orm/activerecord_spec.rb +271 -0
  83. data/spec/orm/datamapper_spec.rb +168 -0
  84. data/spec/orm/mongoid_spec.rb +206 -0
  85. data/spec/orm/mongomapper_spec.rb +202 -0
  86. data/spec/orm/sequel_spec.rb +183 -0
  87. data/spec/processing/image_science_spec.rb +56 -0
  88. data/spec/processing/mini_magick_spec.rb +76 -0
  89. data/spec/processing/rmagick_spec.rb +75 -0
  90. data/spec/sanitized_file_spec.rb +601 -0
  91. data/spec/spec_helper.rb +99 -0
  92. data/spec/storage/grid_fs_spec.rb +82 -0
  93. data/spec/storage/right_s3_spec.rb +75 -0
  94. data/spec/storage/s3_spec.rb +95 -0
  95. data/spec/uploader/cache_spec.rb +209 -0
  96. data/spec/uploader/configuration_spec.rb +105 -0
  97. data/spec/uploader/default_url_spec.rb +85 -0
  98. data/spec/uploader/download_spec.rb +75 -0
  99. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  100. data/spec/uploader/mountable_spec.rb +33 -0
  101. data/spec/uploader/paths_spec.rb +22 -0
  102. data/spec/uploader/processing_spec.rb +73 -0
  103. data/spec/uploader/proxy_spec.rb +54 -0
  104. data/spec/uploader/remove_spec.rb +70 -0
  105. data/spec/uploader/store_spec.rb +248 -0
  106. data/spec/uploader/url_spec.rb +87 -0
  107. data/spec/uploader/versions_spec.rb +298 -0
  108. metadata +351 -0
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader::Download do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#download!' do
17
+
18
+ before do
19
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
20
+ response = mock('HTTP Response')
21
+ response.stub!(:body).and_return('Response Body')
22
+ Net::HTTP.stub!(:get_response).and_return(response)
23
+ end
24
+
25
+ it "should cache a file" do
26
+ @uploader.download!('http://www.example.com/test/file.png')
27
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
28
+ end
29
+
30
+ it "should be cached" do
31
+ @uploader.download!('http://www.example.com/test/file.png')
32
+ @uploader.should be_cached
33
+ end
34
+
35
+ it "should store the cache name" do
36
+ @uploader.download!('http://www.example.com/test/file.png')
37
+ @uploader.cache_name.should == '20071201-1234-345-2255/file.png'
38
+ end
39
+
40
+ it "should set the filename to the file's sanitized filename" do
41
+ @uploader.download!('http://www.example.com/test/file.png')
42
+ @uploader.filename.should == 'file.png'
43
+ end
44
+
45
+ it "should move it to the tmp dir" do
46
+ @uploader.download!('http://www.example.com/test/file.png')
47
+ @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/file.png')
48
+ @uploader.file.exists?.should be_true
49
+ end
50
+
51
+ it "should set the url" do
52
+ @uploader.download!('http://www.example.com/test/file.png')
53
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/file.png'
54
+ end
55
+
56
+ it "should do nothing when trying to download an empty file" do
57
+ @uploader.download!(nil)
58
+ end
59
+
60
+ it "should set permissions if options are given" do
61
+ @uploader_class.permissions = 0777
62
+
63
+ @uploader.download!('http://www.example.com/test/file.png')
64
+ @uploader.should have_permissions(0777)
65
+ end
66
+
67
+ it "should raise an error when trying to download a local file" do
68
+ running {
69
+ @uploader.download!('/etc/passwd')
70
+ }.should raise_error(CarrierWave::DownloadError)
71
+ end
72
+ end
73
+
74
+ end
75
+
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#cache!' do
17
+
18
+ before do
19
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
20
+ end
21
+
22
+ it "should not raise an integiry error if there is no white list" do
23
+ @uploader.stub!(:extension_white_list).and_return(nil)
24
+ running {
25
+ @uploader.cache!(File.open(file_path('test.jpg')))
26
+ }.should_not raise_error(CarrierWave::IntegrityError)
27
+ end
28
+
29
+ it "should not raise an integiry error if there is a white list and the file is on it" do
30
+ @uploader.stub!(:extension_white_list).and_return(%w(jpg gif png))
31
+ running {
32
+ @uploader.cache!(File.open(file_path('test.jpg')))
33
+ }.should_not raise_error(CarrierWave::IntegrityError)
34
+ end
35
+
36
+ it "should raise an integiry error if there is a white list and the file is not on it" do
37
+ @uploader.stub!(:extension_white_list).and_return(%w(txt doc xls))
38
+ running {
39
+ @uploader.cache!(File.open(file_path('test.jpg')))
40
+ }.should raise_error(CarrierWave::IntegrityError)
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#model' do
17
+ it "should be remembered from initialization" do
18
+ model = mock('a model object')
19
+ @uploader = @uploader_class.new(model)
20
+ @uploader.model.should == model
21
+ end
22
+ end
23
+
24
+ describe '#mounted_as' do
25
+ it "should be remembered from initialization" do
26
+ model = mock('a model object')
27
+ @uploader = @uploader_class.new(model, :llama)
28
+ @uploader.model.should == model
29
+ @uploader.mounted_as.should == :llama
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#root' do
17
+ it "should default to the config option" do
18
+ @uploader.root.should == public_path
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '.process' do
17
+ it "should add a single processor when a symbol is given" do
18
+ @uploader_class.process :sepiatone
19
+ @uploader.should_receive(:sepiatone)
20
+ @uploader.process!
21
+ end
22
+
23
+ it "should add multiple processors when an array of symbols is given" do
24
+ @uploader_class.process :sepiatone, :desaturate, :invert
25
+ @uploader.should_receive(:sepiatone)
26
+ @uploader.should_receive(:desaturate)
27
+ @uploader.should_receive(:invert)
28
+ @uploader.process!
29
+ end
30
+
31
+ it "should add a single processor with an argument when a hash is given" do
32
+ @uploader_class.process :format => 'png'
33
+ @uploader.should_receive(:format).with('png')
34
+ @uploader.process!
35
+ end
36
+
37
+ it "should add a single processor with several argument when a hash is given" do
38
+ @uploader_class.process :resize => [200, 300]
39
+ @uploader.should_receive(:resize).with(200, 300)
40
+ @uploader.process!
41
+ end
42
+
43
+ it "should add multiple processors when an hash with multiple keys is given" do
44
+ @uploader_class.process :resize => [200, 300], :format => 'png'
45
+ @uploader.should_receive(:resize).with(200, 300)
46
+ @uploader.should_receive(:format).with('png')
47
+ @uploader.process!
48
+ end
49
+
50
+ context "with 'enable_processing' set to false" do
51
+ it "should not do any processing" do
52
+ @uploader_class.enable_processing = false
53
+ @uploader_class.process :sepiatone, :desaturate, :invert
54
+ @uploader.should_not_receive(:sepiatone)
55
+ @uploader.should_not_receive(:desaturate)
56
+ @uploader.should_not_receive(:invert)
57
+ @uploader.process!
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#cache!' do
63
+ before do
64
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
65
+ end
66
+
67
+ it "should trigger a process!" do
68
+ @uploader.should_receive(:process!)
69
+ @uploader.cache!(File.open(file_path('test.jpg')))
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#blank?' do
17
+ it "should be true when nothing has been done" do
18
+ @uploader.should be_blank
19
+ end
20
+
21
+ it "should not be true when the file is empty" do
22
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
23
+ @uploader.should be_blank
24
+ end
25
+
26
+ it "should not be true when a file has been cached" do
27
+ @uploader.cache!(File.open(file_path('test.jpg')))
28
+ @uploader.should_not be_blank
29
+ end
30
+ end
31
+
32
+ describe '#read' do
33
+ it "should be nil by default" do
34
+ @uploader.read.should be_nil
35
+ end
36
+
37
+ it "should read the contents of a cached file" do
38
+ @uploader.cache!(File.open(file_path('test.jpg')))
39
+ @uploader.read.should == "this is stuff"
40
+ end
41
+ end
42
+
43
+ describe '#size' do
44
+ it "should be zero by default" do
45
+ @uploader.size.should == 0
46
+ end
47
+
48
+ it "should get the size of a cached file" do
49
+ @uploader.cache!(File.open(file_path('test.jpg')))
50
+ @uploader.size.should == 13
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#remove!' do
17
+ before do
18
+ @file = File.open(file_path('test.jpg'))
19
+
20
+ @stored_file = mock('a stored file')
21
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
22
+ @stored_file.stub!(:url).and_return('http://www.example.com')
23
+ @stored_file.stub!(:identifier).and_return('this-is-me')
24
+ @stored_file.stub!(:delete)
25
+
26
+ @storage = mock('a storage engine')
27
+ @storage.stub!(:store!).and_return(@stored_file)
28
+
29
+ @uploader_class.storage.stub!(:new).and_return(@storage)
30
+ @uploader.store!(@file)
31
+ end
32
+
33
+ it "should reset the current path" do
34
+ @uploader.remove!
35
+ @uploader.current_path.should be_nil
36
+ end
37
+
38
+ it "should not be cached" do
39
+ @uploader.remove!
40
+ @uploader.should_not be_cached
41
+ end
42
+
43
+ it "should reset the url" do
44
+ @uploader.cache!(@file)
45
+ @uploader.remove!
46
+ @uploader.url.should be_nil
47
+ end
48
+
49
+ it "should reset the identifier" do
50
+ @uploader.remove!
51
+ @uploader.identifier.should be_nil
52
+ end
53
+
54
+ it "should delete the file" do
55
+ @stored_file.should_receive(:delete)
56
+ @uploader.remove!
57
+ end
58
+
59
+ it "should reset the cache_name" do
60
+ @uploader.cache!(@file)
61
+ @uploader.remove!
62
+ @uploader.cache_name.should be_nil
63
+ end
64
+
65
+ it "should do nothing when trying to remove an empty file" do
66
+ running { @uploader.remove! }.should_not raise_error
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,248 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#store_dir' do
17
+ it "should default to the config option" do
18
+ @uploader.store_dir.should == 'uploads'
19
+ end
20
+ end
21
+
22
+ describe '#filename' do
23
+ it "should default to nil" do
24
+ @uploader.filename.should be_nil
25
+ end
26
+ end
27
+
28
+ describe '#store!' do
29
+ before do
30
+ @file = File.open(file_path('test.jpg'))
31
+
32
+ @stored_file = mock('a stored file')
33
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
34
+ @stored_file.stub!(:url).and_return('http://www.example.com')
35
+
36
+ @storage = mock('a storage engine')
37
+ @storage.stub!(:store!).and_return(@stored_file)
38
+ @storage.stub!(:identifier).and_return('this-is-me')
39
+
40
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
41
+ end
42
+
43
+ it "should set the current path" do
44
+ @uploader.store!(@file)
45
+ @uploader.current_path.should == '/path/to/somewhere'
46
+ end
47
+
48
+ it "should not be cached" do
49
+ @uploader.store!(@file)
50
+ @uploader.should_not be_cached
51
+ end
52
+
53
+ it "should set the url" do
54
+ @uploader.store!(@file)
55
+ @uploader.url.should == 'http://www.example.com'
56
+ end
57
+
58
+ it "should set the identifier" do
59
+ @uploader.store!(@file)
60
+ @uploader.identifier.should == 'this-is-me'
61
+ end
62
+
63
+ it "should, if a file is given as argument, cache that file" do
64
+ @uploader.should_receive(:cache!).with(@file)
65
+ @uploader.store!(@file)
66
+ end
67
+
68
+ it "should use a previously cached file if no argument is given" do
69
+ @uploader.cache!(File.open(file_path('test.jpg')))
70
+ @uploader.should_not_receive(:cache!)
71
+ @uploader.store!
72
+ end
73
+
74
+ it "should instruct the storage engine to store the file" do
75
+ @uploader.cache!(@file)
76
+ @storage.should_receive(:store!).with(@uploader.file).and_return(:monkey)
77
+ @uploader.store!
78
+ end
79
+
80
+ it "should reset the cache_name" do
81
+ @uploader.cache!(@file)
82
+ @uploader.store!
83
+ @uploader.cache_name.should be_nil
84
+ end
85
+
86
+ it "should cache the result given by the storage engine" do
87
+ @uploader.store!(@file)
88
+ @uploader.file.should == @stored_file
89
+ end
90
+
91
+ it "should do nothing when trying to store an empty file" do
92
+ @uploader.store!(nil)
93
+ end
94
+
95
+ it "should not re-store a retrieved file" do
96
+ @stored_file = mock('a stored file')
97
+ @storage.stub!(:retrieve!).and_return(@stored_file)
98
+
99
+ @uploader_class.storage.should_not_receive(:store!)
100
+ @uploader.retrieve_from_store!('monkey.txt')
101
+ @uploader.store!
102
+ end
103
+ end
104
+
105
+ describe '#retrieve_from_store!' do
106
+ before do
107
+ @stored_file = mock('a stored file')
108
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
109
+ @stored_file.stub!(:url).and_return('http://www.example.com')
110
+
111
+ @storage = mock('a storage engine')
112
+ @storage.stub!(:retrieve!).and_return(@stored_file)
113
+ @storage.stub!(:identifier).and_return('this-is-me')
114
+
115
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
116
+ end
117
+
118
+ it "should set the current path" do
119
+ @uploader.retrieve_from_store!('monkey.txt')
120
+ @uploader.current_path.should == '/path/to/somewhere'
121
+ end
122
+
123
+ it "should not be cached" do
124
+ @uploader.retrieve_from_store!('monkey.txt')
125
+ @uploader.should_not be_cached
126
+ end
127
+
128
+ it "should set the url" do
129
+ @uploader.retrieve_from_store!('monkey.txt')
130
+ @uploader.url.should == 'http://www.example.com'
131
+ end
132
+
133
+ it "should set the identifier" do
134
+ @uploader.retrieve_from_store!('monkey.txt')
135
+ @uploader.identifier.should == 'this-is-me'
136
+ end
137
+
138
+ it "should instruct the storage engine to retrieve the file and store the result" do
139
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
140
+ @uploader.retrieve_from_store!('monkey.txt')
141
+ @uploader.file.should == @stored_file
142
+ end
143
+
144
+ it "should overwrite a file that has already been cached" do
145
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
146
+ @uploader.retrieve_from_store!('bork.txt')
147
+ @uploader.file.should == @stored_file
148
+ end
149
+ end
150
+
151
+ describe 'with an overridden filename' do
152
+ before do
153
+ @uploader_class.class_eval do
154
+ def filename; "foo.jpg"; end
155
+ end
156
+ end
157
+
158
+ it "should create new files if there is a file" do
159
+ @file = File.open(file_path('test.jpg'))
160
+ @uploader.store!(@file)
161
+ @path = ::File.expand_path(@uploader.store_path, @uploader.root)
162
+ File.exist?(@path).should be_true
163
+ end
164
+
165
+ it "should not create new files if there is no file" do
166
+ @uploader.store!(nil)
167
+ @path = ::File.expand_path(@uploader.store_path, @uploader.root)
168
+ File.exist?(@path).should be_false
169
+ end
170
+ end
171
+
172
+ describe 'with an overridden, reversing, filename' do
173
+ before do
174
+ @uploader_class.class_eval do
175
+ def filename
176
+ super.reverse unless super.blank?
177
+ end
178
+ end
179
+ end
180
+
181
+ describe '#store!' do
182
+ before do
183
+ @file = File.open(file_path('test.jpg'))
184
+
185
+ @stored_file = mock('a stored file')
186
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
187
+ @stored_file.stub!(:url).and_return('http://www.example.com')
188
+
189
+ @storage = mock('a storage engine')
190
+ @storage.stub!(:store!).and_return(@stored_file)
191
+
192
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
193
+ end
194
+
195
+ it "should set the current path" do
196
+ @uploader.store!(@file)
197
+ @uploader.current_path.should == '/path/to/somewhere'
198
+ end
199
+
200
+ it "should set the url" do
201
+ @uploader.store!(@file)
202
+ @uploader.url.should == 'http://www.example.com'
203
+ end
204
+
205
+ it "should, if a file is given as argument, reverse the filename" do
206
+ @uploader.store!(@file)
207
+ @uploader.filename.should == 'gpj.tset'
208
+ end
209
+
210
+ end
211
+
212
+ describe '#retrieve_from_store!' do
213
+ before do
214
+ @stored_file = mock('a stored file')
215
+ @stored_file.stub!(:path).and_return('/path/to/somewhere')
216
+ @stored_file.stub!(:url).and_return('http://www.example.com')
217
+
218
+ @storage = mock('a storage engine')
219
+ @storage.stub!(:retrieve!).and_return(@stored_file)
220
+
221
+ @uploader_class.storage.stub!(:new).with(@uploader).and_return(@storage)
222
+ end
223
+
224
+ it "should set the current path" do
225
+ @uploader.retrieve_from_store!('monkey.txt')
226
+ @uploader.current_path.should == '/path/to/somewhere'
227
+ end
228
+
229
+ it "should set the url" do
230
+ @uploader.retrieve_from_store!('monkey.txt')
231
+ @uploader.url.should == 'http://www.example.com'
232
+ end
233
+
234
+ it "should pass the identifier to the storage engine" do
235
+ @storage.should_receive(:retrieve!).with('monkey.txt').and_return(@stored_file)
236
+ @uploader.retrieve_from_store!('monkey.txt')
237
+ @uploader.file.should == @stored_file
238
+ end
239
+
240
+ it "should not set the filename" do
241
+ @uploader.retrieve_from_store!('monkey.txt')
242
+ @uploader.filename.should be_nil
243
+ end
244
+ end
245
+
246
+ end
247
+
248
+ end