Commit 9dc9f71f by Jon Yurek

Added before and after post_process hooks

parent 18c0246c
...@@ -150,6 +150,9 @@ module Paperclip ...@@ -150,6 +150,9 @@ module Paperclip
after_save :save_attached_files after_save :save_attached_files
before_destroy :destroy_attached_files before_destroy :destroy_attached_files
define_callbacks :before_post_process, :after_post_process
define_callbacks :"before_#{name}_post_process", :"after_#{name}_post_process"
define_method name do |*args| define_method name do |*args|
a = attachment_for(name) a = attachment_for(name)
(args.length > 0) ? a.to_s(args.first) : a (args.length > 0) ? a.to_s(args.first) : a
......
...@@ -15,7 +15,7 @@ module Paperclip ...@@ -15,7 +15,7 @@ module Paperclip
} }
end end
attr_reader :name, :instance, :styles, :default_style, :convert_options attr_reader :name, :instance, :styles, :default_style, :convert_options, :queued_for_write
# Creates an Attachment object. +name+ is the name of the attachment, +instance+ is the # Creates an Attachment object. +name+ is the name of the attachment, +instance+ is the
# ActiveRecord object instance it's attached to, and +options+ is the same as the hash # ActiveRecord object instance it's attached to, and +options+ is the same as the hash
...@@ -272,6 +272,8 @@ module Paperclip ...@@ -272,6 +272,8 @@ module Paperclip
def post_process #:nodoc: def post_process #:nodoc:
return if @queued_for_write[:original].nil? return if @queued_for_write[:original].nil?
return if callback(:before_post_process) == false
return if callback(:"before_#{name}_post_process") == false
logger.info("[paperclip] Post-processing #{name}") logger.info("[paperclip] Post-processing #{name}")
@styles.each do |name, args| @styles.each do |name, args|
begin begin
...@@ -286,6 +288,12 @@ module Paperclip ...@@ -286,6 +288,12 @@ module Paperclip
(@errors[:processing] ||= []) << e.message if @whiny_thumbnails (@errors[:processing] ||= []) << e.message if @whiny_thumbnails
end end
end end
callback(:"after_#{name}_post_process")
callback(:after_post_process)
end
def callback which
instance.run_callbacks(which, @queued_for_write){|result, obj| result == false }
end end
def interpolate pattern, style = default_style #:nodoc: def interpolate pattern, style = default_style #:nodoc:
......
...@@ -147,29 +147,82 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -147,29 +147,82 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
context "Assigning an attachment with post_process hooks" do
setup do
rebuild_model :styles => { :something => "100x100#" }
Dummy.class_eval do
before_avatar_post_process :do_before_avatar
after_avatar_post_process :do_after_avatar
before_post_process :do_before_all
after_post_process :do_after_all
def do_before_avatar; end
def do_after_avatar; end
def do_before_all; end
def do_after_all; end
end
@file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
@file.stubs(:to_tempfile).returns(@file)
@dummy = Dummy.new
Paperclip::Thumbnail.stubs(:make).returns(@file)
@attachment = @dummy.avatar
end
should "call the defined callbacks when assigned" do
@dummy.expects(:do_before_avatar).with()
@dummy.expects(:do_after_avatar).with()
@dummy.expects(:do_before_all).with()
@dummy.expects(:do_after_all).with()
Paperclip::Thumbnail.expects(:make).returns(@file)
@dummy.avatar = @file
end
should "not cancel the processing if a before_post_process returns nil" do
@dummy.expects(:do_before_avatar).with().returns(nil)
@dummy.expects(:do_after_avatar).with()
@dummy.expects(:do_before_all).with().returns(nil)
@dummy.expects(:do_after_all).with()
Paperclip::Thumbnail.expects(:make).returns(@file)
@dummy.avatar = @file
end
should "cancel the processing if a before_post_process returns false" do
@dummy.expects(:do_before_avatar).never
@dummy.expects(:do_after_avatar).never
@dummy.expects(:do_before_all).with().returns(false)
@dummy.expects(:do_after_all).never
Paperclip::Thumbnail.expects(:make).never
@dummy.avatar = @file
end
should "cancel the processing if a before_avatar_post_process returns false" do
@dummy.expects(:do_before_avatar).with().returns(false)
@dummy.expects(:do_after_avatar).never
@dummy.expects(:do_before_all).with().returns(true)
@dummy.expects(:do_after_all).never
Paperclip::Thumbnail.expects(:make).never
@dummy.avatar = @file
end
end
context "Assigning an attachment" do context "Assigning an attachment" do
setup do setup do
rebuild_model rebuild_model :styles => { :something => "100x100#" }
@file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
@not_file = mock @file.expects(:original_filename).returns("5k.png\n\n")
@not_file.stubs(:nil?).returns(false) @file.expects(:content_type).returns("image/png\n\n")
@not_file.expects(:to_tempfile).returns(self) @file.stubs(:to_tempfile).returns(@file)
@not_file.expects(:original_filename).returns("filename.png\r\n")
@not_file.expects(:content_type).returns("image/png\r\n")
@not_file.expects(:size).returns(10).times(2)
@dummy = Dummy.new @dummy = Dummy.new
Paperclip::Thumbnail.expects(:make).returns(@file)
@dummy.expects(:run_callbacks).with(:before_avatar_post_process, {:original => @file})
@dummy.expects(:run_callbacks).with(:before_post_process, {:original => @file})
@dummy.expects(:run_callbacks).with(:after_avatar_post_process, {:original => @file, :something => @file})
@dummy.expects(:run_callbacks).with(:after_post_process, {:original => @file, :something => @file})
@attachment = @dummy.avatar @attachment = @dummy.avatar
@attachment.expects(:valid_assignment?).with(@not_file).returns(true) @dummy.avatar = @file
@attachment.expects(:queue_existing_for_delete)
@attachment.expects(:post_process)
@attachment.expects(:valid?).returns(true)
@attachment.expects(:validate)
@dummy.avatar = @not_file
end end
should "strip whitespace from original_filename field" do should "strip whitespace from original_filename field" do
assert_equal "filename.png", @dummy.avatar.original_filename assert_equal "5k.png", @dummy.avatar.original_filename
end end
should "strip whitespace from content_type field" do should "strip whitespace from content_type field" do
......
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