Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #567 from chrisroberts/master

Allow conditionals to accept blocks
  • Loading branch information...
commit 3950ff25fbb9667e48dc720e12439c390e3f2939 2 parents c925725 + 78e7513
@bensie bensie authored
View
4 lib/carrierwave/uploader/processing.rb
@@ -79,7 +79,9 @@ def process(*args)
def process!(new_file=nil)
if enable_processing
self.class.processors.each do |method, args, condition|
- next if condition && !self.send(condition, new_file)
+ if(condition)
+ next if !(condition.respond_to?(:call) ? condition.call(self, :args => args, :method => method, :file => new_file) : self.send(condition, new_file))
+ end
self.send(method, *args)
end
end
View
10 lib/carrierwave/uploader/versions.rb
@@ -191,7 +191,15 @@ def assign_parent_cache_id(file)
def active_versions
versions.select do |name, uploader|
condition = self.class.versions[name][:options][:if]
- not condition or send(condition, file)
+ if(condition)
+ if(condition.respond_to?(:call))
+ condition.call(self, :version => name, :file => false)
+ else
+ send(condition, file)
+ end
+ else
+ true
+ end
end
end
View
18 spec/uploader/processing_spec.rb
@@ -65,6 +65,24 @@
@uploader.process!("test.jpg")
end
+ it "should call the processor if the condition block returns true" do
+ @uploader_class.process :resize => [200, 300], :if => lambda{|record, args| record.true?(args[:file])}
+ @uploader_class.process :fancy, :if => :true?
+ @uploader.should_receive(:true?).with("test.jpg").twice.and_return(true)
+ @uploader.should_receive(:resize).with(200, 300)
+ @uploader.should_receive(:fancy).with()
+ @uploader.process!("test.jpg")
+ end
+
+ it "should not call the processor if the condition block returns false" do
+ @uploader_class.process :resize => [200, 300], :if => lambda{|record, args| record.false?(args[:file])}
+ @uploader_class.process :fancy, :if => :false?
+ @uploader.should_receive(:false?).with("test.jpg").twice.and_return(false)
+ @uploader.should_not_receive(:resize)
+ @uploader.should_not_receive(:fancy)
+ @uploader.process!("test.jpg")
+ end
+
context "with 'enable_processing' set to false" do
it "should not do any processing" do
@uploader_class.enable_processing = false
View
36 spec/uploader/versions_spec.rb
@@ -278,19 +278,35 @@ def rotate
@uploader.store!(@file)
@uploader.thumb.should be_present
@uploader.preview.should be_blank
- end
+ end
- it "should not cache file twice when store! called with a file" do
- @uploader_class.process :banana
- @uploader.thumb.class.process :banana
+ it "should process conditional version if the condition block returns true" do
+ @uploader_class.version(:preview)[:options][:if] = lambda{|record, args| record.true?(args[:file])}
+ @uploader.should_receive(:true?).at_least(:once).and_return(true)
+ @uploader.store!(@file)
+ @uploader.thumb.should be_present
+ @uploader.preview.should be_present
+ end
- @uploader.should_receive(:banana).at_least(:once).at_most(:once).and_return(true)
- @uploader.thumb.should_receive(:banana).at_least(:once).at_most(:once).and_return(true)
+ it "should not process conditional versions if the condition block returns false" do
+ @uploader_class.version(:preview)[:options][:if] = lambda{|record, args| record.false?(args[:file])}
+ @uploader.should_receive(:false?).at_least(:once).and_return(false)
+ @uploader.store!(@file)
+ @uploader.thumb.should be_present
+ @uploader.preview.should be_blank
+ end
+
+ it "should not cache file twice when store! called with a file" do
+ @uploader_class.process :banana
+ @uploader.thumb.class.process :banana
+
+ @uploader.should_receive(:banana).at_least(:once).at_most(:once).and_return(true)
+ @uploader.thumb.should_receive(:banana).at_least(:once).at_most(:once).and_return(true)
- @uploader.store!(@file)
- @uploader.store_path.should == 'uploads/test.jpg'
- @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
- end
+ @uploader.store!(@file)
+ @uploader.store_path.should == 'uploads/test.jpg'
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
+ end
end
describe '#recreate_versions!' do
Please sign in to comment.
Something went wrong with that request. Please try again.