samlown-carrierwave 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/Generators +4 -0
  2. data/History.txt +125 -0
  3. data/Manifest.txt +110 -0
  4. data/README.rdoc +524 -0
  5. data/Rakefile +39 -0
  6. data/carrierwave.gemspec +85 -0
  7. data/cucumber.yml +2 -0
  8. data/features/caching.feature +28 -0
  9. data/features/download.feature +20 -0
  10. data/features/file_storage.feature +37 -0
  11. data/features/file_storage_overridden_filename.feature +38 -0
  12. data/features/file_storage_overridden_store_dir.feature +38 -0
  13. data/features/file_storage_reversing_processor.feature +43 -0
  14. data/features/fixtures/bork.txt +1 -0
  15. data/features/fixtures/monkey.txt +1 -0
  16. data/features/grid_fs_storage.feature +32 -0
  17. data/features/mount_activerecord.feature +46 -0
  18. data/features/mount_datamapper.feature +46 -0
  19. data/features/step_definitions/activerecord_steps.rb +22 -0
  20. data/features/step_definitions/caching_steps.rb +14 -0
  21. data/features/step_definitions/datamapper_steps.rb +29 -0
  22. data/features/step_definitions/download_steps.rb +4 -0
  23. data/features/step_definitions/file_steps.rb +53 -0
  24. data/features/step_definitions/general_steps.rb +85 -0
  25. data/features/step_definitions/mount_steps.rb +19 -0
  26. data/features/step_definitions/store_steps.rb +18 -0
  27. data/features/support/activerecord.rb +30 -0
  28. data/features/support/datamapper.rb +7 -0
  29. data/features/support/env.rb +22 -0
  30. data/features/versions_basics.feature +50 -0
  31. data/features/versions_nested_versions.feature +70 -0
  32. data/features/versions_overridden_filename.feature +51 -0
  33. data/features/versions_overriden_store_dir.feature +41 -0
  34. data/lib/carrierwave.rb +98 -0
  35. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  36. data/lib/carrierwave/core_ext/blank.rb +46 -0
  37. data/lib/carrierwave/core_ext/file.rb +11 -0
  38. data/lib/carrierwave/core_ext/inheritable_attributes.rb +108 -0
  39. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  40. data/lib/carrierwave/mount.rb +359 -0
  41. data/lib/carrierwave/orm/activerecord.rb +73 -0
  42. data/lib/carrierwave/orm/datamapper.rb +27 -0
  43. data/lib/carrierwave/orm/mongoid.rb +23 -0
  44. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  45. data/lib/carrierwave/orm/sequel.rb +45 -0
  46. data/lib/carrierwave/processing/image_science.rb +101 -0
  47. data/lib/carrierwave/processing/mini_magick.rb +265 -0
  48. data/lib/carrierwave/processing/rmagick.rb +282 -0
  49. data/lib/carrierwave/sanitized_file.rb +273 -0
  50. data/lib/carrierwave/storage/abstract.rb +30 -0
  51. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  52. data/lib/carrierwave/storage/file.rb +48 -0
  53. data/lib/carrierwave/storage/grid_fs.rb +97 -0
  54. data/lib/carrierwave/storage/right_s3.rb +3 -0
  55. data/lib/carrierwave/storage/s3.rb +206 -0
  56. data/lib/carrierwave/test/matchers.rb +128 -0
  57. data/lib/carrierwave/uploader.rb +44 -0
  58. data/lib/carrierwave/uploader/cache.rb +145 -0
  59. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  60. data/lib/carrierwave/uploader/configuration.rb +132 -0
  61. data/lib/carrierwave/uploader/default_url.rb +19 -0
  62. data/lib/carrierwave/uploader/download.rb +59 -0
  63. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  64. data/lib/carrierwave/uploader/mountable.rb +39 -0
  65. data/lib/carrierwave/uploader/processing.rb +83 -0
  66. data/lib/carrierwave/uploader/proxy.rb +62 -0
  67. data/lib/carrierwave/uploader/remove.rb +22 -0
  68. data/lib/carrierwave/uploader/store.rb +89 -0
  69. data/lib/carrierwave/uploader/url.rb +33 -0
  70. data/lib/carrierwave/uploader/versions.rb +146 -0
  71. data/merb_generators/uploader_generator.rb +22 -0
  72. data/rails_generators/uploader/USAGE +2 -0
  73. data/rails_generators/uploader/templates/uploader.rb +47 -0
  74. data/rails_generators/uploader/uploader_generator.rb +21 -0
  75. data/script/console +10 -0
  76. data/script/destroy +14 -0
  77. data/script/generate +14 -0
  78. data/spec/compatibility/paperclip_spec.rb +52 -0
  79. data/spec/fixtures/bork.txt +1 -0
  80. data/spec/fixtures/landscape.jpg +0 -0
  81. data/spec/fixtures/portrait.jpg +0 -0
  82. data/spec/fixtures/test.jpeg +1 -0
  83. data/spec/fixtures/test.jpg +1 -0
  84. data/spec/mount_spec.rb +538 -0
  85. data/spec/orm/activerecord_spec.rb +271 -0
  86. data/spec/orm/datamapper_spec.rb +168 -0
  87. data/spec/orm/mongoid_spec.rb +202 -0
  88. data/spec/orm/mongomapper_spec.rb +202 -0
  89. data/spec/orm/sequel_spec.rb +183 -0
  90. data/spec/processing/image_science_spec.rb +56 -0
  91. data/spec/processing/mini_magick_spec.rb +76 -0
  92. data/spec/processing/rmagick_spec.rb +75 -0
  93. data/spec/sanitized_file_spec.rb +623 -0
  94. data/spec/spec_helper.rb +92 -0
  95. data/spec/storage/cloudfiles_spec.rb +78 -0
  96. data/spec/storage/grid_fs_spec.rb +83 -0
  97. data/spec/storage/s3_spec.rb +118 -0
  98. data/spec/uploader/cache_spec.rb +209 -0
  99. data/spec/uploader/configuration_spec.rb +105 -0
  100. data/spec/uploader/default_url_spec.rb +85 -0
  101. data/spec/uploader/download_spec.rb +75 -0
  102. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  103. data/spec/uploader/mountable_spec.rb +33 -0
  104. data/spec/uploader/paths_spec.rb +22 -0
  105. data/spec/uploader/processing_spec.rb +73 -0
  106. data/spec/uploader/proxy_spec.rb +54 -0
  107. data/spec/uploader/remove_spec.rb +70 -0
  108. data/spec/uploader/store_spec.rb +264 -0
  109. data/spec/uploader/url_spec.rb +102 -0
  110. data/spec/uploader/versions_spec.rb +298 -0
  111. metadata +433 -0
