Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Use Rails 3 callbacks.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jon Yurek committed Apr 26, 2010
1 parent 3f2b0c0 commit 9cd1f8e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
26 changes: 19 additions & 7 deletions lib/paperclip.rb
Expand Up @@ -46,7 +46,7 @@
# documentation for Paperclip::ClassMethods for more useful information.
module Paperclip

VERSION = "2.3.1.1"
VERSION = "2.3.2"

class << self
# Provides configurability to Paperclip. There are a number of options available, such as:
Expand Down Expand Up @@ -111,9 +111,6 @@ def bit_bucket #:nodoc:

def included base #:nodoc:
base.extend ClassMethods
unless base.respond_to?(:define_callbacks)
base.send(:include, Paperclip::CallbackCompatability)
end
end

def processor name #:nodoc:
Expand Down Expand Up @@ -222,9 +219,8 @@ def has_attached_file name, options = {}
after_save :save_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_paperclip_callbacks :post_process, :"#{name}_post_process"

define_method name do |*args|
a = attachment_for(name)
(args.length > 0) ? a.to_s(args.first) : a
Expand Down Expand Up @@ -318,6 +314,22 @@ def validates_attachment_content_type name, options = {}
def attachment_definitions
read_inheritable_attribute(:attachment_definitions)
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

module InstanceMethods #:nodoc:
Expand Down
17 changes: 5 additions & 12 deletions lib/paperclip/attachment.rb
Expand Up @@ -280,18 +280,11 @@ def extra_options_for(style) #:nodoc:

def post_process #:nodoc:
return if @queued_for_write[:original].nil?
return if fire_events(:before)
post_process_styles
return if fire_events(:after)
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)
instance.run_callbacks(:post_process) do
instance.run_callbacks(:"#{name}_post_process") do
post_process_styles
end
end
end

def post_process_styles #:nodoc:
Expand Down
8 changes: 4 additions & 4 deletions test/attachment_test.rb
Expand Up @@ -362,7 +362,7 @@ class Paperclip::Test < Paperclip::Processor; end

context "Assigning an attachment with post_process hooks" do
setup do
rebuild_model :styles => { :something => "100x100#" }
rebuild_class :styles => { :something => "100x100#" }
Dummy.class_eval do
before_avatar_post_process :do_before_avatar
after_avatar_post_process :do_after_avatar
Expand Down Expand Up @@ -402,16 +402,16 @@ def do_after_all; end
@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
@dummy.expects(:do_after_all)
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_after_avatar)
@dummy.expects(:do_before_all).with().returns(true)
@dummy.expects(:do_after_all).never
@dummy.expects(:do_after_all)
Paperclip::Thumbnail.expects(:make).never
@dummy.avatar = @file
end
Expand Down

0 comments on commit 9cd1f8e

Please sign in to comment.