Commit d8f45ee0 by Jon Yurek

Backporting the modifications for Rails 3 to Rails 2.3

parent 9b9b944f
......@@ -36,6 +36,7 @@ require 'paperclip/storage'
require 'paperclip/interpolations'
require 'paperclip/style'
require 'paperclip/attachment'
require 'paperclip/callback_compatability'
if defined?(Rails.root) && Rails.root
Dir.glob(File.join(File.expand_path(Rails.root), "lib", "paperclip_processors", "*.rb")).each do |processor|
require processor
......@@ -111,6 +112,13 @@ module Paperclip
def included base #:nodoc:
base.extend ClassMethods
if base.respond_to?("set_callback")
base.send :include, Paperclip::CallbackCompatability::Rails3
elsif !base.respond_to?("define_callbacks")
base.send :include, Paperclip::CallbackCompatability::Rails20
else
base.send :include, Paperclip::CallbackCompatability::Rails21
end
end
def processor name #:nodoc:
......@@ -314,22 +322,6 @@ module Paperclip
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:
......
......@@ -280,8 +280,8 @@ module Paperclip
def post_process #:nodoc:
return if @queued_for_write[:original].nil?
instance.run_callbacks(:post_process) do
instance.run_callbacks(:"#{name}_post_process") do
instance.run_paperclip_callbacks(:post_process) do
instance.run_paperclip_callbacks(:"#{name}_post_process") do
post_process_styles
end
end
......
......@@ -2,32 +2,80 @@ module Paperclip
# This module is intended as a compatability shim for the differences in
# callbacks between Rails 2.0 and Rails 2.1.
module CallbackCompatability
module Rails20
def self.included(base)
base.extend(ClassMethods)
base.send(:include, InstanceMethods)
base.extend(Defining)
base.send(:include, Running)
puts "Including Rails 2.0 Compatability"
end
module ClassMethods
# The implementation of this method is taken from the Rails 1.2.6 source,
# from rails/activerecord/lib/active_record/callbacks.rb, line 192.
def define_callbacks(*args)
args.each do |method|
self.class_eval <<-"end_eval"
def self.#{method}(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(#{method.to_sym.inspect}, callbacks)
module Defining
def define_paperclip_callbacks(*args)
end
end_eval
end
module Running
def run_paperclip_callbacks(callback, opts = nil, &blk)
end
end
end
module Rails21
def self.included(base)
base.extend(Defining)
base.send(:include, Running)
end
module Defining
def define_paperclip_callbacks(*args)
args.each do |callback|
define_callbacks("before_#{callback}")
define_callbacks("after_#{callback}")
end
end
end
module Running
def run_paperclip_callbacks(callback, opts = nil, &blk)
# The overall structure of this isn't ideal since after callbacks run even if
# befores return false. But this is how rails 3's callbacks work, unfortunately.
if run_callbacks(:"before_#{callback}"){ |result, object| result == false } != false
blk.call
end
run_callbacks(:"after_#{callback}"){ |result, object| result == false }
end
end
end
module Rails3
def self.included(base)
base.extend(Defining)
base.send(:include, Running)
end
module InstanceMethods
# The callbacks in < 2.1 don't worry about the extra options or the
# block, so just run what we have available.
def run_callbacks(meth, opts = nil, &blk)
callback(meth)
module Defining
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 Running
def run_paperclip_callbacks(callback, opts = nil, &block)
run_callbacks(callback, opts, &block)
end
end
end
end
end
......@@ -399,12 +399,17 @@ class AttachmentTest < Test::Unit::TestCase
end
should "cancel the processing if a before_post_process returns false" do
begin
$DEBUG = true
@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)
Paperclip::Thumbnail.expects(:make).never
@dummy.avatar = @file
ensure
$DEBUG = false
end
end
should "cancel the processing if a before_avatar_post_process returns false" do
......
......@@ -8,8 +8,9 @@ require 'mocha'
gem 'sqlite3-ruby'
require 'active_record'
require 'active_support'
require 'activerecord'
require 'activesupport'
require 'actionpack'
begin
require 'ruby-debug'
rescue LoadError
......@@ -94,8 +95,9 @@ class FakeModel
@errors ||= []
end
def run_callbacks name, *args
def run_paperclip_callbacks name, *args
end
end
def attachment options
......
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