Skip to content
Browse files

selectively reprocessing attachment styles

  • Loading branch information...
1 parent cf41b01 commit 338fc35300fd6841a464c13b185c2b35d2d157f0 yar committed with jyurek Mar 7, 2011
Showing with 82 additions and 8 deletions.
  1. +10 −8 lib/paperclip/attachment.rb
  2. +72 −0 test/integration_test.rb
View
18 lib/paperclip/attachment.rb
@@ -241,15 +241,15 @@ def self.interpolations
# in the paperclip:refresh rake task and that's it. It will regenerate all
# thumbnails forcefully, by reobtaining the original file and going through
# the post-process again.
- def reprocess!
+ def reprocess!(*style_args)
new_original = Tempfile.new("paperclip-reprocess")
new_original.binmode
if old_original = to_file(:original)
new_original.write( old_original.respond_to?(:get) ? old_original.get : old_original.read )
new_original.rewind
@queued_for_write = { :original => new_original }
- post_process
+ post_process(*style_args)
old_original.close if old_original.respond_to?(:close)
@@ -324,21 +324,23 @@ def extra_options_for(style) #:nodoc:
[ style_options, all_options ].compact.join(" ")
end
- def post_process #:nodoc:
+ def post_process(*style_args) #:nodoc:
return if @queued_for_write[:original].nil?
instance.run_paperclip_callbacks(:post_process) do
instance.run_paperclip_callbacks(:"#{name}_post_process") do
- post_process_styles
+ post_process_styles(*style_args)
end
end
end
- def post_process_styles #:nodoc:
+ def post_process_styles(*style_args) #:nodoc:
styles.each do |name, style|
begin
- raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
- @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
- Paperclip.processor(processor).make(file, style.processor_options, self)
+ if style_args.empty? || style_args.include?(name)
+ raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
+ @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
+ Paperclip.processor(processor).make(file, style.processor_options, self)
+ end
end
rescue PaperclipError => e
log("An error was received while processing: #{e.inspect}")
View
72 test/integration_test.rb
@@ -69,6 +69,78 @@ class IntegrationTest < Test::Unit::TestCase
end
end
+ context "Attachment" do
+ setup do
+ @thumb_path = "./test/../public/system/avatars/1/thumb/5k.png"
+ File.delete(@thumb_path) if File.exists?(@thumb_path)
+ rebuild_model :styles => { :thumb => "50x50#" }
+ @dummy = Dummy.new
+ @file = File.new(File.join(File.dirname(__FILE__),
+ "fixtures",
+ "5k.png"), 'rb')
+
+ end
+
+ teardown { @file.close }
+
+ should "not create the thumbnails upon saving when post-processing is disabled" do
+ @dummy.avatar.post_processing = false
+ @dummy.avatar = @file
+ assert @dummy.save
+ assert !File.exists?(@thumb_path)
+ end
+
+ should "create the thumbnails upon saving when post_processing is enabled" do
+ @dummy.avatar.post_processing = true
+ @dummy.avatar = @file
+ assert @dummy.save
+ assert File.exists?(@thumb_path)
+ end
+ end
+
+ context "Attachment with no generated thumbnails" do
+ setup do
+ @thumb_small_path = "./test/../public/system/avatars/1/thumb_small/5k.png"
+ @thumb_large_path = "./test/../public/system/avatars/1/thumb_large/5k.png"
+ File.delete(@thumb_small_path) if File.exists?(@thumb_small_path)
+ File.delete(@thumb_large_path) if File.exists?(@thumb_large_path)
+ rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" }
+ @dummy = Dummy.new
+ @file = File.new(File.join(File.dirname(__FILE__),
+ "fixtures",
+ "5k.png"), 'rb')
+
+ @dummy.avatar.post_processing = false
+ @dummy.avatar = @file
+ assert @dummy.save
+ @dummy.avatar.post_processing = true
+ end
+
+ teardown { @file.close }
+
+ should "allow us to create all thumbnails in one go" do
+ assert !File.exists?(@thumb_small_path)
+ assert !File.exists?(@thumb_large_path)
+
+ @dummy.avatar.reprocess!
+
+ assert File.exists?(@thumb_small_path)
+ assert File.exists?(@thumb_large_path)
+ end
+
+ should "allow us to selectively create each thumbnail" do
+ assert !File.exists?(@thumb_small_path)
+ assert !File.exists?(@thumb_large_path)
+
+ @dummy.avatar.reprocess! :thumb_small
+ assert File.exists?(@thumb_small_path)
+ assert !File.exists?(@thumb_large_path)
+
+ @dummy.avatar.reprocess! :thumb_large
+ assert File.exists?(@thumb_large_path)
+ end
+ end
+
context "A model that modifies its original" do
setup do
rebuild_model :styles => { :original => "2x2#" }

0 comments on commit 338fc35

Please sign in to comment.
Something went wrong with that request. Please try again.