durran-carrierwave 0.3.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Generators +4 -0
- data/History.txt +66 -0
- data/LICENSE +8 -0
- data/Manifest.txt +89 -0
- data/README.rdoc +342 -0
- data/Rakefile +30 -0
- data/carrierwave.gemspec +57 -0
- data/cucumber.yml +2 -0
- data/features/caching.feature +28 -0
- data/features/file_storage.feature +37 -0
- data/features/file_storage_overridden_filename.feature +38 -0
- data/features/file_storage_overridden_store_dir.feature +38 -0
- data/features/file_storage_reversing_processor.feature +43 -0
- data/features/fixtures/bork.txt +1 -0
- data/features/fixtures/monkey.txt +1 -0
- data/features/mount_activerecord.feature +46 -0
- data/features/mount_datamapper.feature +46 -0
- data/features/step_definitions/activerecord_steps.rb +22 -0
- data/features/step_definitions/caching_steps.rb +14 -0
- data/features/step_definitions/datamapper_steps.rb +29 -0
- data/features/step_definitions/file_steps.rb +42 -0
- data/features/step_definitions/general_steps.rb +80 -0
- data/features/step_definitions/mount_steps.rb +19 -0
- data/features/step_definitions/store_steps.rb +18 -0
- data/features/support/activerecord.rb +30 -0
- data/features/support/datamapper.rb +7 -0
- data/features/support/env.rb +35 -0
- data/features/versions_basics.feature +50 -0
- data/features/versions_nested_versions.feature +70 -0
- data/features/versions_overridden_filename.feature +51 -0
- data/features/versions_overriden_store_dir.feature +41 -0
- data/lib/carrierwave.rb +145 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/core_ext/blank.rb +46 -0
- data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
- data/lib/carrierwave/core_ext/module_setup.rb +51 -0
- data/lib/carrierwave/mount.rb +332 -0
- data/lib/carrierwave/orm/activerecord.rb +73 -0
- data/lib/carrierwave/orm/datamapper.rb +27 -0
- data/lib/carrierwave/orm/mongomapper.rb +27 -0
- data/lib/carrierwave/orm/sequel.rb +57 -0
- data/lib/carrierwave/processing/image_science.rb +72 -0
- data/lib/carrierwave/processing/rmagick.rb +286 -0
- data/lib/carrierwave/sanitized_file.rb +272 -0
- data/lib/carrierwave/storage/abstract.rb +32 -0
- data/lib/carrierwave/storage/file.rb +50 -0
- data/lib/carrierwave/storage/s3.rb +215 -0
- data/lib/carrierwave/test/matchers.rb +114 -0
- data/lib/carrierwave/uploader.rb +43 -0
- data/lib/carrierwave/uploader/cache.rb +116 -0
- data/lib/carrierwave/uploader/callbacks.rb +42 -0
- data/lib/carrierwave/uploader/default_path.rb +23 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/paths.rb +27 -0
- data/lib/carrierwave/uploader/processing.rb +81 -0
- data/lib/carrierwave/uploader/proxy.rb +62 -0
- data/lib/carrierwave/uploader/remove.rb +23 -0
- data/lib/carrierwave/uploader/store.rb +156 -0
- data/lib/carrierwave/uploader/url.rb +24 -0
- data/lib/carrierwave/uploader/versions.rb +147 -0
- data/lib/generators/uploader_generator.rb +22 -0
- data/rails_generators/uploader/USAGE +2 -0
- data/rails_generators/uploader/templates/uploader.rb +47 -0
- data/rails_generators/uploader/uploader_generator.rb +21 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/compatibility/paperclip_spec.rb +43 -0
- data/spec/fixtures/bork.txt +1 -0
- data/spec/fixtures/test.jpeg +1 -0
- data/spec/fixtures/test.jpg +1 -0
- data/spec/mount_spec.rb +517 -0
- data/spec/orm/activerecord_spec.rb +271 -0
- data/spec/orm/datamapper_spec.rb +161 -0
- data/spec/orm/mongomapper_spec.rb +184 -0
- data/spec/orm/sequel_spec.rb +192 -0
- data/spec/sanitized_file_spec.rb +612 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/uploader/cache_spec.rb +196 -0
- data/spec/uploader/default_path_spec.rb +68 -0
- data/spec/uploader/extension_whitelist_spec.rb +44 -0
- data/spec/uploader/mountable_spec.rb +33 -0
- data/spec/uploader/paths_spec.rb +22 -0
- data/spec/uploader/processing_spec.rb +62 -0
- data/spec/uploader/proxy_spec.rb +54 -0
- data/spec/uploader/remove_spec.rb +70 -0
- data/spec/uploader/store_spec.rb +274 -0
- data/spec/uploader/url_spec.rb +87 -0
- data/spec/uploader/versions_spec.rb +306 -0
- metadata +228 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Merb
|
4
|
+
module Generators
|
5
|
+
class UploaderGenerator < NamedGenerator
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
File.join(File.dirname(__FILE__), '..', '..', 'rails_generators', 'uploader', 'templates')
|
9
|
+
end
|
10
|
+
|
11
|
+
first_argument :name, :required => true, :desc => "The name of this uploader"
|
12
|
+
|
13
|
+
template :uploader do |t|
|
14
|
+
t.source = 'uploader.rb'
|
15
|
+
t.destination = "app/uploaders/#{file_name}_uploader.rb"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
add :uploader, UploaderGenerator
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class <%= class_name %>Uploader < CarrierWave::Uploader::Base
|
4
|
+
|
5
|
+
# Include RMagick or ImageScience support
|
6
|
+
# include CarrierWave::RMagick
|
7
|
+
# include CarrierWave::ImageScience
|
8
|
+
|
9
|
+
# Choose what kind of storage to use for this uploader
|
10
|
+
storage :file
|
11
|
+
# storage :s3
|
12
|
+
|
13
|
+
# Override the directory where uploaded files will be stored
|
14
|
+
# This is a sensible default for uploaders that are meant to be mounted:
|
15
|
+
def store_dir
|
16
|
+
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Provide a default path as a default if there hasn't been a file uploaded
|
20
|
+
# def default_path
|
21
|
+
# "images/fallback/" + [version_name, "default.png"].compact.join('_')
|
22
|
+
# end
|
23
|
+
|
24
|
+
# Process files as they are uploaded.
|
25
|
+
# process :scale => [200, 300]
|
26
|
+
#
|
27
|
+
# def scale(width, height)
|
28
|
+
# # do something
|
29
|
+
# end
|
30
|
+
|
31
|
+
# Create different versions of your uploaded files
|
32
|
+
# version :thumb do
|
33
|
+
# process :scale => [50, 50]
|
34
|
+
# end
|
35
|
+
|
36
|
+
# Add a white list of extensions which are allowed to be uploaded,
|
37
|
+
# for images you might use something like this:
|
38
|
+
# def extension_white_list
|
39
|
+
# %w(jpg jpeg gif png)
|
40
|
+
# end
|
41
|
+
|
42
|
+
# Override the filename of the uploaded files
|
43
|
+
# def filename
|
44
|
+
# "something.jpg"
|
45
|
+
# end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class UploaderGenerator < Rails::Generator::NamedBase
|
4
|
+
|
5
|
+
def manifest
|
6
|
+
record do |m|
|
7
|
+
m.directory 'app/uploaders'
|
8
|
+
m.template 'uploader.rb', "app/uploaders/#{name.underscore}_uploader.rb"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def class_name
|
13
|
+
name.camelize
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
def banner
|
19
|
+
"Usage: #{$0} uploader UploaderName"
|
20
|
+
end
|
21
|
+
end
|
data/script/console
ADDED
@@ -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"
|
data/script/destroy
ADDED
@@ -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)
|
data/script/generate
ADDED
@@ -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,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
4
|
+
|
5
|
+
require 'carrierwave/orm/activerecord'
|
6
|
+
|
7
|
+
describe CarrierWave::Compatibility::Paperclip do
|
8
|
+
|
9
|
+
before do
|
10
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base) do
|
11
|
+
include CarrierWave::Compatibility::Paperclip
|
12
|
+
end
|
13
|
+
@model = mock('a model')
|
14
|
+
@model.stub!(:id).and_return(23)
|
15
|
+
@uploader = @uploader_class.new(@model, :monkey)
|
16
|
+
end
|
17
|
+
|
18
|
+
after do
|
19
|
+
FileUtils.rm_rf(public_path)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#store_path' do
|
23
|
+
it "should mimics paperclip default" do
|
24
|
+
@uploader.store_path("monkey.png").should == CarrierWave.config[:root] + "/public/system/monkeys/23/original/monkey.png"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should interpolate the root path" do
|
28
|
+
@uploader.stub!(:paperclip_path).and_return(":rails_root/foo/bar")
|
29
|
+
@uploader.store_path("monkey.png").should == CarrierWave.config[:root] + "/foo/bar"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should interpolate the attachment" do
|
33
|
+
@uploader.stub!(:paperclip_path).and_return("/foo/:attachment/bar")
|
34
|
+
@uploader.store_path("monkey.png").should == "/foo/monkeys/bar"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should interpolate the id" do
|
38
|
+
@uploader.stub!(:paperclip_path).and_return("/foo/:id/bar")
|
39
|
+
@uploader.store_path("monkey.png").should == "/foo/23/bar"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
bork bork bork Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
@@ -0,0 +1 @@
|
|
1
|
+
this is stuff
|
@@ -0,0 +1 @@
|
|
1
|
+
this is stuff
|
data/spec/mount_spec.rb
ADDED
@@ -0,0 +1,517 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
4
|
+
|
5
|
+
describe CarrierWave::Mount do
|
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::Base)
|
18
|
+
|
19
|
+
@class.mount_uploader(:image, @uploader)
|
20
|
+
@instance = @class.new
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should maintain the ability to super" do
|
24
|
+
@class.class_eval do
|
25
|
+
def image_uploader
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def image=(val)
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
@instance.image_uploader.should be_an_instance_of(@uploader)
|
35
|
+
@instance.image = stub_file('test.jpg')
|
36
|
+
@instance.image.should be_an_instance_of(@uploader)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should inherit uploaders to subclasses" do
|
40
|
+
@subclass = Class.new(@class)
|
41
|
+
@subclass_instance = @subclass.new
|
42
|
+
@subclass_instance.image_uploader.should be_an_instance_of(@uploader)
|
43
|
+
@subclass_instance.image = stub_file('test.jpg')
|
44
|
+
@subclass_instance.image.should be_an_instance_of(@uploader)
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#image_uploader' do
|
48
|
+
it "should return the uploader" do
|
49
|
+
@instance.image_uploader.should be_an_instance_of(@uploader)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#image_uploader=' do
|
54
|
+
it "should set the uploader" do
|
55
|
+
@my_uploader = @uploader.new
|
56
|
+
@instance.image_uploader = @my_uploader
|
57
|
+
@instance.image_uploader.should == @my_uploader
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should use the set uploader" do
|
61
|
+
@my_uploader = @uploader.new
|
62
|
+
@my_uploader.store!(stub_file('test.jpg'))
|
63
|
+
@instance.image_uploader = @my_uploader
|
64
|
+
@instance.image_uploader.should == @my_uploader
|
65
|
+
@instance.image.should == @my_uploader
|
66
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#image' do
|
71
|
+
|
72
|
+
it "should return a blank uploader when nothing has been assigned" do
|
73
|
+
@instance.should_receive(:read_uploader).with(:image).twice.and_return(nil)
|
74
|
+
@instance.image.should be_an_instance_of(@uploader)
|
75
|
+
@instance.image.should be_blank
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return a blank uploader when an empty string has been assigned" do
|
79
|
+
@instance.should_receive(:read_uploader).with(:image).twice.and_return('')
|
80
|
+
@instance.image.should be_an_instance_of(@uploader)
|
81
|
+
@instance.image.should be_blank
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should retrieve a file from the storage if a value is stored in the database" do
|
85
|
+
@instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
|
86
|
+
@instance.image.should be_an_instance_of(@uploader)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should set the path to the store dir" do
|
90
|
+
@instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
|
91
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#image=' do
|
97
|
+
|
98
|
+
it "should cache a file" do
|
99
|
+
@instance.image = stub_file('test.jpg')
|
100
|
+
@instance.image.should be_an_instance_of(@uploader)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should copy a file into into the cache directory" do
|
104
|
+
@instance.image = stub_file('test.jpg')
|
105
|
+
@instance.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should do nothing when nil is assigned" do
|
109
|
+
@instance.should_not_receive(:write_uploader)
|
110
|
+
@instance.image = nil
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should do nothing when an empty string is assigned" do
|
114
|
+
@instance.should_not_receive(:write_uploader)
|
115
|
+
@instance.image = stub_file('test.jpg')
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should fail silently if the image fails an integrity check" do
|
119
|
+
@uploader.class_eval do
|
120
|
+
def extension_white_list
|
121
|
+
%(txt)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
@instance.image = stub_file('test.jpg')
|
125
|
+
@instance.image.should be_blank
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should fail silently if the image fails to be processed" do
|
129
|
+
@uploader.class_eval do
|
130
|
+
process :monkey
|
131
|
+
def monkey
|
132
|
+
raise CarrierWave::ProcessingError, "Ohh noez!"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
@instance.image = stub_file('test.jpg')
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
describe '#image?' do
|
141
|
+
|
142
|
+
it "should be false when nothing has been assigned" do
|
143
|
+
@instance.should_receive(:read_uploader).with(:image).and_return(nil)
|
144
|
+
@instance.image?.should be_false
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should be false when an empty string has been assigned" do
|
148
|
+
@instance.should_receive(:read_uploader).with(:image).and_return('')
|
149
|
+
@instance.image?.should be_false
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should be true when a file has been cached" do
|
153
|
+
@instance.image = stub_file('test.jpg')
|
154
|
+
@instance.image?.should be_true
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
describe '#image_url' do
|
160
|
+
|
161
|
+
it "should return nil when nothing has been assigned" do
|
162
|
+
@instance.should_receive(:read_uploader).with(:image).and_return(nil)
|
163
|
+
@instance.image_url.should be_nil
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should return nil when an empty string has been assigned" do
|
167
|
+
@instance.should_receive(:read_uploader).with(:image).and_return('')
|
168
|
+
@instance.image_url.should be_nil
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should get the url from a retrieved file" do
|
172
|
+
@instance.should_receive(:read_uploader).at_least(:once).with(:image).and_return('test.jpg')
|
173
|
+
@instance.image_url.should == '/uploads/test.jpg'
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should get the url from a cached file" do
|
177
|
+
@instance.image = stub_file('test.jpg')
|
178
|
+
@instance.image_url.should =~ %r{uploads/tmp/[\d\-]+/test.jpg}
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should get the url from a cached file's version" do
|
182
|
+
@uploader.version(:thumb)
|
183
|
+
@instance.image = stub_file('test.jpg')
|
184
|
+
@instance.image_url(:thumb).should =~ %r{uploads/tmp/[\d\-]+/thumb_test.jpg}
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
describe '#image_cache' do
|
190
|
+
|
191
|
+
before do
|
192
|
+
@instance.stub!(:write_uploader)
|
193
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should return nil when nothing has been assigned" do
|
197
|
+
@instance.image_cache.should be_nil
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should be nil when a file has been stored" do
|
201
|
+
@instance.image = stub_file('test.jpg')
|
202
|
+
@instance.image.store!
|
203
|
+
@instance.image_cache.should be_nil
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should be the cache name when a file has been cached" do
|
207
|
+
@instance.image = stub_file('test.jpg')
|
208
|
+
@instance.image_cache.should =~ %r(^[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}/test\.jpg$)
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
describe '#image_cache=' do
|
214
|
+
|
215
|
+
before do
|
216
|
+
@instance.stub!(:write_uploader)
|
217
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
218
|
+
CarrierWave::SanitizedFile.new(file_path('test.jpg')).copy_to(public_path('uploads/tmp/19990512-1202-123-1234/test.jpg'))
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should do nothing when nil is assigned" do
|
222
|
+
@instance.image_cache = nil
|
223
|
+
@instance.image.should be_blank
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should do nothing when an empty string is assigned" do
|
227
|
+
@instance.image_cache = ''
|
228
|
+
@instance.image.should be_blank
|
229
|
+
end
|
230
|
+
|
231
|
+
it "retrieve from cache when a cache name is assigned" do
|
232
|
+
@instance.image_cache = '19990512-1202-123-1234/test.jpg'
|
233
|
+
@instance.image.current_path.should == public_path('uploads/tmp/19990512-1202-123-1234/test.jpg')
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should not write over a previously assigned file" do
|
237
|
+
@instance.image = stub_file('test.jpg')
|
238
|
+
@instance.image_cache = '19990512-1202-123-1234/monkey.jpg'
|
239
|
+
@instance.image.current_path.should =~ /test.jpg$/
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
describe '#store_image!' do
|
244
|
+
|
245
|
+
before do
|
246
|
+
@instance.stub!(:write_uploader)
|
247
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
248
|
+
end
|
249
|
+
|
250
|
+
it "should do nothing when no file has been uploaded" do
|
251
|
+
@instance.store_image!
|
252
|
+
@instance.image.should be_blank
|
253
|
+
end
|
254
|
+
|
255
|
+
it "store an assigned file" do
|
256
|
+
@instance.image = stub_file('test.jpg')
|
257
|
+
@instance.store_image!
|
258
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should remove an uploaded file when remove_image? returns true" do
|
262
|
+
@instance.image = stub_file('test.jpg')
|
263
|
+
path = @instance.image.current_path
|
264
|
+
@instance.remove_image = true
|
265
|
+
@instance.store_image!
|
266
|
+
@instance.image.should be_blank
|
267
|
+
File.exist?(path).should be_false
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
describe '#remove_image!' do
|
272
|
+
|
273
|
+
before do
|
274
|
+
@instance.stub!(:write_uploader)
|
275
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should do nothing when no file has been uploaded" do
|
279
|
+
@instance.remove_image!
|
280
|
+
@instance.image.should be_blank
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should remove an uploaded file" do
|
284
|
+
@instance.image = stub_file('test.jpg')
|
285
|
+
path = @instance.image.current_path
|
286
|
+
@instance.remove_image!
|
287
|
+
@instance.image.should be_blank
|
288
|
+
File.exist?(path).should be_false
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
describe '#remove_image' do
|
293
|
+
|
294
|
+
it "should store a value" do
|
295
|
+
@instance.remove_image = true
|
296
|
+
@instance.remove_image.should be_true
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
describe '#remove_image?' do
|
302
|
+
|
303
|
+
it "should be true when the value is truthy" do
|
304
|
+
@instance.remove_image = true
|
305
|
+
@instance.remove_image?.should be_true
|
306
|
+
end
|
307
|
+
|
308
|
+
it "should be false when the value is falsey" do
|
309
|
+
@instance.remove_image = false
|
310
|
+
@instance.remove_image?.should be_false
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should be false when the value is ''" do
|
314
|
+
@instance.remove_image = ''
|
315
|
+
@instance.remove_image?.should be_false
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should be false when the value is '0'" do
|
319
|
+
@instance.remove_image = '0'
|
320
|
+
@instance.remove_image?.should be_false
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should be false when the value is 'false'" do
|
324
|
+
@instance.remove_image = 'false'
|
325
|
+
@instance.remove_image?.should be_false
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|
329
|
+
|
330
|
+
describe '#image_integrity_error' do
|
331
|
+
|
332
|
+
it "should be nil by default" do
|
333
|
+
@instance.image_integrity_error.should be_nil
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should be nil after a file is cached" do
|
337
|
+
@instance.image = stub_file('test.jpg')
|
338
|
+
@instance.image_integrity_error.should be_nil
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should be an error instance after an integrity check has failed" do
|
342
|
+
@uploader.class_eval do
|
343
|
+
def extension_white_list
|
344
|
+
%(txt)
|
345
|
+
end
|
346
|
+
end
|
347
|
+
@instance.image = stub_file('test.jpg')
|
348
|
+
@instance.image_integrity_error.should be_an_instance_of(CarrierWave::IntegrityError)
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
describe '#image_processing_error' do
|
353
|
+
|
354
|
+
it "should be nil by default" do
|
355
|
+
@instance.image_processing_error.should be_nil
|
356
|
+
end
|
357
|
+
|
358
|
+
it "should be nil after a file is cached" do
|
359
|
+
@instance.image = stub_file('test.jpg')
|
360
|
+
@instance.image_processing_error.should be_nil
|
361
|
+
end
|
362
|
+
|
363
|
+
it "should be an error instance after an integrity check has failed" do
|
364
|
+
@uploader.class_eval do
|
365
|
+
process :monkey
|
366
|
+
def monkey
|
367
|
+
raise CarrierWave::ProcessingError, "Ohh noez!"
|
368
|
+
end
|
369
|
+
end
|
370
|
+
@instance.image = stub_file('test.jpg')
|
371
|
+
@instance.image_processing_error.should be_an_instance_of(CarrierWave::ProcessingError)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
describe '#write_image_identifier' do
|
376
|
+
it "should write to the column" do
|
377
|
+
@instance.should_receive(:write_uploader).with(:image, "test.jpg")
|
378
|
+
@instance.image = stub_file('test.jpg')
|
379
|
+
@instance.write_image_identifier
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should remove from the column when remove_image is true" do
|
383
|
+
@instance.image = stub_file('test.jpg')
|
384
|
+
@instance.store_image!
|
385
|
+
@instance.remove_image = true
|
386
|
+
@instance.should_receive(:write_uploader).with(:image, "")
|
387
|
+
@instance.write_image_identifier
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
end
|
392
|
+
|
393
|
+
describe '#mount_uploader with a block' do
|
394
|
+
|
395
|
+
before do
|
396
|
+
@class = Class.new
|
397
|
+
@class.send(:extend, CarrierWave::Mount)
|
398
|
+
@class.mount_uploader(:image) do
|
399
|
+
def monkey
|
400
|
+
'blah'
|
401
|
+
end
|
402
|
+
end
|
403
|
+
@instance = @class.new
|
404
|
+
end
|
405
|
+
|
406
|
+
describe '#image' do
|
407
|
+
|
408
|
+
before do
|
409
|
+
@instance.stub!(:read_uploader).and_return('test.jpg')
|
410
|
+
end
|
411
|
+
|
412
|
+
it "should return an instance of a subclass of CarrierWave::Uploader::Base" do
|
413
|
+
@instance.image.should be_a(CarrierWave::Uploader::Base)
|
414
|
+
end
|
415
|
+
|
416
|
+
it "should set the path to the store dir" do
|
417
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
418
|
+
end
|
419
|
+
|
420
|
+
it "should apply any custom modifications" do
|
421
|
+
@instance.image.monkey.should == "blah"
|
422
|
+
end
|
423
|
+
|
424
|
+
end
|
425
|
+
|
426
|
+
end
|
427
|
+
|
428
|
+
describe '#mount_uploader with :ignore_integrity_errors => false' do
|
429
|
+
|
430
|
+
before do
|
431
|
+
@class = Class.new
|
432
|
+
@class.send(:extend, CarrierWave::Mount)
|
433
|
+
|
434
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
435
|
+
|
436
|
+
@class.mount_uploader(:image, @uploader, :ignore_integrity_errors => false)
|
437
|
+
@instance = @class.new
|
438
|
+
end
|
439
|
+
|
440
|
+
it "should raise an error if the image fails an integrity check" do
|
441
|
+
@uploader.class_eval do
|
442
|
+
def extension_white_list
|
443
|
+
%(txt)
|
444
|
+
end
|
445
|
+
end
|
446
|
+
running {
|
447
|
+
@instance.image = stub_file('test.jpg')
|
448
|
+
}.should raise_error(CarrierWave::IntegrityError)
|
449
|
+
end
|
450
|
+
|
451
|
+
end
|
452
|
+
|
453
|
+
describe '#mount_uploader with :ignore_processing_errors => false' do
|
454
|
+
|
455
|
+
before do
|
456
|
+
@class = Class.new
|
457
|
+
@class.send(:extend, CarrierWave::Mount)
|
458
|
+
|
459
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
460
|
+
|
461
|
+
@class.mount_uploader(:image, @uploader, :ignore_processing_errors => false)
|
462
|
+
@instance = @class.new
|
463
|
+
end
|
464
|
+
|
465
|
+
it "should raise an error if the image fails to be processed" do
|
466
|
+
@uploader.class_eval do
|
467
|
+
process :monkey
|
468
|
+
def monkey
|
469
|
+
raise CarrierWave::ProcessingError, "Ohh noez!"
|
470
|
+
end
|
471
|
+
end
|
472
|
+
running {
|
473
|
+
@instance.image = stub_file('test.jpg')
|
474
|
+
}.should raise_error(CarrierWave::ProcessingError)
|
475
|
+
end
|
476
|
+
|
477
|
+
end
|
478
|
+
|
479
|
+
describe '#mount_uploader with :mount_on => :monkey' do
|
480
|
+
|
481
|
+
|
482
|
+
before do
|
483
|
+
@class = Class.new
|
484
|
+
@class.send(:extend, CarrierWave::Mount)
|
485
|
+
|
486
|
+
@uploader = Class.new(CarrierWave::Uploader::Base)
|
487
|
+
|
488
|
+
@class.mount_uploader(:image, @uploader, :mount_on => :monkey)
|
489
|
+
@instance = @class.new
|
490
|
+
end
|
491
|
+
|
492
|
+
describe '#image' do
|
493
|
+
it "should retrieve a file from the storage if a value is stored in the database" do
|
494
|
+
@instance.should_receive(:read_uploader).at_least(:once).with(:monkey).twice.and_return('test.jpg')
|
495
|
+
@instance.image.should be_an_instance_of(@uploader)
|
496
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
497
|
+
end
|
498
|
+
end
|
499
|
+
|
500
|
+
describe '#write_image_identifier' do
|
501
|
+
it "should write to the given column" do
|
502
|
+
@instance.should_receive(:write_uploader).with(:monkey, "test.jpg")
|
503
|
+
@instance.image = stub_file('test.jpg')
|
504
|
+
@instance.write_image_identifier
|
505
|
+
end
|
506
|
+
|
507
|
+
it "should remove from the given column when remove_image is true" do
|
508
|
+
@instance.image = stub_file('test.jpg')
|
509
|
+
@instance.store_image!
|
510
|
+
@instance.remove_image = true
|
511
|
+
@instance.should_receive(:write_uploader).with(:monkey, "")
|
512
|
+
@instance.write_image_identifier
|
513
|
+
end
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
end
|