Permalink
Browse files

Allow a new option in versions from which start to process the file.

For performance reasons, when you are working with big files it makes sense, if possible, start processing the file from a small version, instead of using the original one. I.e.:

  version :thumb do
    process resize_to_fill: [280, 280]
  end

  version :small, from_version: :thumb do
    process resize_to_fill: [60, 60]
  end
  • Loading branch information...
1 parent efaafc4 commit fe1de4f3d7b49805f821f77008d246f50cf5fc54 @ferblape ferblape committed Feb 21, 2012
@@ -0,0 +1 @@
+THIS IS A FILE
@@ -9,6 +9,15 @@
@uploader = @klass.new
end
+Given /^a processor method named :upcase$/ do
+ @klass.class_eval do
+ define_method(:upcase) do
+ content = File.read(current_path)
+ File.write(current_path, content.upcase)
+ end
+ end
+end
+
Then /^the contents of the file should be '(.*?)'$/ do |contents|
@uploader.read.chomp.should == contents
end
@@ -45,10 +54,20 @@ def filename
end
end
-Given /^that the uploader class has a version named '(.*?)'$/ do |name|
+Given /^that the uploader class has a version named '([^\']+)'$/ do |name|
@klass.version(name)
end
+Given /^that the uploader class has a version named '([^\']+)' which process '([a-zA-Z0-9\_\?!]*)'$/ do |name, processor_name|
+ @klass.version(name) do
+ process processor_name.to_sym
+ end
+end
+
+Given /^that the uploader class has a version named '([^\']+)' which is based on version '(.*?)'$/ do |name, based_version_name|
+ @klass.version(name, {:from_version => based_version_name.to_sym})
+end
+
Given /^yo dawg, I put a version called '(.*?)' in your version called '(.*?)'$/ do |v2, v1|
@klass.version(v1) do
version(v2)
@@ -0,0 +1,32 @@
+Feature: uploader with file storage and versions with overridden store dir
+ In order to be awesome
+ As a developer using CarrierWave
+ I want to upload files to the filesystem
+
+ Background:
+ Given an uploader class that uses the 'file' storage
+ Given a processor method named :upcase
+ And that the uploader class has a version named 'thumb' which process 'upcase'
+ And that the version 'thumb' has the store_dir overridden to 'public/monkey/llama'
+ And that the uploader class has a version named 'small_thumb' which is based on version 'thumb'
+ And that the version 'small_thumb' has the store_dir overridden to 'public/monkey/toro'
+ And an instance of that class
+
+ Scenario: cache a file and then store it
+ When I cache the file 'fixtures/bork.txt'
+ Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
+ Then there should be a file called 'thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
+ Then there should be a file called 'small_thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
+ And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
+ And the file called 'thumb_bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/upcased_bork.txt'
+ And the file called 'small_thumb_bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/upcased_bork.txt'
+ And there should not be a file at 'public/uploads/bork.txt'
+ And there should not be a file at 'public/monkey/llama/thumb_bork.txt'
+ And there should not be a file at 'public/monkey/toro/small_thumb_bork.txt'
+ When I store the file
+ Then there should be a file at 'public/uploads/bork.txt'
+ Then there should be a file at 'public/monkey/llama/thumb_bork.txt'
+ Then there should be a file at 'public/monkey/toro/small_thumb_bork.txt'
+ And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
+ And the file at 'public/monkey/llama/thumb_bork.txt' should be identical to the file at 'fixtures/upcased_bork.txt'
+ And the file at 'public/monkey/toro/small_thumb_bork.txt' should be identical to the file at 'fixtures/upcased_bork.txt'
@@ -214,8 +214,17 @@ def cache_versions!(new_file)
:filename => new_file.original_filename
active_versions.each do |name, v|
+ # If option :from_version is present, create cache using cached file from
+ # version indicated
+ if self.class.versions[name][:options] &&
+ self.class.versions[name][:options][:from_version]
+ processed_version = SanitizedFile.new :tempfile => versions[self.class.versions[name][:options][:from_version]],
+ :filename => new_file.original_filename
+ v.cache!(processed_version)
+ else
+ v.cache!(processed_parent)
+ end
v.send(:cache_id=, cache_id)
- v.cache!(processed_parent)
end
end

0 comments on commit fe1de4f

Please sign in to comment.