@@ -0,0 +1,146 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Versions
6
+
7
+ depends_on CarrierWave::Uploader::Callbacks
8
+
9
+ setup do
10
+ after :cache, :cache_versions!
11
+ after :store, :store_versions!
12
+ after :remove, :remove_versions!
13
+ after :retrieve_from_cache, :retrieve_versions_from_cache!
14
+ after :retrieve_from_store, :retrieve_versions_from_store!
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ def version_names
20
+ @version_names ||= []
21
+ end
22
+
23
+ ##
24
+ # Adds a new version to this uploader
25
+ #
26
+ # === Parameters
27
+ #
28
+ # [name (#to_sym)] name of the version
29
+ # [&block (Proc)] a block to eval on this version of the uploader
30
+ #
31
+ def version(name, &block)
32
+ name = name.to_sym
33
+ unless versions[name]
34
+ versions[name] = Class.new(self)
35
+ versions[name].version_names.push(*version_names)
36
+ versions[name].version_names.push(name)
37
+ class_eval <<-RUBY
38
+ def #{name}
39
+ versions[:#{name}]
40
+ end
41
+ RUBY
42
+ end
43
+ versions[name].class_eval(&block) if block
44
+ versions[name]
45
+ end
46
+
47
+ ##
48
+ # === Returns
49
+ #
50
+ # [Hash{Symbol => Class}] a list of versions available for this uploader
51
+ #
52
+ def versions
53
+ @versions ||= {}
54
+ end
55
+
56
+ end # ClassMethods
57
+
58
+ ##
59
+ # Returns a hash mapping the name of each version of the uploader to an instance of it
60
+ #
61
+ # === Returns
62
+ #
63
+ # [Hash{Symbol => CarrierWave::Uploader}] a list of uploader instances
64
+ #
65
+ def versions
66
+ return @versions if @versions
67
+ @versions = {}
68
+ self.class.versions.each do |name, klass|
69
+ @versions[name] = klass.new(model, mounted_as)
70
+ end
71
+ @versions
72
+ end
73
+
74
+ ##
75
+ # === Returns
76
+ #
77
+ # [String] the name of this version of the uploader
78
+ #
79
+ def version_name
80
+ self.class.version_names.join('_').to_sym unless self.class.version_names.blank?
81
+ end
82
+
83
+ ##
84
+ # When given a version name as a parameter, will return the url for that version
85
+ # This also works with nested versions.
86
+ #
87
+ # === Example
88
+ #
89
+ # my_uploader.url # => /path/to/my/uploader.gif
90
+ # my_uploader.url(:thumb) # => /path/to/my/thumb_uploader.gif
91
+ # my_uploader.url(:thumb, :small) # => /path/to/my/thumb_small_uploader.gif
92
+ #
93
+ # === Parameters
94
+ #
95
+ # [*args (Symbol)] any number of versions
96
+ #
97
+ # === Returns
98
+ #
99
+ # [String] the location where this file is accessible via a url
100
+ #
101
+ def url(*args)
102
+ if(args.first)
103
+ raise ArgumentError, "Version #{args.first} doesn't exist!" if versions[args.first.to_sym].nil?
104
+ # recursively proxy to version
105
+ versions[args.first.to_sym].url(*args[1..-1])
106
+ else
107
+ super()
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ def full_filename(for_file)
114
+ [version_name, super(for_file)].compact.join('_')
115
+ end
116
+
117
+ def full_original_filename
118
+ [version_name, super].compact.join('_')
119
+ end
120
+
121
+ def cache_versions!(new_file)
122
+ versions.each do |name, v|
123
+ v.send(:cache_id=, cache_id)
124
+ v.cache!(new_file)
125
+ end
126
+ end
127
+
128
+ def store_versions!(new_file)
129
+ versions.each { |name, v| v.store!(new_file) }
130
+ end
131
+
132
+ def remove_versions!
133
+ versions.each { |name, v| v.remove! }
134
+ end
135
+
136
+ def retrieve_versions_from_cache!(cache_name)
137
+ versions.each { |name, v| v.retrieve_from_cache!(cache_name) }
138
+ end
139
+
140
+ def retrieve_versions_from_store!(identifier)
141
+ versions.each { |name, v| v.retrieve_from_store!(identifier) }
142
+ end
143
+
144
+ end # Versions
145
+ end # Uploader
146
+ end # CarrierWave
@@ -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,2 @@
1
+ Description:
2
+ Generates a skeleton for a new uploader.
@@ -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 URL as a default if there hasn't been a file uploaded
20
+ # def default_url
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" if original_filename
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
@@ -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,52 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ require 'carrierwave/orm/activerecord'
6
+
7
+ module Rails
8
+ def self.root
9
+ File.expand_path(File.join('..'), File.dirname(__FILE__))
10
+ end
11
+ def self.env
12
+ "test"
13
+ end
14
+ end unless defined?(Rails)
15
+
16
+ describe CarrierWave::Compatibility::Paperclip do
17
+
18
+ before do
19
+ @uploader_class = Class.new(CarrierWave::Uploader::Base) do
20
+ include CarrierWave::Compatibility::Paperclip
21
+ end
22
+ @model = mock('a model')
23
+ @model.stub!(:id).and_return(23)
24
+ @uploader = @uploader_class.new(@model, :monkey)
25
+ end
26
+
27
+ after do
28
+ FileUtils.rm_rf(public_path)
29
+ end
30
+
31
+ describe '#store_path' do
32
+ it "should mimics paperclip default" do
33
+ @uploader.store_path("monkey.png").should == CarrierWave::Uploader::Base.root + "/system/monkeys/23/original/monkey.png"
34
+ end
35
+
36
+ it "should interpolate the root path" do
37
+ @uploader.stub!(:paperclip_path).and_return(":rails_root/foo/bar")
38
+ @uploader.store_path("monkey.png").should == Rails.root + "/foo/bar"
39
+ end
40
+
41
+ it "should interpolate the attachment" do
42
+ @uploader.stub!(:paperclip_path).and_return("/foo/:attachment/bar")
43
+ @uploader.store_path("monkey.png").should == "/foo/monkeys/bar"
44
+ end
45
+
46
+ it "should interpolate the id" do
47
+ @uploader.stub!(:paperclip_path).and_return("/foo/:id/bar")
48
+ @uploader.store_path("monkey.png").should == "/foo/23/bar"
49
+ end
50
+ end
51
+
52
+ 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
@@ -0,0 +1,538 @@
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 = stub_file('test.jpg')
35
+ @instance.image.should be_an_instance_of(@uploader)
36
+ end
37
+
38
+ it "should inherit uploaders to subclasses" do
39
+ @subclass = Class.new(@class)
40
+ @subclass_instance = @subclass.new
41
+ @subclass_instance.image = stub_file('test.jpg')
42
+ @subclass_instance.image.should be_an_instance_of(@uploader)
43
+ end
44
+
45
+ describe '#image' do
46
+
47
+ it "should return a blank uploader when nothing has been assigned" do
48
+ @instance.should_receive(:read_uploader).with(:image).twice.and_return(nil)
49
+ @instance.image.should be_an_instance_of(@uploader)
50
+ @instance.image.should be_blank
51
+ end
52
+
53
+ it "should return a blank uploader when an empty string has been assigned" do
54
+ @instance.should_receive(:read_uploader).with(:image).twice.and_return('')
55
+ @instance.image.should be_an_instance_of(@uploader)
56
+ @instance.image.should be_blank
57
+ end
58
+
59
+ it "should retrieve a file from the storage if a value is stored in the database" do
60
+ @instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
61
+ @instance.image.should be_an_instance_of(@uploader)
62
+ end
63
+
64
+ it "should set the path to the store dir" do
65
+ @instance.should_receive(:read_uploader).with(:image).at_least(:once).and_return('test.jpg')
66
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
67
+ end
68
+
69
+ end
70
+
71
+ describe '#image=' do
72
+
73
+ it "should cache a file" do
74
+ @instance.image = stub_file('test.jpg')
75
+ @instance.image.should be_an_instance_of(@uploader)
76
+ end
77
+
78
+ it "should copy a file into into the cache directory" do
79
+ @instance.image = stub_file('test.jpg')
80
+ @instance.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
81
+ end
82
+
83
+ it "should do nothing when nil is assigned" do
84
+ @instance.should_not_receive(:write_uploader)
85
+ @instance.image = nil
86
+ end
87
+
88
+ it "should do nothing when an empty string is assigned" do
89
+ @instance.should_not_receive(:write_uploader)
90
+ @instance.image = stub_file('test.jpg')
91
+ end
92
+
93
+ it "should fail silently if the image fails an integrity check" do
94
+ @uploader.class_eval do
95
+ def extension_white_list
96
+ %(txt)
97
+ end
98
+ end
99
+ @instance.image = stub_file('test.jpg')
100
+ @instance.image.should be_blank
101
+ end
102
+
103
+ it "should fail silently if the image fails to be processed" do
104
+ @uploader.class_eval do
105
+ process :monkey
106
+ def monkey
107
+ raise CarrierWave::ProcessingError, "Ohh noez!"
108
+ end
109
+ end
110
+ @instance.image = stub_file('test.jpg')
111
+ end
112
+
113
+ end
114
+
115
+ describe '#image?' do
116
+
117
+ it "should be false when nothing has been assigned" do
118
+ @instance.should_receive(:read_uploader).with(:image).and_return(nil)
119
+ @instance.image?.should be_false
120
+ end
121
+
122
+ it "should be false when an empty string has been assigned" do
123
+ @instance.should_receive(:read_uploader).with(:image).and_return('')
124
+ @instance.image?.should be_false
125
+ end
126
+
127
+ it "should be true when a file has been cached" do
128
+ @instance.image = stub_file('test.jpg')
129
+ @instance.image?.should be_true
130
+ end
131
+
132
+ end
133
+
134
+ describe '#image_url' do
135
+
136
+ it "should return nil when nothing has been assigned" do
137
+ @instance.should_receive(:read_uploader).with(:image).and_return(nil)
138
+ @instance.image_url.should be_nil
139
+ end
140
+
141
+ it "should return nil when an empty string has been assigned" do
142
+ @instance.should_receive(:read_uploader).with(:image).and_return('')
143
+ @instance.image_url.should be_nil
144
+ end
145
+
146
+ it "should get the url from a retrieved file" do
147
+ @instance.should_receive(:read_uploader).at_least(:once).with(:image).and_return('test.jpg')
148
+ @instance.image_url.should == '/uploads/test.jpg'
149
+ end
150
+
151
+ it "should get the url from a cached file" do
152
+ @instance.image = stub_file('test.jpg')
153
+ @instance.image_url.should =~ %r{uploads/tmp/[\d\-]+/test.jpg}
154
+ end
155
+
156
+ it "should get the url from a cached file's version" do
157
+ @uploader.version(:thumb)
158
+ @instance.image = stub_file('test.jpg')
159
+ @instance.image_url(:thumb).should =~ %r{uploads/tmp/[\d\-]+/thumb_test.jpg}
160
+ end
161
+
162
+ end
163
+
164
+ describe '#image_cache' do
165
+
166
+ before do
167
+ @instance.stub!(:write_uploader)
168
+ @instance.stub!(:read_uploader).and_return(nil)
169
+ end
170
+
171
+ it "should return nil when nothing has been assigned" do
172
+ @instance.image_cache.should be_nil
173
+ end
174
+
175
+ it "should be nil when a file has been stored" do
176
+ @instance.image = stub_file('test.jpg')
177
+ @instance.image.store!
178
+ @instance.image_cache.should be_nil
179
+ end
180
+
181
+ it "should be the cache name when a file has been cached" do
182
+ @instance.image = stub_file('test.jpg')
183
+ @instance.image_cache.should =~ %r(^[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}/test\.jpg$)
184
+ end
185
+
186
+ end
187
+
188
+ describe '#image_cache=' do
189
+
190
+ before do
191
+ @instance.stub!(:write_uploader)
192
+ @instance.stub!(:read_uploader).and_return(nil)
193
+ CarrierWave::SanitizedFile.new(file_path('test.jpg')).copy_to(public_path('uploads/tmp/19990512-1202-123-1234/test.jpg'))
194
+ end
195
+
196
+ it "should do nothing when nil is assigned" do
197
+ @instance.image_cache = nil
198
+ @instance.image.should be_blank
199
+ end
200
+
201
+ it "should do nothing when an empty string is assigned" do
202
+ @instance.image_cache = ''
203
+ @instance.image.should be_blank
204
+ end
205
+
206
+ it "retrieve from cache when a cache name is assigned" do
207
+ @instance.image_cache = '19990512-1202-123-1234/test.jpg'
208
+ @instance.image.current_path.should == public_path('uploads/tmp/19990512-1202-123-1234/test.jpg')
209
+ end
210
+
211
+ it "should not write over a previously assigned file" do
212
+ @instance.image = stub_file('test.jpg')
213
+ @instance.image_cache = '19990512-1202-123-1234/monkey.jpg'
214
+ @instance.image.current_path.should =~ /test.jpg$/
215
+ end
216
+ end
217
+
218
+ describe '#remote_image_url' do
219
+ before do
220
+ response = mock('HTTP Response')
221
+ response.stub!(:body).and_return('Response Body')
222
+ Net::HTTP.stub!(:get_response).and_return(response)
223
+ end
224
+
225
+ it "should return nil" do
226
+ @instance.remote_image_url.should be_nil
227
+ end
228
+
229
+ it "should return previously cached URL" do
230
+ @instance.remote_image_url = 'http://www.example.com/funky/monkey.png'
231
+ @instance.remote_image_url.should == 'http://www.example.com/funky/monkey.png'
232
+ end
233
+ end
234
+
235
+ describe '#remote_image_url=' do
236
+ before do
237
+ response = mock('HTTP Response')
238
+ response.stub!(:body).and_return('Response Body')
239
+ Net::HTTP.stub!(:get_response).and_return(response)
240
+ end
241
+
242
+ it "should do nothing when nil is assigned" do
243
+ @instance.remote_image_url = nil
244
+ @instance.image.should be_blank
245
+ end
246
+
247
+ it "should do nothing when an empty string is assigned" do
248
+ @instance.remote_image_url = ''
249
+ @instance.image.should be_blank
250
+ end
251
+
252
+ it "retrieve from cache when a cache name is assigned" do
253
+ @instance.remote_image_url = 'http://www.example.com/funky/monkey.png'
254
+ @instance.image.current_path.should =~ /monkey.png$/
255
+ end
256
+
257
+ it "should not write over a previously assigned file" do
258
+ @instance.image = stub_file('test.jpg')
259
+ @instance.remote_image_url = '19990512-1202-123-1234/monkey.jpg'
260
+ @instance.image.current_path.should =~ /test.jpg$/
261
+ end
262
+ end
263
+
264
+ describe '#store_image!' do
265
+
266
+ before do
267
+ @instance.stub!(:write_uploader)
268
+ @instance.stub!(:read_uploader).and_return(nil)
269
+ end
270
+
271
+ it "should do nothing when no file has been uploaded" do
272
+ @instance.store_image!
273
+ @instance.image.should be_blank
274
+ end
275
+
276
+ it "store an assigned file" do
277
+ @instance.image = stub_file('test.jpg')
278
+ @instance.store_image!
279
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
280
+ end
281
+
282
+ it "should remove an uploaded file when remove_image? returns true" do
283
+ @instance.image = stub_file('test.jpg')
284
+ path = @instance.image.current_path
285
+ @instance.remove_image = true
286
+ @instance.store_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
+ before do
295
+ @instance.stub!(:write_uploader)
296
+ @instance.stub!(:read_uploader).and_return(nil)
297
+ end
298
+
299
+ it "should do nothing when no file has been uploaded" do
300
+ @instance.remove_image!
301
+ @instance.image.should be_blank
302
+ end
303
+
304
+ it "should remove an uploaded file" do
305
+ @instance.image = stub_file('test.jpg')
306
+ path = @instance.image.current_path
307
+ @instance.remove_image!
308
+ @instance.image.should be_blank
309
+ File.exist?(path).should be_false
310
+ end
311
+ end
312
+
313
+ describe '#remove_image' do
314
+
315
+ it "should store a value" do
316
+ @instance.remove_image = true
317
+ @instance.remove_image.should be_true
318
+ end
319
+
320
+ end
321
+
322
+ describe '#remove_image?' do
323
+
324
+ it "should be true when the value is truthy" do
325
+ @instance.remove_image = true
326
+ @instance.remove_image?.should be_true
327
+ end
328
+
329
+ it "should be false when the value is falsey" do
330
+ @instance.remove_image = false
331
+ @instance.remove_image?.should be_false
332
+ end
333
+
334
+ it "should be false when the value is ''" do
335
+ @instance.remove_image = ''
336
+ @instance.remove_image?.should be_false
337
+ end
338
+
339
+ it "should be false when the value is '0'" do
340
+ @instance.remove_image = '0'
341
+ @instance.remove_image?.should be_false
342
+ end
343
+
344
+ it "should be false when the value is 'false'" do
345
+ @instance.remove_image = 'false'
346
+ @instance.remove_image?.should be_false
347
+ end
348
+
349
+ end
350
+
351
+ describe '#image_integrity_error' do
352
+
353
+ it "should be nil by default" do
354
+ @instance.image_integrity_error.should be_nil
355
+ end
356
+
357
+ it "should be nil after a file is cached" do
358
+ @instance.image = stub_file('test.jpg')
359
+ @instance.image_integrity_error.should be_nil
360
+ end
361
+
362
+ it "should be an error instance after an integrity check has failed" do
363
+ @uploader.class_eval do
364
+ def extension_white_list
365
+ %(txt)
366
+ end
367
+ end
368
+ @instance.image = stub_file('test.jpg')
369
+ @instance.image_integrity_error.should be_an_instance_of(CarrierWave::IntegrityError)
370
+ end
371
+ end
372
+
373
+ describe '#image_processing_error' do
374
+
375
+ it "should be nil by default" do
376
+ @instance.image_processing_error.should be_nil
377
+ end
378
+
379
+ it "should be nil after a file is cached" do
380
+ @instance.image = stub_file('test.jpg')
381
+ @instance.image_processing_error.should be_nil
382
+ end
383
+
384
+ it "should be an error instance after an integrity check has failed" do
385
+ @uploader.class_eval do
386
+ process :monkey
387
+ def monkey
388
+ raise CarrierWave::ProcessingError, "Ohh noez!"
389
+ end
390
+ end
391
+ @instance.image = stub_file('test.jpg')
392
+ @instance.image_processing_error.should be_an_instance_of(CarrierWave::ProcessingError)
393
+ end
394
+ end
395
+
396
+ describe '#write_image_identifier' do
397
+ it "should write to the column" do
398
+ @instance.should_receive(:write_uploader).with(:image, "test.jpg")
399
+ @instance.image = stub_file('test.jpg')
400
+ @instance.write_image_identifier
401
+ end
402
+
403
+ it "should remove from the column when remove_image is true" do
404
+ @instance.image = stub_file('test.jpg')
405
+ @instance.store_image!
406
+ @instance.remove_image = true
407
+ @instance.should_receive(:write_uploader).with(:image, "")
408
+ @instance.write_image_identifier
409
+ end
410
+ end
411
+
412
+ end
413
+
414
+ describe '#mount_uploader with a block' do
415
+
416
+ before do
417
+ @class = Class.new
418
+ @class.send(:extend, CarrierWave::Mount)
419
+ @class.mount_uploader(:image) do
420
+ def monkey
421
+ 'blah'
422
+ end
423
+ end
424
+ @instance = @class.new
425
+ end
426
+
427
+ describe '#image' do
428
+
429
+ before do
430
+ @instance.stub!(:read_uploader).and_return('test.jpg')
431
+ end
432
+
433
+ it "should return an instance of a subclass of CarrierWave::Uploader::Base" do
434
+ @instance.image.should be_a(CarrierWave::Uploader::Base)
435
+ end
436
+
437
+ it "should set the path to the store dir" do
438
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
439
+ end
440
+
441
+ it "should apply any custom modifications" do
442
+ @instance.image.monkey.should == "blah"
443
+ end
444
+
445
+ end
446
+
447
+ end
448
+
449
+ describe '#mount_uploader with :ignore_integrity_errors => false' do
450
+
451
+ before do
452
+ @class = Class.new
453
+ @class.send(:extend, CarrierWave::Mount)
454
+
455
+ @uploader = Class.new(CarrierWave::Uploader::Base)
456
+
457
+ @class.mount_uploader(:image, @uploader, :ignore_integrity_errors => false)
458
+ @instance = @class.new
459
+ end
460
+
461
+ it "should raise an error if the image fails an integrity check" do
462
+ @uploader.class_eval do
463
+ def extension_white_list
464
+ %(txt)
465
+ end
466
+ end
467
+ running {
468
+ @instance.image = stub_file('test.jpg')
469
+ }.should raise_error(CarrierWave::IntegrityError)
470
+ end
471
+
472
+ end
473
+
474
+ describe '#mount_uploader with :ignore_processing_errors => false' do
475
+
476
+ before do
477
+ @class = Class.new
478
+ @class.send(:extend, CarrierWave::Mount)
479
+
480
+ @uploader = Class.new(CarrierWave::Uploader::Base)
481
+
482
+ @class.mount_uploader(:image, @uploader, :ignore_processing_errors => false)
483
+ @instance = @class.new
484
+ end
485
+
486
+ it "should raise an error if the image fails to be processed" do
487
+ @uploader.class_eval do
488
+ process :monkey
489
+ def monkey
490
+ raise CarrierWave::ProcessingError, "Ohh noez!"
491
+ end
492
+ end
493
+ running {
494
+ @instance.image = stub_file('test.jpg')
495
+ }.should raise_error(CarrierWave::ProcessingError)
496
+ end
497
+
498
+ end
499
+
500
+ describe '#mount_uploader with :mount_on => :monkey' do
501
+
502
+
503
+ before do
504
+ @class = Class.new
505
+ @class.send(:extend, CarrierWave::Mount)
506
+
507
+ @uploader = Class.new(CarrierWave::Uploader::Base)
508
+
509
+ @class.mount_uploader(:image, @uploader, :mount_on => :monkey)
510
+ @instance = @class.new
511
+ end
512
+
513
+ describe '#image' do
514
+ it "should retrieve a file from the storage if a value is stored in the database" do
515
+ @instance.should_receive(:read_uploader).at_least(:once).with(:monkey).twice.and_return('test.jpg')
516
+ @instance.image.should be_an_instance_of(@uploader)
517
+ @instance.image.current_path.should == public_path('uploads/test.jpg')
518
+ end
519
+ end
520
+
521
+ describe '#write_image_identifier' do
522
+ it "should write to the given column" do
523
+ @instance.should_receive(:write_uploader).with(:monkey, "test.jpg")
524
+ @instance.image = stub_file('test.jpg')
525
+ @instance.write_image_identifier
526
+ end
527
+
528
+ it "should remove from the given column when remove_image is true" do
529
+ @instance.image = stub_file('test.jpg')
530
+ @instance.store_image!
531
+ @instance.remove_image = true
532
+ @instance.should_receive(:write_uploader).with(:monkey, "")
533
+ @instance.write_image_identifier
534
+ end
535
+ end
536
+ end
537
+
538
+ end