From 78e75139821d2452500a484fc1cc15284b839d48 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 19 Dec 2011 09:23:02 -0800 Subject: [PATCH] Allow conditionals to accept blocks --- lib/carrierwave/uploader/processing.rb | 4 ++- lib/carrierwave/uploader/versions.rb | 10 ++++++- spec/uploader/processing_spec.rb | 18 +++++++++++++ spec/uploader/versions_spec.rb | 36 +++++++++++++++++++------- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/lib/carrierwave/uploader/processing.rb b/lib/carrierwave/uploader/processing.rb index 73994cda6..d3617f4d6 100644 --- a/lib/carrierwave/uploader/processing.rb +++ b/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 diff --git a/lib/carrierwave/uploader/versions.rb b/lib/carrierwave/uploader/versions.rb index 846c1d850..7524f4992 100644 --- a/lib/carrierwave/uploader/versions.rb +++ b/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 diff --git a/spec/uploader/processing_spec.rb b/spec/uploader/processing_spec.rb index 54dcea4f3..5bed8c74e 100644 --- a/spec/uploader/processing_spec.rb +++ b/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 diff --git a/spec/uploader/versions_spec.rb b/spec/uploader/versions_spec.rb index 66a1dbb55..a7d738e30 100644 --- a/spec/uploader/versions_spec.rb +++ b/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