Commit 9cd1f8e1 by Jon Yurek

Use Rails 3 callbacks.

These callbacks are different from pre-rails 3. They do NOT cancel the
after_ if the before_ returns false. They will cancell inner callbacks,
however, but that's it. This is a change in functionality.
parent 3f2b0c0e
...@@ -46,7 +46,7 @@ end ...@@ -46,7 +46,7 @@ end
# documentation for Paperclip::ClassMethods for more useful information. # documentation for Paperclip::ClassMethods for more useful information.
module Paperclip module Paperclip
VERSION = "2.3.1.1" VERSION = "2.3.2"
class << self class << self
# Provides configurability to Paperclip. There are a number of options available, such as: # Provides configurability to Paperclip. There are a number of options available, such as:
...@@ -111,9 +111,6 @@ module Paperclip ...@@ -111,9 +111,6 @@ module Paperclip
def included base #:nodoc: def included base #:nodoc:
base.extend ClassMethods base.extend ClassMethods
unless base.respond_to?(:define_callbacks)
base.send(:include, Paperclip::CallbackCompatability)
end
end end
def processor name #:nodoc: def processor name #:nodoc:
...@@ -222,9 +219,8 @@ module Paperclip ...@@ -222,9 +219,8 @@ 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_paperclip_callbacks :post_process, :"#{name}_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
...@@ -318,6 +314,22 @@ module Paperclip ...@@ -318,6 +314,22 @@ module Paperclip
def attachment_definitions def attachment_definitions
read_inheritable_attribute(:attachment_definitions) read_inheritable_attribute(:attachment_definitions)
end end
private
def define_paperclip_callbacks(*callbacks)
define_callbacks *[callbacks, {:terminator => "result == false"}].flatten
callbacks.each do |callback|
eval <<-end_callbacks
def before_#{callback}(*args, &blk)
set_callback(:#{callback}, :before, *args, &blk)
end
def after_#{callback}(*args, &blk)
set_callback(:#{callback}, :after, *args, &blk)
end
end_callbacks
end
end
end end
module InstanceMethods #:nodoc: module InstanceMethods #:nodoc:
......
...@@ -280,18 +280,11 @@ module Paperclip ...@@ -280,18 +280,11 @@ 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 fire_events(:before) instance.run_callbacks(:post_process) do
post_process_styles instance.run_callbacks(:"#{name}_post_process") do
return if fire_events(:after) post_process_styles
end end
end
def fire_events(which) #:nodoc:
return true if callback(:"#{which}_post_process") == false
return true if callback(:"#{which}_#{name}_post_process") == false
end
def callback which #:nodoc:
instance.run_callbacks(which, @queued_for_write)
end end
def post_process_styles #:nodoc: def post_process_styles #:nodoc:
......
...@@ -362,7 +362,7 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -362,7 +362,7 @@ class AttachmentTest < Test::Unit::TestCase
context "Assigning an attachment with post_process hooks" do context "Assigning an attachment with post_process hooks" do
setup do setup do
rebuild_model :styles => { :something => "100x100#" } rebuild_class :styles => { :something => "100x100#" }
Dummy.class_eval do Dummy.class_eval do
before_avatar_post_process :do_before_avatar before_avatar_post_process :do_before_avatar
after_avatar_post_process :do_after_avatar after_avatar_post_process :do_after_avatar
...@@ -402,16 +402,16 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -402,16 +402,16 @@ class AttachmentTest < Test::Unit::TestCase
@dummy.expects(:do_before_avatar).never @dummy.expects(:do_before_avatar).never
@dummy.expects(:do_after_avatar).never @dummy.expects(:do_after_avatar).never
@dummy.expects(:do_before_all).with().returns(false) @dummy.expects(:do_before_all).with().returns(false)
@dummy.expects(:do_after_all).never @dummy.expects(:do_after_all)
Paperclip::Thumbnail.expects(:make).never Paperclip::Thumbnail.expects(:make).never
@dummy.avatar = @file @dummy.avatar = @file
end end
should "cancel the processing if a before_avatar_post_process returns false" do should "cancel the processing if a before_avatar_post_process returns false" do
@dummy.expects(:do_before_avatar).with().returns(false) @dummy.expects(:do_before_avatar).with().returns(false)
@dummy.expects(:do_after_avatar).never @dummy.expects(:do_after_avatar)
@dummy.expects(:do_before_all).with().returns(true) @dummy.expects(:do_before_all).with().returns(true)
@dummy.expects(:do_after_all).never @dummy.expects(:do_after_all)
Paperclip::Thumbnail.expects(:make).never Paperclip::Thumbnail.expects(:make).never
@dummy.avatar = @file @dummy.avatar = @file
end end
......
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