Commit 338fc353 by yar Committed by Jon Yurek

selectively reprocessing attachment styles

parent cf41b01c
...@@ -241,7 +241,7 @@ module Paperclip ...@@ -241,7 +241,7 @@ module Paperclip
# in the paperclip:refresh rake task and that's it. It will regenerate all # in the paperclip:refresh rake task and that's it. It will regenerate all
# thumbnails forcefully, by reobtaining the original file and going through # thumbnails forcefully, by reobtaining the original file and going through
# the post-process again. # the post-process again.
def reprocess! def reprocess!(*style_args)
new_original = Tempfile.new("paperclip-reprocess") new_original = Tempfile.new("paperclip-reprocess")
new_original.binmode new_original.binmode
if old_original = to_file(:original) if old_original = to_file(:original)
...@@ -249,7 +249,7 @@ module Paperclip ...@@ -249,7 +249,7 @@ module Paperclip
new_original.rewind new_original.rewind
@queued_for_write = { :original => new_original } @queued_for_write = { :original => new_original }
post_process post_process(*style_args)
old_original.close if old_original.respond_to?(:close) old_original.close if old_original.respond_to?(:close)
...@@ -324,21 +324,23 @@ module Paperclip ...@@ -324,21 +324,23 @@ module Paperclip
[ style_options, all_options ].compact.join(" ") [ style_options, all_options ].compact.join(" ")
end end
def post_process #:nodoc: def post_process(*style_args) #:nodoc:
return if @queued_for_write[:original].nil? return if @queued_for_write[:original].nil?
instance.run_paperclip_callbacks(:post_process) do instance.run_paperclip_callbacks(:post_process) do
instance.run_paperclip_callbacks(:"#{name}_post_process") do instance.run_paperclip_callbacks(:"#{name}_post_process") do
post_process_styles post_process_styles(*style_args)
end end
end end
end end
def post_process_styles #:nodoc: def post_process_styles(*style_args) #:nodoc:
styles.each do |name, style| styles.each do |name, style|
begin begin
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank? if style_args.empty? || style_args.include?(name)
@queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor| raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
Paperclip.processor(processor).make(file, style.processor_options, self) @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 end
rescue PaperclipError => e rescue PaperclipError => e
log("An error was received while processing: #{e.inspect}") log("An error was received while processing: #{e.inspect}")
......
...@@ -69,6 +69,78 @@ class IntegrationTest < Test::Unit::TestCase ...@@ -69,6 +69,78 @@ class IntegrationTest < Test::Unit::TestCase
end end
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 context "A model that modifies its original" do
setup do setup do
rebuild_model :styles => { :original => "2x2#" } rebuild_model :styles => { :original => "2x2#" }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment